用于 C++ 图像分析的 OpenCV 二进制图像掩码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31554187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:58:45  来源:igfitidea点击:

OpenCV Binary Image Mask for Image Analysis in C++

c++opencvimage-processingmaskthreshold

提问by MSTTm

I'm trying to analyse some images which have a lot of noise around the outside of the image, but a clear circular centre with a shape inside. The centre is the part I'm interested in, but the outside noise is affecting my binary thresholding of the image.

我正在尝试分析一些图像,这些图像在图像外部周围有很多噪音,但内部有一个清晰的圆形中心。中心是我感兴趣的部分,但外部噪声正在影响我对图像的二进制阈值处理。

To ignore the noise, I'm trying to set up a circular mask of known centre position and radius whereby all pixels outside this circle are changed to black. I figure that everything inside the circle will now be easy to analyse with binary thresholding.

为了忽略噪音,我试图设置一个已知中心位置和半径的圆形遮罩,由此该圆圈外的所有像素都变为黑色。我认为圆圈内的所有内容现在都可以很容易地使用二进制阈值进行分析。

I'm just wondering if someone might be able to point me in the right direction for this sort of problem please? I've had a look at this solution: How to black out everything outside a circle in Open CVbut some of my constraints are different and I'm confused by the method in which source images are loaded.

我只是想知道是否有人可以为我指出解决此类问题的正确方向?我看过这个解决方案:如何在 Open CV 中将圆圈外的所有内容涂黑,但我的一些约束不同,我对加载源图像的方法感到困惑。

Thank you in advance!

先感谢您!

回答by Derman

//First load your source image, here load as gray scale
cv::Mat srcImage = cv::imread("sourceImage.jpg", CV_LOAD_IMAGE_GRAYSCALE);

//Then define your mask image
cv::Mat mask = cv::Mat::zeros(srcImage.size(), srcImage.type());

//Define your destination image
cv::Mat dstImage = cv::Mat::zeros(srcImage.size(), srcImage.type());    

//I assume you want to draw the circle at the center of your image, with a radius of 50
cv::circle(mask, cv::Point(mask.rows/2, mask.cols/2), 50, cv::Scalar(255, 0, 0), -1, 8, 0);

//Now you can copy your source image to destination image with masking
srcImage.copyTo(dstImage, mask);

Then do your further processing on your dstImage. Assume this is your source image:

然后对您的dstImage. 假设这是您的源图像:

enter image description here

在此处输入图片说明

Then the above code gives you this as gray scale input:

然后上面的代码给你这个作为灰度输入:

enter image description here

在此处输入图片说明

And this is the binary mask you created:

这是您创建的二进制掩码:

enter image description here

在此处输入图片说明

And this is your final result after masking operation:

这是屏蔽操作后的最终结果:

enter image description here

在此处输入图片说明

回答by Giridhur

Since you are looking for a clear circular center with a shape inside, you could use Hough Transform to get that area- a careful selection of parameters will help you get this area perfectly.

由于您正在寻找内部有形状的清晰圆形中心,您可以使用霍夫变换来获得该区域 - 仔细选择参数将帮助您完美地获得该区域。

A detailed tutorial is here: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

详细教程在这里:http: //docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

For setting pixels outside a region black:

设置黑色区域外的像素:

Create a mask image : cv::Mat mask(img_src.size(),img_src.type());

创建蒙版图像: cv::Mat mask(img_src.size(),img_src.type());

Mark the points inside with white color :

用白色标记里面的点:

cv::circle( mask, center, radius, cv::Scalar(255,255,255),-1, 8, 0 );

cv::circle( mask, center, radius, cv::Scalar(255,255,255),-1, 8, 0 );

You can now use bitwise_AND and thus get an output image with only the pixels enclosed in mask.

您现在可以使用 bitwise_AND 从而获得仅包含在掩码中的像素的输出图像。

cv::bitwise_and(mask,img_src,output);

cv::bitwise_and(mask,img_src,output);