0

I'm trying to figure out how to automatically cut some images like the one below (this is a negative film), basically, I want to remove the blank parts at the top and at the bottom. I'm not looking for complete code for it, I just want to understand a way to do it. The language is not important at this point, but I think this kind of thing normally is accomplished with Python.

enter image description here

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Édipo Féderle
  • 4,169
  • 5
  • 31
  • 35
  • 1
    You could invert the image, so the empty white areas become black. Then sum the pixels across the image to see where it ceases to be black... https://stackoverflow.com/a/54286937/2836621 – Mark Setchell Jan 21 '22 at 14:21

1 Answers1

2

I think there are several ways to do that, ranging from simple to complex. You can see the problem as detecting white rectangles or segmenting the image I would say.

I can suggest you opencv (which is available in more than one language, among which python), you can have a look here at the image processing examples

First we need to find the white part, then remove it.

Finding the white part

Thresholding

Let's start with an easy one: thresholding

Thresholding means dividing the image into two parts (usually black and white). You can do that by selecting a threshold (in your case, the threshold would be towards white - or black if you invert the image). By doing so, however, you may also threshold some parts of the images (for example the chickens and the white part above the kid). Do you have information about position of the white stripes? Are they always on top and bottom of the image? If so, you can apply the thresholding operation only on the top 25% and bottom 25% of the image. You would then most likely not "ruin" the image.

Finding the white rectangle

If that does not work or you would like to try something else, you can see the white stripes as rectangles and try to find their contour. You can see how in this tutorial. In this case you do not get a binary image, but a bounding box of the white areas. You most likely find the chickens also in this case, but by looking at the bounding box is easy to understand which one are correct and which one not. You can also check this calculating areas of the bounding box (width * height) and keep only the big ones.

Removing the part

Once you get the binary image (white part and not white part) or the bounding box, you have to crop the image. This also can be done in several ways, but I think the easiest one would be cropping by just selecting the central part of the image. For example, if the image has H pixels vertically, you would keep only the pixel from H1 (the height of the first white space) to H-H2 (where H2 is the height of the second white space). There is no tutorial on cropping, but several questions here on SO, as for example this one.

Additional notes

You could use more advanced segmentation algorithms as the Watershed one or even learn and use advanced techinques as machine learning to do that (here an article), as you can see the rabbit hole is pretty deep in this case. But I believe that would be an overkill and already the easy techniques would give you some results in this case.

Hope this was helpful and have fun!

freerafiki
  • 539
  • 3
  • 10