OpenCV - 洪水填充到新Mat [英] OpenCV - Floodfill onto new Mat
问题描述
考虑到图像上的一个点,我想要填充连接到该点的所有点 - 但是要填充到新图像上。一种天真的方法是将原始图像填充到特殊的魔术颜色值。然后,访问每个像素,并将具有此魔术颜色值的所有像素复制到新图像。必须有更好的方法!
Given a point on an image, I'd like to floodfill all points connected to that point - but onto a new image. A naive way to do this would be to floodfill the original image to a special magic colour value. Then, visit each pixel, and copy all pixels with this magic colour value to the new image. There must be a better way!
推荐答案
为什么不使用 cv :: floodFill 创建一个面具?
Why don't you use the second variant of cv::floodFill to create a mask?
int floodFill(InputOutputArray image,InputOutputArray mask,Point
seedPoint,Scalar newVal,Rect * rect = 0,Scalar loDiff = Scalar(),Scalar
upDiff = Scalar(),int flags = 4)
int floodFill(InputOutputArray image, InputOutputArray mask, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )
-
原始图片
Original image
cv :: Mat img = cv :: imread(square。 png);
-
第一个变种
First variant
cv :: floodFill(img,cv :: Point(150,150),cv :: Scalar(255.0,255.0,255.0));
这是 img
-
第二变体
Second variant
cv :: Mat mask = cv :: Mat :: zeros(img.rows + 2,img.cols + 2,CV_8U) ;
cv :: floodFill(img,mask,cv :: Point(150,150),255,0,cv :: Scalar(),cv :: Scalar (),4 +(255 << 8)+ cv :: FLOODFILL_MASK_ONLY);
这是掩码
。 img
不会改变
如果您使用此功能,请注意:
If you go with this though, note that:
由于蒙版大于填充图像,因此像素(x) ,y)图像对应于蒙版中的像素(x + 1,y + 1)。
这篇关于OpenCV - 洪水填充到新Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!