OpenCV - Floodfill onto new Mat

江枫思渺然 提交于 2019-12-17 18:29:13

问题


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!


回答1:


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 )


  • Original image

    cv::Mat img = cv::imread("squares.png");

  • First variant

    cv::floodFill(img, cv::Point(150,150), cv::Scalar(255.0, 255.0, 255.0));

This is the 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);

This is the mask. img doesn't change


If you go with this though, note that:

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.



来源:https://stackoverflow.com/questions/12574937/opencv-floodfill-onto-new-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!