问题描述
我正在使用JavaCV(OpenCV包装器)进行硬币检测,但是当硬币连接时我遇到了一些问题。如果我试图侵蚀它们以分离这些硬币,它们会松开它们的圆形形状,如果我试图计算每个硬币内部的像素,就会出现问题,因此一些硬币可能被错误计算为更大的硬币。我想要做的是首先重塑它们并使它们像一个圆圈(等于该硬币的半径),然后计算它们内部的像素。
I'm doing a coin detection using JavaCV (OpenCV wrapper) but I have a little problem when the coins are connected. If I try to erode them to separate these coins they loose their circle form and if I try to count pixels inside each coin there can be problems so that some coins can be miscounted as one that bigger. What I want to do is firstly to reshape them and make them like a circle (equal with the radius of that coin) and then count pixels inside them.
这是我的阈值图像:
在这里被侵蚀的图像:
And here is eroded image:
任何建议?或者有没有更好的方法来打破硬币之间的桥梁?
Any suggestions? Or is there any better way to break bridges between coins?
推荐答案
它看起来类似于我最近分离细菌菌落的问题生长在琼脂平板上。
我在阈值图像上执行了距离变换(在您的情况下,您需要将其反转)。
然后找到距离图的峰值(通过计算扩张距离图和距离图之间的差异并找到零值)。
然后,我假设每个峰值为圆的中心(硬币),距离图中的峰值为圆的半径。
It looks similar to a problem I recently had to separate bacterial colonies growing on agar plates.I performed a distance transform on the thresholded image (in your case you will need to invert it).Then found the peaks of the distance map (by calculating the difference between a the dilated distance map and the distance map and finding the zero values).Then, I assumed each peak to be the centre of a circle (coin) and the value of the peak in the distance map to be the radius of the circle.
以下是此管道后图片的结果:
Here is the result of your image after this pipeline:
我是OpenCV和c ++的新手,所以我的代码可能非常混乱,但我做到了:
I am new to OpenCV, and c++ so my code is probably very messy, but I did that:
int main( int argc, char** argv ){
cv::Mat objects, distance,peaks,results;
std::vector<std::vector<cv::Point> > contours;
objects=cv::imread("CUfWj.jpg");
objects.copyTo(results);
cv::cvtColor(objects, objects, CV_BGR2GRAY);
//THIS IS THE LINE TO BLUR THE IMAGE CF COMMENTS OF THIS POST
cv::blur( objects,objects,cv::Size(3,3));
cv::threshold(objects,objects,125,255,cv::THRESH_BINARY_INV);
/*Applies a distance transform to "objects".
* The result is saved in "distance" */
cv::distanceTransform(objects,distance,CV_DIST_L2,CV_DIST_MASK_5);
/* In order to find the local maxima, "distance"
* is subtracted from the result of the dilatation of
* "distance". All the peaks keep the save value */
cv::dilate(distance,peaks,cv::Mat(),cv::Point(-1,-1),3);
cv::dilate(objects,objects,cv::Mat(),cv::Point(-1,-1),3);
/* Now all the peaks should be exactely 0*/
peaks=peaks-distance;
/* And the non-peaks 255*/
cv::threshold(peaks,peaks,0,255,cv::THRESH_BINARY);
peaks.convertTo(peaks,CV_8U);
/* Only the zero values of "peaks" that are non-zero
* in "objects" are the real peaks*/
cv::bitwise_xor(peaks,objects,peaks);
/* The peaks that are distant from less than
* 2 pixels are merged by dilatation */
cv::dilate(peaks,peaks,cv::Mat(),cv::Point(-1,-1),1);
/* In order to map the peaks, findContours() is used.
* The results are stored in "contours" */
cv::findContours(peaks, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
/* The next steps are applied only if, at least,
* one contour exists */
cv::imwrite("CUfWj2.jpg",peaks);
if(contours.size()>0){
/* Defines vectors to store the moments of the peaks, the center
* and the theoritical circles of the object of interest*/
std::vector <cv::Moments> moms(contours.size());
std::vector <cv::Point> centers(contours.size());
std::vector<cv::Vec3f> circles(contours.size());
float rad,x,y;
/* Caculates the moments of each peak and then the center of the peak
* which are approximatively the center of each objects of interest*/
for(unsigned int i=0;i<contours.size();i++) {
moms[i]= cv::moments(contours[i]);
centers[i]= cv::Point(moms[i].m10/moms[i].m00,moms[i].m01/moms[i].m00);
x= (float) (centers[i].x);
y= (float) (centers[i].y);
if(x>0 && y>0){
rad= (float) (distance.at<float>((int)y,(int)x)+1);
circles[i][0]= x;
circles[i][3]= y;
circles[i][2]= rad;
cv::circle(results,centers[i],rad+1,cv::Scalar( 255, 0,0 ), 2, 4, 0 );
}
}
cv::imwrite("CUfWj2.jpg",results);
}
return 1;
}
这篇关于将嘈杂的硬币重塑成圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!