2015-07-22 141 views
2

我正試圖分析一些圖像,這些圖像在圖像的外部有很多噪點,但裏面有一個形狀清晰的圓形中心。該中心是我感興趣的部分,但外部噪聲正在影響我對圖像的二進制閾值。用於C++圖像分析的OpenCV二進制圖像蒙版

要忽略噪聲,我試圖設置一個已知中心位置和半徑的圓形掩膜,從而將該圓圈外的所有像素都更改爲黑色。我認爲圈內的一切現在都可以通過二進制閾值進行分析。

我只是想知道如果有人能夠指出我對這類問題的正確方向嗎?我已經看過這個解決方案:How to black out everything outside a circle in Open CV但我的一些約束是不同的,我很困惑的源圖像加載方法。

預先感謝您!

+0

上傳部分樣本圖片,並附上您的問題的描述。 –

回答

14
//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的進一步處理。假設這是你的源圖像:

enter image description here

接着上面的代碼給你這是灰度輸入:

enter image description here

這是你創建的二進制掩碼:

enter image description here

這是您的隱藏操作後的最終結果N:

enter image description here

+0

非常感謝!這正是我所期待的!完善! – MSTTm

3

由於您使用的是形狀內尋找一個清晰的圓形中心,你可以使用霍夫變換來獲取面積 - 參數的精心選擇將幫助你完全得到這個區域。

詳細教程這裏: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

對於黑色的區域的外側設置像素:用白色 cv::Mat mask(img_src.size(),img_src.type());

標記內的點:

創建掩模圖像

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

您現在可以使用bitwise_AND,從而獲得僅包含在蒙版中的像素的輸出圖像。

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

+1

也謝謝你的幫助。非常感激。 – MSTTm