检测图像上的硬币(并适合椭圆)

问题描述 投票:0回答:4

我目前正在开展一个项目,我试图检测平坦表面(即桌子)上的一些硬币。硬币不会重叠,也不会被其他物体隐藏。但可能还有其他可见的物体,并且照明条件可能并不完美......基本上,想象一下你自己拍摄你的桌子上有一些硬币。

所以每个点都应该是可见的椭圆。由于我不知道相机的位置,椭圆的形状可能会有所不同,从圆形(从顶部看)到扁平椭圆,具体取决于拍摄硬币的角度。

我的问题是我不知道如何提取硬币并最终在它们上面加上椭圆(我正在寻找它来进行进一步的计算)。

目前,我刚刚进行了第一次尝试,在 OpenCV 中设置阈值,使用 findContours() 获取轮廓线并拟合椭圆。不幸的是,轮廓线很少给出硬币的形状(反射、光线不好……),而且这种方式也不是首选,因为我不希望用户设置任何阈值。

另一个想法是在该图像上使用椭圆的模板匹配方法,但由于我不知道相机的角度也不知道椭圆的大小,我认为这不会很好地工作......

所以我想问是否有人可以告诉我一种适合我的情况的方法。

有没有快速的方法从图像中提取三个硬币?计算应在移动设备上实时进行,并且该方法不应对不同或变化的灯光或背景颜色过于敏感。

如果有人能给我任何关于哪种方法适合我的建议,那就太好了。

image image-processing opencv image-recognition
4个回答
49
投票

这里有一些实现传统方法的 C99 源代码(基于 OpenCV doco):

#include "cv.h"
#include "highgui.h"

#include <stdio.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

//
// We need this to be high enough to get rid of things that are too small too
// have a definite shape.  Otherwise, they will end up as ellipse false positives.
//
#define MIN_AREA 100.00    
//
// One way to tell if an object is an ellipse is to look at the relationship
// of its area to its dimensions.  If its actual occupied area can be estimated
// using the well-known area formula Area = PI*A*B, then it has a good chance of
// being an ellipse.
//
// This value is the maximum permissible error between actual and estimated area.
//
#define MAX_TOL  100.00

int main( int argc, char** argv )
{
    IplImage* src;
    // the first command line parameter must be file name of binary (black-n-white) image
    if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
    {
        IplImage* dst  = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* contour = 0;    
        cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
        //
        // Invert the image such that white is foreground, black is background.
        // Dilate to get rid of noise.
        //
        cvXorS(src, cvScalar(255, 0, 0, 0), src, NULL);
        cvDilate(src, src, NULL, 2);    
        cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
        cvZero( dst );

        for( ; contour != 0; contour = contour->h_next )
        {
            double actual_area = fabs(cvContourArea(contour, CV_WHOLE_SEQ, 0));
            if (actual_area < MIN_AREA)
                continue;

            //
            // FIXME:
            // Assuming the axes of the ellipse are vertical/perpendicular.
            //
            CvRect rect = ((CvContour *)contour)->rect;
            int A = rect.width / 2; 
            int B = rect.height / 2;
            double estimated_area = M_PI * A * B;
            double error = fabs(actual_area - estimated_area);    
            if (error > MAX_TOL)
                continue;    
            printf
            (
                 "center x: %d y: %d A: %d B: %d\n",
                 rect.x + A,
                 rect.y + B,
                 A,
                 B
            );

            CvScalar color = CV_RGB( rand() % 255, rand() % 255, rand() % 255 );
            cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0));
        }

        cvSaveImage("coins.png", dst, 0);
    }
}

鉴于 Carnieri 提供的二值图像,这是输出:

./opencv-contour.out coin-ohtsu.pbm
center x: 291 y: 328 A: 54 B: 42
center x: 286 y: 225 A: 46 B: 32
center x: 471 y: 221 A: 48 B: 33
center x: 140 y: 210 A: 42 B: 28
center x: 419 y: 116 A: 32 B: 19

这是输出图像:

coins

您可以改进的地方:

  • 处理不同的椭圆方向(目前,我假设轴是垂直/水平的)。使用图像时刻这并不难做到。
  • 检查物体的凸度(看看
    cvConvexityDefects

区分硬币和其他物体的最佳方法可能是通过形状。我想不出任何其他低级图像特征(颜色显然已经过时了)。所以,我可以想到两种方法:

传统物体检测

您的第一个任务是将物体(硬币和非硬币)与背景分开。正如卡涅里所建议的,大津的方法在这里很有效。您似乎担心图像是“二分的”,但我认为这不会成为问题。只要有大量可见的桌子,就一定会在直方图中出现一个峰值。只要桌子上有几个视觉上可区分的物体,你就一定会获得第二个高峰。

扩张

您的二值图像几次,以消除阈值处理留下的噪声。这些硬币相对较大,因此它们应该能够在这种形态操作中幸存下来。 使用区域生长将白色像素分组为对象 - 只需迭代连接相邻的前景像素即可。在此操作结束时,您将得到一个不相交对象的列表,并且您将知道每个对象占用哪些像素。

从这些信息中,您将知道对象的宽度和高度(来自上一步)。因此,现在您可以估计围绕该对象的椭圆的大小,然后查看该特定对象与椭圆的匹配程度。仅使用宽度与高度的比例可能会更容易。

或者,您可以使用

时刻

以更精确的方式确定对象的形状。


6
投票
threshold

方法,参数 ThresholdType 等于

THRESH_OTSU

但请注意,Otsu 的方法仅适用于具有双峰直方图的图像(例如,深色背景上具有明亮物体的图像)。

您可能已经看到过这一点,但还有一种方法可以在一组 2D 点(例如,连接的组件)周围

拟合椭圆

编辑

:大津的方法应用于示例图像: 灰度图像:

grayscale image应用大津法的结果:

Otsu image


6
投票

一旦您使用

findContours

找到轮廓(如上面 Misha 的答案),您可以使用

fitEllipse
轻松拟合椭圆,例如

vector<vector<Point> > contours; findContours(img, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0)); RotatedRect rotRecs[contours.size()]; for (int i = 0; i < contours.size(); i++) { rotRecs[i] = fitEllipse(contours[i]); }



-1
投票

https://github.com/opencv/opencv_contrib/blob/4.x/modules/ximgproc/samples/edge_drawing.py

© www.soinside.com 2019 - 2024. All rights reserved.