Java and OpenCV color detection

2019-09-12 07:32发布

问题:

I'm working on a project at school, which basically is: writing an application to make a drone fly autonomously, and through scanning QR-codes hung up on walls, be able to navigate through a room in order to complete a certain task.

What I am currently working on, is for the drone to detect cardboard boxes (working as obstacles). These boxes are white, and have a blue circle on them. How I'm planning to solve this, is by scanning the frame for colors and squares:

If the drone detects a square, check if it's white. If it's white, check if it contains a blue circle. If it does, I can say that it most likely is a cardboard box.

This is what the box looks like:

If anyone would be able to provide some pointers as to how I can start working on the color detection, I would be very happy!

PS: I haven't provided any code, since I don't really know what to provide. I would be more than happy to provide some if needed

UPDATE: for anyone stuck at the same problem as I, a fellow student provided an excellent link for my exact situation:

http://opencv-java-tutorials.readthedocs.io/en/latest/08-object-detection.html

回答1:

I would go from a different angle to do this by detecting the blue circle first.

  1. Detect base colors

    see RGB value base color name

  2. Select all blue pixels neighboring white or gray-ish ones

    As your circlehas black border then you have to select all blue pixels near white,gray,black... just to be sure. This is the result (Green are selected pixels):

    another (more robust) possibility is to select all black pixels neighboring white and blue at the same time.

  3. do a connected components analysis

    so merge all connected pixels into polylines

  4. For each polyline decide if it is circle/ellipse/oval

    that can be done by investigating angle between line segments. If has sharp spikes then sharp edges are present and it is not an oval. If the circumference is too far from circle/elipse/oval computed from its bounding box then it is not oval but some more complicated curvature.

  5. For each oval decide if it is filled with blue

    so just flood fill mask of the oval circumference and compare how many pixels are int the original image blue against those that are not. if the ratio is closer to 100% blue then it is filled blue oval shape....

    As your marker has also some features inside you can compute the ratio of all base colors inside it to more accurately detect the marker.

    Look at Algorithms: Ellipse matching for some additional ideas.

  6. now you can similarly check if the background is white/gray-ish

There are a lot of other possible approaches like OCR and character similarity or based on FFT/DCT, Hough transform for circles... also you are not bound only to geometric properties comparation instead you can compare histograms...