OpenCV:等間隔採樣和局部均值的圖像縮小

void scalePartAverage(const Mat &src, Mat &dst, double xRatio, double yRatio)
{
    int rows = static_cast<int>(src.rows * xRatio);
    int cols = static_cast<int>(src.cols * yRatio);

    dst.create(rows, cols, src.type());

    int lastRow = 0;
    int lastCol = 0;

    Vec3b *p;
    for (int i = 0; i < rows; i++) {
        p = dst.ptr<Vec3b>(i);
        int row = static_cast<int>((i + 1) / xRatio + 0.5) - 1;

        for (int j = 0; j < cols; j++) {
            int col = static_cast<int>((j + 1) / yRatio + 0.5) - 1;

            Vec3b pix;
            average(src, Point_<int>(lastRow, lastCol), Point_<int>(row, col), pix);
            p[j] = pix;

            lastCol = col + 1; //下一個子塊左上角的列座標,行座標不變
        }
        lastCol = 0; //子塊的左上角列座標,從0開始
        lastRow = row + 1; //子塊的左上角行座標
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章