繁体   English   中英

将泛洪输出(连接的像素)复制到新垫中

[英]Get floodfill outputs (connected pixels) copied into a new mat

是否有一种方便的方法可以根据floodfill作业的输出来创建新的垫子? 我想获得仅包含被检测为连接到种子像素并在技术上进行洪水填充的像素的垫子。

猜猜我执行了floodFill方法到某个种子点,并且在连接时仅填充了总像素的1/4。 我只想将这些像素复制到一个新图像,该图像仅代表那些1/4像素,并且很可能小于原始输入图像。

无论如何,我都是通过一种耗时长,CPU使用时间较长的方法来完成此操作的。 简而言之,我的方法是为不同的floodfill调用提供不同的颜色,并将相同颜色的像素记录保留在单独的数据结构中,等等。

我想知道是否存在使用直接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;
}

测试图片img

在此处输入图片说明

floodFill后的口罩mask

在此处输入图片说明

仅使用seedColor像素的蒙版result

在此处输入图片说明

裁剪面膜裁剪croppedResult

在此处输入图片说明


更新

    // B/W image, where white pixels are the one set to seedColor by floodFill
    Mat1b resultMask = (mask == seedColor);
    Mat1b resultMaskWithoutBorder = resultMask(Rect(1,1,img.cols,img.rows));

    Mat3b originalImage;
    cvtColor(img, originalImage, COLOR_GRAY2BGR); // Probably your original image is already 3 channel

    Mat3b imgMasked(img.size(), Vec3b(0,0,0));
    originalImage.copyTo(imgMasked, resultMaskWithoutBorder);

    Mat3b croppedResult = imgMasked(roi);
    return 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM