OpenCV Binary Image Mask for Image Analysis in C++

谁都会走 提交于 2019-12-04 09:49:50

问题


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

Thank you in advance!


回答1:


//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:

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:




回答2:


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

For setting pixels outside a region black:

Create a mask image : 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 );

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

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



来源:https://stackoverflow.com/questions/31554187/opencv-binary-image-mask-for-image-analysis-in-c

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