2

How do i remove the irregular white pixels from the preprocessed image.

i have tried doing erosion and that would make all pixels black.

After Preprocessing:

enter image description here

Requirement:

enter image description here

My Code:

Mat  img, edges, erode, blurred, element;
element = getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7), cv::Point(-1,-1) ); 

img = imread("img1925.jpeg");    //  read the image
cv::Canny(img, edges, 30, 255, 3);    // detect the edges with threshold limit

//    cv::erode(edges, erode, element);

GaussianBlur(edges, blurred, cv::Size(7, 7), 0);    // blurring

//    Rect ROI = boundingRect(blurred);       // draw rect for ROI
//    Mat src = thresh(ROI);

namedWindow("image", WINDOW_NORMAL);
imshow("image", blurred);
waitKey(0);
return 0;

Looking for suggestions!

Praveen
  • 267
  • 1
  • 5
  • 19
  • You can easily use a function which makes pixels any value which you want. If you have a certain interval values of RGB, you can change any pixel you want: openCvImage.at(i,j)[0]=0; (i and j are the coordinates, [0] represents first layer, 0 is the value you assigned. Related topic: – Yunus Temurlenk Nov 25 '19 at 12:31
  • Unfortunately, I have lot of images and I am not sure where the irregular pixels are! So that option doesn't work. – Praveen Nov 25 '19 at 12:38
  • What do u mean exactly by saying "irregular pixels" – Yunus Temurlenk Nov 25 '19 at 13:23
  • irregularities in the image is what I mean when i tell irregular pixels. My roi might be located on the top and irregularities can be present at the bottom. So using the pointers to alter will work on 1-2 images not on thousands of images :( – Praveen Nov 25 '19 at 13:30
  • 1
    After preprocessing step, you can check the density of irregular pixels, to locate your roi correctly. – Yunus Temurlenk Nov 25 '19 at 13:40
  • 1
    This sounds like a solution. I would look into this. I would appreciate if you could throw some light too! – Praveen Nov 25 '19 at 13:43
  • But there are some issues u ll probably have problem. 1- Gaussian Blur doesnt always give the results you need, 2- You can check the pixel values of your image using the method I mentioned in first comment of me. 3- You can see where the white pixels are majority and locate your rectangle there. – Yunus Temurlenk Nov 25 '19 at 13:57

1 Answers1

2

I'm not sure what you mean by removing the irregular white pixels from the preprocessed image but if your goal is to extract a ROI of the object then here's an approach:

  • Convert image to grayscale and Gaussian blur to smooth image
  • Adaptive threshold to obtain a binary image
  • Find contours and filter using contour area
  • Extract the largest contour in the image and crop the ROI

Here's the result

enter image description here

If you cropped the ROI on the thresholded image, here's the result

enter image description here

I implemented it in Python but you can follow the same steps in C++

import cv2

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,3)

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    break

cv2.imwrite('ROI.png', ROI)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137