how do you remove a protruding part of a square shape?

For this specific image my pipeline would be very simple:

  1. Binary threshold the image with a fixed threshold. The rectangle is quite dark compared to the rest of the image.
  2. Morphological opening with a large rectangular kernel to get rid of the “noise”.
  3. To get a perfect rectangle, determine the bounding rectangle of the remaining part, and draw a white rectangle.

That’d be the whole code:

// Read image
cv::Mat img = cv::imread("OTH61.png", cv::IMREAD_GRAYSCALE);

// Binary threshold image at fixed threshold
cv::Mat img_thr;
cv::threshold(img, img_thr, 32, 255, cv::THRESH_BINARY_INV);

// Morphological opening with large rectangular kernel
cv::Mat img_mop;
cv::morphologyEx(img_thr, img_mop, cv::MORPH_OPEN, cv::Mat::ones(51, 51, CV_8UC1));

// Draw rectangle w.r.t. to the bounding rectangle of the remaining part
cv::rectangle(img_mop, cv::boundingRect(img_mop), 255, cv::FILLED);

The thresholded image:

Thresholded image

The morphological opened image:

Opened image

The cleaned image:

Cleaned image

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top