Search code examples
pythonimageopencvimage-processingcut

How is it possible to detect multiple attached images, and cut them off?


Good evening :)

I would like to create a script capable of cutting the images present on large image one by one, so I want to cut out the 9 different images.

enter image description here

It is not so difficult to cut out these images because in fact it is enough just to cut out the good lines and the turn is played .. So where is my problem?

This problem becomes complicated, because there exist indeed several variants of great images; For example, it is likely that my script falls on a large image such as enter image description here

(Here, you can see 8 different images, so the lines to be cut are different).

So how do I get there?

I am still a beginner and I do not understand everything :(

I hope I have been as clear as possible. Thank you very much in advance!


Solution

  • Create two arrays, one in the length of the image width and one in the length of the image height and init them with zeroes.

    Since you are trying to test similarity of horizontal lines and vertical lines, you should iterate through the image pixels and compare each pixel in the position (x,y) to the pixel in the location (x+1,y) and (x,y+1) meaning each pixel is compared to the pixel right to it and the pixel below it. The result of each comparison should result with a similarity percentage.

    To calculate similarity percentage of two pixels you can follow the answer to this question: Algorithm to check similarity of colors

    The percentage result of each horizontal comparison of pixels in the location (x,y) should be added to the value in the horizontal array in the location x. So for example if:

    pixel(3,0) compare to pixel(4,0) = 80%
    horizontalArray[3] += 80% (80)
    pixel(3,1) compare to pixel(4,1) = 72%
    horizontalArray[3] += 72% (152)
    pixel(3,2) compare to pixel(4,2) = 95%
    horizontalArray[3] += 95% (247)
    ...
    

    And in a similar way calculate the values for the vertical comparison:

    pixel(0,3) compare to pixel(0,4) = 22%
    verticalArray[3] += 80% (22)
    pixel(1,3) compare to pixel(1.4) = 10%
    verticalArray[3] += 72% (32)
    pixel(2,3) compare to pixel(2,4) = 76%
    verticalArray[3] += 95% (108)
    ...
    

    After iterating through all pf the image pixels divide the values in the horizontalArray with the image height and the values in the verticalArray with the image width. the result of this action leave you now with two arrays containing average similarity percentage of each horizontal and each vertical line in you picture, now you can choose a arbitrary a magic number, lets say 15% and say that each line that got less than 15% similarity is a line you will perform a cut by it.

    Test the script and see how accurately it find the correct lines.

    If it's not sensitive enough increase the the value of your "magic number", if it is too sensitive and find lines where it shouldn't decrease the value of this magic number and try again.

    EDIT

    I edited my suggestion to color similarity formula as it had mistakes in it that would lead to not accurate results. Instead I added reference to another answer dealing with the question of color comparison, use it with the rest of the algorithm and it should work.