Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Object Detection - Center Point

Given an object on a plain white background, does anybody know if OpenCV provides functionality to easily detect an object from a captured frame?

I'm trying to locate the corner/center points of an object (rectangle). The way I'm currently doing it, is by brute force (scanning the image for the object) and not accurate. I'm wondering if there is functionality under the hood that i'm not aware of.

Edit Details: The size about the same as a small soda can. The camera is positioned above the object, to give it a 2D/Rectangle feel. The orientation/angle from from the camera is random, which is calculated from the corner points.

It's just a white background, with the object on it (black). The quality of the shot is about what you'd expect to see from a Logitech webcam.

Once I get the corner points, I calculate the center. The center point is then converted to centimeters.

It's refining just 'how' I get those 4 corners is what I'm trying to focus on. You can see my brute force method with this image: Image

like image 934
David McGraw Avatar asked Nov 10 '08 22:11

David McGraw


2 Answers

There's already an example of how to do rectangle detection in OpenCV (look in samples/squares.c), and it's quite simple, actually.

Here's the rough algorithm they use:

0. rectangles <- {} 1. image <- load image 2. for every channel: 2.1  image_canny <- apply canny edge detector to this channel 2.2  for threshold in bunch_of_increasing_thresholds: 2.2.1   image_thresholds[threshold] <- apply threshold to this channel 2.3  for each contour found in {image_canny} U image_thresholds: 2.3.1   Approximate contour with polygons 2.3.2   if the approximation has four corners and the angles are close to 90 degrees. 2.3.2.1    rectangles <- rectangles U {contour} 

Not an exact transliteration of what they are doing, but it should help you.

like image 126
Ismael C Avatar answered Sep 23 '22 01:09

Ismael C


Hope this helps, uses the moment method to get the centroid of a black and white image.

cv::Point getCentroid(cv::Mat img) {     cv::Point Coord;     cv::Moments mm = cv::moments(img,false);     double moment10 = mm.m10;     double moment01 = mm.m01;     double moment00 = mm.m00;     Coord.x = int(moment10 / moment00);     Coord.y = int(moment01 / moment00);     return Coord; } 
like image 22
Rod Dockter Avatar answered Sep 20 '22 01:09

Rod Dockter