这是一些实现传统方法的 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
这是输出图像:

你可以改进的地方:
- 处理不同的椭圆方向(目前,我假设轴是垂直/水平的)。使用图像时刻这并不难。
- 检查对象凸度(看看
cvConvexityDefects
)
区分硬币和其他物体的最佳方法可能是通过形状。我想不出任何其他低级图像特征(颜色显然已经出来了)。所以,我可以想到两种方法:
传统物体检测
您的首要任务是将对象(硬币和非硬币)与背景分开。正如 Carnieri 所建议的,Ohtsu 的方法在这里很有效。您似乎担心图像是二分的,但我认为这不会成为问题。只要有大量可见的桌子,就可以保证在直方图中有一个峰值。只要桌子上有几个视觉上可区分的物体,就可以保证您的第二个高峰。
将二值图像放大几次,以消除阈值化留下的噪声。硬币相对较大,因此它们应该能够在这种形态操作中幸存下来。
使用区域增长将白色像素分组为对象 - 只需迭代连接相邻的前景像素。在此操作结束时,您将获得一个不相交对象的列表,并且您将知道每个对象占用哪些像素。
从这些信息中,您将知道对象的宽度和高度(来自上一步)。因此,现在您可以估计围绕对象的椭圆的大小,然后查看该特定对象与椭圆的匹配程度。仅使用宽高比可能更容易。
或者,您可以使用矩以更精确的方式确定对象的形状。