我的工作是基于具有点阵的图像(图1),最终结果如图4所示。我将逐步解释我的工作。Opencv:边缘检测,扩张和质心绘图
图1个原始图像
步骤1:检测的每个对象的边缘,包括我想删除获得更好的性能的点和“环”。而边缘检测的结果如图2所示。我使用Canny边缘检测器,但对于一些浅灰色的点不能很好地工作。 我的第一个问题是如何关闭点的轮廓并尽可能减少其他噪点?
图2的边缘检测
步骤2:扩张每一个对象。我没有找到填补漏洞的好方法,所以我直接扩张它们。如图3所示,空洞似乎过大,其他噪音也会增大。 我的第二个问题是如何填充或扩大孔,以使它们以相同/相似的大小填充圆圈?
图3扩张
步骤3:查找和绘制每个点的质心。如图4所示,由于粗略的图像处理,存在“环”的标记,并且一些点以两个白色像素示出。 想要的结果应该只显示一个点的点和一个白色像素。
图4:质谱中心
这是我对这些3个步骤的代码。任何人都可以帮助改善我的工作吗?
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;
// Global variables
Mat src, edge, dilation;
int dilation_size = 2;
// Function header
void thresh_callback(int, void*);
int main(int argc, char* argv)
{
IplImage* img = cvLoadImage("c:\\dot1.bmp", 0); // dot1.bmp = Fig. 1
// Perform canny edge detection
cvCanny(img, img, 33, 100, 3);
// IplImage to Mat
Mat imgMat(img);
src = img;
namedWindow("Step 1: Edge", CV_WINDOW_AUTOSIZE);
imshow("Step 1: Edge", src);
// Apply the dilation operation
Mat element = getStructuringElement(2, Size(2 * dilation_size + 1, 2 * dilation_size + 1),
Point(dilation_size, dilation_size)); // dilation_type = MORPH_ELLIPSE
dilate(src, dilation, element);
// imwrite("c:\\dot1_dilate.bmp", dilation);
namedWindow("Step 2: Dilation", CV_WINDOW_AUTOSIZE);
imshow("Step 2: Dilation", dilation);
thresh_callback(0, 0);
waitKey(0);
return 0;
}
/* function thresh_callback */
void thresh_callback(int, void*)
{
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
// Find contours
findContours(dilation, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// Get the moments
vector<Moments> mu(contours.size());
for(int i = 0; i < contours.size(); i++) {
mu[i] = moments(contours[i], false);
}
// Get the mass centers
vector<Point2f> mc(contours.size());
for(int i = 0; i < contours.size(); i++) {
mc[i] = Point2f(mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00);
}
// Draw mass centers
Mat drawing = Mat::zeros(dilation.size(), CV_8UC1);
for(int i = 0; i< contours.size(); i++) {
Scalar color = Scalar(255, 255, 255);
line(drawing, mc[i], mc[i], color, 1, 8, 0);
}
namedWindow("Step 3: Mass Centers", CV_WINDOW_AUTOSIZE);
imshow("Step 3: Mass Centers", drawing);
}
你有没有尝试过[这里](http://stackoverflow.com/questions/1716274/fill-the-holes-in-opencv)呢? – William