Ask Your Question

Remove black border lines around image after scan with opencv c++

asked 2014-02-17 03:03:25 -0500

vsay gravatar image

updated 2014-02-17 07:59:11 -0500

I have tiff images. After scan those images, there are long black lines around the the borders.

I manualy crop the image with increase x += 50; y += 50; width -= 100; heigth -= 100

With above approache, I can crop the images.

I want to ask if there is any way i could remove it autometically without tunning the parameters of x, y, width and heigth such as image enhancement?



  • The image is grayscale image
  • There is only black and white color in the image so basically i would like to replace those black color with whit color
  • After scan the image there are long black pixel at the borders of the image and i want to remove that
edit retag flag offensive close merge delete



you probably meant: x += 50; y += 50; width -= 100; heigth -= 100

berak gravatar imageberak ( 2014-02-17 04:58:24 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-02-17 16:51:51 -0500

Nghia gravatar image

updated 2014-02-17 16:52:26 -0500

If you can determine the first border pixel accurately eg. occurs near (0,0) then you might be able to

  1. floodFill all black pixels starting at (0,0) or where ever, generate a binary image such that 0 is for border pixels, and 1 is for everything else
  2. use findContour + boundingRect to find the bounding box of the inside pixels. The findContour is very fast, it only visits pixels on the edge (not inside like a floodFill). findContour will return an array of possible contours, pick the largest one.
  3. crop the borders out using the bounding rect of the non-border pixels
edit flag offensive delete link more

answered 2014-02-17 09:16:35 -0500

updated 2014-02-17 09:17:16 -0500

I would suggest running 4 nested loops on x,y, width, height and using the function countNonZero to count the number of white pixels. You can also take the negative image and use countNonZero to count the number of black pixels.


negative image:

edit flag offensive delete link more


well, 4 nested loops will affect my program's performances

vsay gravatar imagevsay ( 2014-02-17 09:25:49 -0500 )edit

Question Tools


Asked: 2014-02-17 03:03:25 -0500

Seen: 6,315 times

Last updated: Feb 17 '14