Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将泛光填充输出(连接的像素)复制到新垫中_C++_Image_Opencv_Image Processing_Flood Fill - Fatal编程技术网

C++ 将泛光填充输出(连接的像素)复制到新垫中

C++ 将泛光填充输出(连接的像素)复制到新垫中,c++,image,opencv,image-processing,flood-fill,C++,Image,Opencv,Image Processing,Flood Fill,是否有一种方便的方法可以从溢流填充操作的输出创建一个新的垫?我想得到一个只有像素的垫子,这些像素被检测为连接到种子像素,并且在技术上是泛光填充的 我猜我对某个种子点执行了泛光填充方法,当它们连接时,只填充了总像素的1/4。我只想将这些像素复制到一个新的图像中,它只表示1/4个像素,并且很可能比原始输入图像小 无论如何,我都是通过一种非常长、更耗时的方法来实现这一点的。简而言之,我的方法是为不同的泛光填充调用提供不同的颜色,并在单独的数据结构中保存相同颜色像素的记录,等等 我想知道是否有一种直接且

是否有一种方便的方法可以从溢流填充操作的输出创建一个新的垫?我想得到一个只有像素的垫子,这些像素被检测为连接到种子像素,并且在技术上是泛光填充的

我猜我对某个种子点执行了泛光填充方法,当它们连接时,只填充了总像素的1/4。我只想将这些像素复制到一个新的图像中,它只表示1/4个像素,并且很可能比原始输入图像小

无论如何,我都是通过一种非常长、更耗时的方法来实现这一点的。简而言之,我的方法是为不同的泛光填充调用提供不同的颜色,并在单独的数据结构中保存相同颜色像素的记录,等等


我想知道是否有一种直接且更简单的方法,使用由floodfill创建的遮罩或使用任何其他方法。

还不完全清楚您到底需要什么。 请查看此代码,并检查croppedResult是否符合您的要求

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Create a test image
    Mat1b img(300, 200, uchar(0));
    circle(img, Point(150, 200), 30, Scalar(255));
    rectangle(img, Rect(30, 50, 40, 20), Scalar(255));
    rectangle(img, Rect(100, 80, 30, 40), Scalar(255));

    // Seed inside the circle
    Point seed(160, 220);

    // Setting up a mask with correct dimensions
    Mat1b mask;
    copyMakeBorder(img, mask, 1, 1, 1, 1, BORDER_CONSTANT, Scalar(0));

    Rect roi;
    uchar seedColor = 200;
    floodFill(img, mask,
        seed + Point(1,1),  // Since the mask is larger than the filled image, a pixel (x,y) in image corresponds to the pixel (x+1,y+1) in the mask
        Scalar(0),          // If FLOODFILL_MASK_ONLY is set, the function does not change the image ( newVal is ignored),
        &roi,               // Minimum bounding rectangle of the repainted domain.
        Scalar(5),          // loDiff
        Scalar(5),          // upDiff 
        4 | (int(seedColor) << 8) | FLOODFILL_MASK_ONLY);
        // 4-connected | with defined seedColor | use only the mask 

    // B/W image, where white pixels are the one set to seedColor by floodFill
    Mat1b result = (mask == seedColor);

    // Cropped image
    roi += Point(1,1);
    Mat1b croppedResult = result(roi);

    return 0;
}

你到底需要什么还不完全清楚。 请查看此代码,并检查croppedResult是否符合您的要求

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Create a test image
    Mat1b img(300, 200, uchar(0));
    circle(img, Point(150, 200), 30, Scalar(255));
    rectangle(img, Rect(30, 50, 40, 20), Scalar(255));
    rectangle(img, Rect(100, 80, 30, 40), Scalar(255));

    // Seed inside the circle
    Point seed(160, 220);

    // Setting up a mask with correct dimensions
    Mat1b mask;
    copyMakeBorder(img, mask, 1, 1, 1, 1, BORDER_CONSTANT, Scalar(0));

    Rect roi;
    uchar seedColor = 200;
    floodFill(img, mask,
        seed + Point(1,1),  // Since the mask is larger than the filled image, a pixel (x,y) in image corresponds to the pixel (x+1,y+1) in the mask
        Scalar(0),          // If FLOODFILL_MASK_ONLY is set, the function does not change the image ( newVal is ignored),
        &roi,               // Minimum bounding rectangle of the repainted domain.
        Scalar(5),          // loDiff
        Scalar(5),          // upDiff 
        4 | (int(seedColor) << 8) | FLOODFILL_MASK_ONLY);
        // 4-connected | with defined seedColor | use only the mask 

    // B/W image, where white pixels are the one set to seedColor by floodFill
    Mat1b result = (mask == seedColor);

    // Cropped image
    roi += Point(1,1);
    Mat1b croppedResult = result(roi);

    return 0;
}

谢谢你的回复。但我不理解这些说法裁剪图像roi+=点1,1;Mat1b croppedResult=resultroi;| | | |泛光填充的roi参数是什么,存储了什么?只选择值==种子颜色的像素;向下平移Rect 1px,向右平移1px;使用Rect获取子矩阵。正如doc所说,roi是泛洪点的边界框。检查更新的答案。您只需要使用带有掩码的copyTo。您应该注意初始化矩阵,并确保维度和通道一致。:比较:A cmpop B,A cmpop alpha,alpha cmpop A,其中cmpop是其中之一:>,>=,=,!=,它基本上创建了一个二进制掩码resultMask,其中值等于maskare中seedColor的像素设置为255,或者0,感谢您的回复。但我不理解这些说法裁剪图像roi+=点1,1;Mat1b croppedResult=resultroi;| | | |泛光填充的roi参数是什么,存储了什么?只选择值==种子颜色的像素;向下平移Rect 1px,向右平移1px;使用Rect获取子矩阵。正如doc所说,roi是泛洪点的边界框。检查更新的答案。您只需要使用带有掩码的copyTo。您应该注意初始化矩阵,并确保维度和通道一致。:比较:A cmpop B,A cmpop alpha,alpha cmpop A,其中cmpop是其中之一:>,>=,=,!=,它基本上创建一个二进制掩码结果掩码,其中值等于maskare中seedColor的像素设置为255,否则为0