我有一个像下面这样的图像:
有了这个源代码,我已经成功提取了图像中每个点的中心坐标。
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <vector>
int main(int argc, char **argv)
{
cv::Mat matSrc,matTmp,matDst;
cv::Mat matV,matROI;
std::vector<cv::Mat> vectorHSV;
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
matSrc = cv::imread(argv[1],1);
// matSrc = cv::imread("123.jpg",1);
// cv::imshow("Source", matSrc);
cv::cvtColor(matSrc,matTmp,CV_RGB2HSV);
cv::split(matTmp,vectorHSV);
matV = vectorHSV[2];
matV.copyTo(matTmp);
cv::normalize(matTmp,matTmp,0,255,CV_MINMAX,CV_8UC1);
cv::medianBlur(matTmp,matTmp,5);
cv::adaptiveThreshold(matTmp,matTmp,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY_INV,125,7);
cv::equalizeHist(matTmp,matTmp);
// cv::erode(matTmp,matTmp,cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3)));
cv::morphologyEx(matTmp,matTmp,cv::MORPH_CLOSE,cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3)));
cv::Canny(matTmp,matTmp,50,150,3);
cv::Rect ROI(0,0,matTmp.cols,matTmp.rows);
matROI = matTmp(ROI);
cv::findContours(matROI,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_TC89_KCOS,cv::Point(0,0));
std::vector<cv::Moments> muDots(contours.size());
std::vector<cv::Point> mcDots(contours.size());
for(size_t c=0;c < contours.size();c++)
{
muDots[c] = cv::moments(contours[c],false);
mcDots[c] = cv::Point(static_cast<int>(muDots[c].m10/muDots[c].m00) , static_cast<int>(muDots[c].m01/muDots[c].m00));
}
for(int allDots=0;allDots < mcDots.size();allDots++)
{
std::cout << allDots << ": " << mcDots[allDots].x << "," << mcDots[allDots].y << std::endl;
}
matTmp.copyTo(matDst);
imshow("ROI",matROI);
// imshow("Result", matDst);
cv::waitKey(0);
return 0;
}
我想问的是如何处理轮廓是否存在,简而言之,我想在这个算法中做这样的事情:
for(x=0;x < Image.cols;x++)
{
for(y=0;y < Image.rows;y++)
{
if(Contour coordinates at X,Y = Exist)
{
vectorBraille.push_back = 1;
}
else
{
vectorBraille.push_back = 0;
}
}
}
拜托,我真的需要这个问题的建议,我有点卡在这里。
任何帮助将不胜感激。谢谢