Search code examples
opencvaugmented-realityconvolutionphotogrammetry

OpenCV: Detect a black to white gradient in an area


I uploaded an example image for better understanding: http://www.imagebanana.com/view/kaja46ko/test.jpg

In the image you can see some scanlines and a marker (the white retangle with the circle in it). I want OpenCV to go along a specified area (in the example outlined trough the scanlines) that should be around 5x5. If that area contains a gradient from black to white, I want OpenCV to save the position of that area, so that I can work with it later.

The final result would be to differentiate between the marker and the other retangles separated trough black and white lines.

Is something like that possible? I googled a lot but I only found edge detectors but that's not what I want, I really need the detection of the black to white gradient only.

Thanks in advance.


Solution

  • Thanks for your answerer dnul, but it didn't really help me work this out. I though about a histogram to approach the problem but it's not quite what I want.

    I solved this problem by creating a 40x40 matrix which holds 5x5 matrix's containing the raw pixel data in all 3 channels. I iterated trough each 40px-area and inside iterated trough each border of 5px-area. I checked each pixel and saved the ones which are darker then a certain threshold a storage.

    After the iteration I had a rough idea of how many black pixels their are, so I checked each one of them for neighbors with white-pixels in all 3 channels. I then marked each of those pixels and saved them to another storage.

    I then used the ransac algorithm to construct lines out of these points. It constructs about 5-20 lines per marker edge. I then looked at the lines which meet each other and saved the position of those that meet in a square angle. The 4 points I get from that are the edges of the marker.

    If you want to reproduce this you would have to filter the image in beforehand and apply a threshold to make it easier to distinguish between black and white pixels.

    A sample picture, save after finding the points and before constructing the lines: http://www.imagebanana.com/view/i6gfe6qi/9.jpg