C. Hediger
C. Hediger

Reputation: 482

Recognize small objects with OpenCV

I would like to be able to recognize the position (center) and the angle of some small components with openCV with C#. To achieve that, I am grabbing pictures from a webcam and try to process them with the Canny algorithm. Unfortunately, the results are not that good as expected. Sometimes it is ok sometimes it is not.

I have attached an example image from the cam and the corresponding output of OpenCV.

I hope that someone could give me hints or maybe some code snippets, how to achieve my desired results. Is this something that is usually done with AI?

Example images:

Input:

Input image

Output 1:

enter image description here

Output 2:

enter image description here

Expected:

enter image description here

Thanks.

Actual code:

        Mat src;
        src = BitmapConverter.ToMat(lastFrame);
        Mat dst = new Mat();
        Mat dst2 = new Mat();

        Cv2.Canny(src, dst, hScrollBar1.Value, hScrollBar2.Value);

        // Find contours
        OpenCvSharp.Point[][] contours; //vector<vector<Point>> contours;
        HierarchyIndex[] hierarchyIndexes; //vector<Vec4i> hierarchy;

        Cv2.FindContours(dst, out contours, out hierarchyIndexes, RetrievalModes.External, ContourApproximationModes.ApproxTC89L1);

        foreach (OpenCvSharp.Point[] element in contours)
        {
            var biggestContourRect = Cv2.BoundingRect(element);

            Cv2.Rectangle(dst,
                new OpenCvSharp.Point(biggestContourRect.X, biggestContourRect.Y),
                new OpenCvSharp.Point(biggestContourRect.X + biggestContourRect.Width, biggestContourRect.Y + biggestContourRect.Height),
                new Scalar(255, 0, 0), 3);
        }



        using (new Window("dst image", dst)) ;
        using (new Window("src image", src)) ;

Upvotes: 0

Views: 344

Answers (1)

Rakshith G B
Rakshith G B

Reputation: 826

If you already have a ROI (the box) and you just want to compute the actual orientation of it, you could use the contour inside the right box and compute its moments. A tutorial on how to do this is here (Sorry only C++).

Once you have the moments you can compute the orientation easily. To do this follow the solution here.

If you have trouble figuring out the right box itself, you are actually half way with canny boxes. You could then further try:

Equalize source image:

equalize

Posterize next (to 2 levels):

Posterize

Threshold (255):

Threshold

Then you can use all the canny boxes you found in the centre and use them as masks to get the right contour in the thresholded image. You can then find the biggest contour here and compute its orientation with image moments. Hope this helps!

Upvotes: 1

Related Questions