SouvikMaji
SouvikMaji

Reputation: 1145

How to remove lines from the sides in opencv?

I have to remove some lines from the sides of hundreds of grayscale images.

enter image description here

In this image lines appear in three sides.

The lines are not consistent though, i.e, they appear above, below, left and/or right side of the image. And they are of unequal length and width.

Upvotes: -1

Views: 1292

Answers (1)

Humam Helfawi
Humam Helfawi

Reputation: 20264

If you could assume that the borders are free of important information, you may crop the photo like this:

C++ code:

cv::Mat img;
//load your image into img;
int padding=MAX_WIDTH_HEIGHT_OF_THE LINEAS_AREA
img=img(cv::Rect(padding,padding,img.cols-padding,img.rows-padding));

If not, you have to find a less dumb solution like this for example:

  1. Findcontours
  2. Delete contours that are far from the borders.
  3. Draw contours on blank image
  4. Apply hough line with suitable thresholds.
  5. Delete contours that intersect with lines inside the image border.

Another solution, assuming the handwritten shape is connected:

  1. Findcontours
  2. Get the contour with the biggest area.
  3. Draw it on a blank image with -1(fill) flag in the strock argument.
  4. bitwise_and between the original image and the one you made

Another solution, asuming that the handwritten shape could be discontinuity :

  1. Findcontours
  2. Delete any contour that its all points are very close to the border (using euclidian distance with a threshold)
  3. Draw all remaining contours on a blank image with -1(fill) flag in the strock argument.
  4. bitwise_and between the original image and the one you made

P.S. I did not touch HoughLine transform since I do not about the shapes. I assume that some of them may contain very straight lines.

Upvotes: 1

Related Questions