假设我有一个如上图所示的平面图的图像。是否有一种简单的处理方法将此图像转换为由坐标[(x1,y1),(x2,y2)],[(x2,y2),(x3,y3)],...表示的一组线, [(XN-1,炔1),(XN,YN)]
答案 0 :(得分:0)
我想你想找到图像中所有线条的坐标。通过遵循c ++中的简单步骤很容易找到共同符号:
void thinningIteration(Mat& im, int iter)
{
Mat marker = Mat::zeros(im.size(), CV_8UC1);
for (int i = 1; i < im.rows-1; i++)
{
for (int j = 1; j < im.cols-1; j++)
{
uchar p2 = im.at<uchar>(i-1, j);
uchar p3 = im.at<uchar>(i-1, j+1);
uchar p4 = im.at<uchar>(i, j+1);
uchar p5 = im.at<uchar>(i+1, j+1);
uchar p6 = im.at<uchar>(i+1, j);
uchar p7 = im.at<uchar>(i+1, j-1);
uchar p8 = im.at<uchar>(i, j-1);
uchar p9 = im.at<uchar>(i-1, j-1);
int A = (p2 == 0 && p3 == 1) + (p3 == 0 && p4 == 1) +
(p4 == 0 && p5 == 1) + (p5 == 0 && p6 == 1) +
(p6 == 0 && p7 == 1) + (p7 == 0 && p8 == 1) +
(p8 == 0 && p9 == 1) + (p9 == 0 && p2 == 1);
int B = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);
int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);
if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0)
marker.at<uchar>(i,j) = 1;
}
}
im &= ~marker;
}
void thinning(Mat& im)
{
im /= 255;
Mat prev = Mat::zeros(im.size(), CV_8UC1);
Mat diff;
do
{
thinningIteration(im, 0);
thinningIteration(im, 1);
absdiff(im, prev, diff);
im.copyTo(prev);
}
while (countNonZero(diff) > 0);
im *= 255;
}
void main()
{
Mat mSource_Bgr,mSource_Gray,mThreshold,mThinning;
mSource_Bgr= imread(FileName_S.c_str(),IMREAD_COLOR);
mSource_Gray= imread(FileName_S.c_str(),0);
threshold(mSource_Gray,mThreshold,120,255,THRESH_BINARY_INV);
mThinning= mThreshold.clone();
thinning(mThinning);
imshow("mThinning",mThinning);
vector<Point2i> locations; // output, locations of non-zero pixels
findNonZero(mThinning, locations);
for (int i = 0; i < locations.size(); i++)
{
circle(mSource_Bgr,locations[i],2,Scalar(0,255,0),1);
}
imshow("mResult",mSource_Bgr);
//To Find the Lines from the image
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours( mThinning.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 2, true );
}
for (int i = 0; i < contours_poly.size(); i++)
{
drawContours(mSource_Bgr,contours_poly,i,Scalar(255,0,0),2);
}
imshow("mResult",mSource_Bgr);
}