Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize dirty region Detection in low contrast Image by OpenCV

Shulou Source: shulou.com Published: 2022-06-03 09:13:10 09月24日 Update

This article mainly introduces OpenCV how to achieve low-contrast image dirty area detection, the article is very detailed, has a certain reference value, interested friends must read it!

1. Detection of dirty areas in low-contrast images

First, the picture above:

If the first picture is not marked, I do not find where the dirty area is, and the second picture is clearer. We can basically see that there is a dark area near the left edge of the image. This is what we call the dirty area. This is also the area we want to detect.

Label the result diagram:

two。 Introduction of implementation method

Two implementation methods are introduced here.

The first is to use C++ to achieve reference blog posts, that is, using the gradient method to detect, the specific steps are as follows:

The image is denoised by Gaussian blur, and the gradient calculation is very sensitive to noise.

Call the Sobel function to calculate the gradient of the image in the x direction y direction.

Call the convertScaleAbs function to limit the pixel value of the XBI y gradient image to 0-255.

Call the addWeight function to fuse the XBI y gradient image.

Call the threshold function to binarize the fused image

The morphological processing method of corrosion first and then expansion is used to filter the non-dirty region of the binary image.

Call the findContours method to find the outline of the dirty area.

The second method is realized according to the idea of improving image contrast, and the specific steps are as follows:

8. Gaussian blur denoising of image

9. Using local histogram equalization to improve image contrast

10. Rough segmentation of dirty areas using OTSU binarization threshold method

11. Use the morphological operation of corrosion to filter out some non-dirty areas of binary images.

twelve。 Call the findContours method to find the outline of the dirty area.

3. C++ source code implementation # include # include int main () {using namespace cv; std::string strImgFile = "C:\\ Temp\\ common\\ Workspace\\ Opencv\\ images\\ led1.jpg"; Mat mSrc = imread (strImgFile); CV_Assert (mSrc.empty () = = false); Mat mSrc2 = mSrc.clone (); CV_Assert (mSrc2.empty () = = false) Mat mGray; cvtColor (mSrc, mGray, COLOR_BGR2GRAY); GaussianBlur (mGray, mGray, Size (5,5), 1.0); Mat mGray2 = mGray.clone (); CV_Assert (mGray.empty () = = false); imshow ("gray", mGray.clone ()); / / method 1: using gradient change to detect defects Mat mSobelX, mSobelY Sobel (mGray, mSobelX, CV_16S, 1,0,7); Sobel (mGray, mSobelY, CV_16S, 0,1,7); convertScaleAbs (mSobelX, mSobelX); convertScaleAbs (mSobelY, mSobelY); Mat mEdge; addWeighted (mSobelX, 1, mSobelY, 1,0, mEdge); imshow ("edge", mEdge); Mat mThresh Threshold (mEdge, mThresh, 0,255, THRESH_BINARY | THRESH_OTSU); imshow ("thresh", mThresh); Mat kernel1 = getStructuringElement (MORPH_RECT, Size (11,11)); CV_Assert (kernel1.empty () = = false); Mat mMorph; morphologyEx (mThresh, mMorph, MORPH_ERODE, kernel1); imshow ("erode", mMorph); Mat kernel2 = getStructuringElement (MORPH_RECT, Size (5,5)) MorphologyEx (mMorph, mMorph, MORPH_DILATE, kernel2); imshow ("dilate", mMorph); std::vector contours; findContours (mMorph, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); for (int I = 0; I

< contours.size(); i++) { float area = contourArea(contours[i]); if (area >

{drawContours (mSrc, contours, I, Scalar (0,0,255));}} imshow ("result1", mSrc.clone ()); / / method 2: using local histogram equalization method to detect defects Ptr ptrCLAHE = createCLAHE (20, Size (30,30); ptrCLAHE- > apply (mGray2, mGray2) Imshow ("equalizeHist", mGray2); Mat mThresh3; threshold (mGray2, mThresh3, 0,255, THRESH_BINARY_INV | THRESH_OTSU); CV_Assert (mThresh3.empty () = = false); imshow ("thresh", mThresh3); Mat kernel2_1 = getStructuringElement (MORPH_RECT, Size (9,9)); Mat mMorph3; morphologyEx (mThresh3, mMorph3, MORPH_ERODE, kernel2_1) CV_Assert (mMorph3.empty () = = false); imshow ("morph3", mMorph3); std::vector contours2; findContours (mMorph3, contours2, RETR_EXTERNAL, CHAIN_APPROX_NONE); for (int I = 0; I

< contours2.size(); i++) { float area = contourArea(contours2[i]); if (area >

{drawContours (mSrc2, contours2, I, Scalar (0,0,255));}} imshow ("result2", mSrc2); waitKey (0); destroyAllWindows (); system ("pause"); return 0;} 4. Result

The detection results of gradient method:

The detection results of local histogram equalization method:

These are all the contents of the article "how to detect dirty areas in low-contrast images by OpenCV". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Methods images regions stains detection gradients contrast results functions equalization local histograms content morphology steps articles defects contours Gaussian Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn OPPO Reno Shulou Information macOS