用于C ++图像分析的OpenCV二进制图像掩码 [英] OpenCV Binary Image Mask for Image Analysis in C++

查看:393
本文介绍了用于C ++图像分析的OpenCV二进制图像掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我只是想知道是否有人能够指出我正确的方向请问这类问题?我已经看过这个解决方案:如何在Open CV中消除圆圈外的所有内容但我的一些约束条件有所不同,我对加载源图像的方法感到困惑。

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 CV but some of my constraints are different and I'm confused by the method in which source images are loaded.

提前谢谢!

推荐答案

//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);

然后在 dstImage 上进行进一步处理。假设这是您的源图像:

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

然后上面的代码将此作为灰度输入:

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

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

And this is the binary mask you created:

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

And this is your final result after masking operation:

这篇关于用于C ++图像分析的OpenCV二进制图像掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆