OpenCV Binary Image Mask for Image Analysis in C ++

I am trying to analyze some images that have a lot of noise around the image, but a clean circular center with an inner shape. Center is what I'm interested in, but outside noise affects my image binary threshold.

To ignore the noise, I'm trying to set up a circle mask with a known center position and radius, so that all pixels outside that circle will be changed to black. I suppose everything inside the circle will now be easily parsed with a binary threshold.

I'm just wondering if anyone can point me in the right direction for this kind of problem? I looked into this solution: How to cross out everything outside the circle in Open CV , but some of my limitations are different and I'm confused about the method in which the original images are loaded.

Thank you in advance!

+3


source to share


2 answers


//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 carry out further processing on dstImage

. Let's assume this is your original image:

enter image description here

Then the above code gives you this value as a grayscale:

enter image description here



And this is the binary mask you created:

enter image description here

And this is your final result after the masking operation:

enter image description here

+16


source


Since you are looking for a clean circular center with an inner shape, you can use the Hough Transform to get this area - careful selection of parameters will help you get this area perfect.

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

To set pixels outside of the black area:

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



Mark the dots inside in white:

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

Now you can use bitwise_AND and thus get the output image with only the pixels enclosed in the mask.

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

+3


source







All Articles