Reputation: 31
Is there any algorithm to detect a corner of an object on a picture? I have a list of pictures that look like this:
Now on picture 1 I expect top-left corner, on picture 2 I expect bottom-right, etc.
The algorithm that I need should "ensure" that the image on the picture is definitely top-left / bottom-right / etc (not just a noise, for example like on the picture below) and if it is - to return the X and Y of the corner.
The additional problem is there could be some noise on the picture as in case of picture with the bottom right corner. One more problem is that the picture of the corner can be slightly rotated.
Slightly rotated corner
UPD I should also make sure the image is the picture of the corner that I expect, because the image can look like this:
Noise
Upvotes: 3
Views: 979
Reputation: 862
I think the core of @Davids answer is more robust if the corners you're after can be rotated. However, instead of using a flood-fill algorithm to determine the corner, perhaps performing a low pass filter would be better. Ill outline the process first, then explain why it might be a better option afterwards.
Start with you original image (top left panel of the image below) and filter it by convolving it with a 2D Gaussian of the same dimension. I built my Gaussian with sigma=5 and plotted the result in the top right panel below. Low pass filtering takes a binary image (made of 0s and 1s or black and white pixels) and smooths those values across the surrounding pixels. White pixels that are surrounded by black pixels will thus have a lower value than white pixels near other white pixels. Now you can set a threshold and select only those values above the threshold (Bottom left panel -- values above threshold are in green). Now you can use @Davids advice and find the shortest distance from the most upper left point of your image to the points above the threshold. The shortest distance should be the corner you're looking for.
Now the reason I suggest using a low pass filter is that if you perform similar smoothing operation to the noisy image you provided and use the same threshold you get distances at or very close to 0. See below:
Now it would be easy to put any number of conditionals to determine if the image is worth finding the corner in. For instance you could set a conditional on the total area of the green pixels above the threshold, determine a minimum distance to you corner, etc... You would of course have to build your Gaussian low pass filter and choose your threshold appropriately.
Upvotes: 1
Reputation: 862
You might consider a 2 dimensional cross correlation algorithm. Image cross correlation works by finding the point (x,y values) of overlap where two images best match (or correlate) with one another (Wikipedia--cross correlation). Considering you are looking for a corner, you can correlate a simple box with you original image. As shown below:
Then you correlate the box (above right hand image) with your original (above left hand image to find the point of best overlap. As shown below:
Note that the axes were expanded in the above image to show the entire shifted box image. My cross correlation returned a shift value of x=-87 y=63. Which means when I shift the box by those values, it best overlaps with the original image. Those shifted values also correspond to the corner of you are looking for! Simply add the shift in the x direction (-87) to the number of rows in your image. Thus the corner is at/near the point x=152 y=63 pixels.
Also note you can drastically speed this algorithm up by performing the cross correlation in the frequency domain (Wikipedia -- Convolution theorem)
Upvotes: 2
Reputation: 34563
I would start in the corner of the image and then loop through the pixels to find the white pixel that is closest to the corner. Then when you find a white pixel, use the flood-fill algorithm to count how many contiguous white pixels you see. If this pixel count is above your noise threshold (maybe 20 pixels?), then consider this pixel as your corner. This should handle images that are slightly rotated.
Upvotes: 3