python-OpenCV中使用漫水填充,遍历图像时报错,但是在C++程序中这样的遍历方式是没有错的,这样要怎么解决?

python-OpenCV中使用漫水填充,遍历图像时报错,但是在C++程序中这样的遍历方式是没有错的,这样要怎么解决?

问题描述:

其中h和w是图像的长和宽

for i in range(h):
        for j in range(w):
           cv2.floodFill(copyImg,mask,(i,j),tuple(copyImg[i][j]*1.0),(5,5,5),(5,5,5),cv2.FLOODFILL_FIXED_RANGE)

error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\floodfill.cpp:509: error: (-211:One of the arguments' values is out of range) Seed point is outside of image in function 'cv::floodFill'

在C++程序中,这样却是没有问题的

Mat mask(dst.rows + 2, dst.cols + 2, CV_8UC1, Scalar::all(0));
    for (int i = 0; i<dst.rows; i++)    
        for (int j = 0; j<dst.cols; j++)
            if (mask.at<uchar>(i + 1, j + 1) == 0)
            {
                Scalar newcolor(rng(256), rng(256), rng(256));
                floodFill(dst, mask, Point(j, i), newcolor, 0, Scalar::all(5), Scalar::all(5));
            }

看你代码,是不是把宽和高弄反了额,python里面i是h,height高,对应了C++里面的i的rows行,python里面的j是w,width宽,对应了C++里面的j的cols列,两边的i和j都是对应的,为啥函数调用传参是反着的额?

for i in range(h-1):
        for j in range(w-1):
看看,是不是越界了

有的点数,超出范围了