我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想在两点之间有一个来自骨架的点向量。
此代码在添加一些点后结束。我没有找到解决方案。
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/legacy/compat.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <cmath>
#include <list>
using namespace cv;
using namespace std;
//this method to find the 8-neighbors of a point from image
vector<Point> search8Neighbor(cv::Mat mat,Point startPoint)
{
vector<Point> segment;
Point p;
//uchar p1 = mat.at<uchar>(startPoint.x, startPoint.y);
uchar p2 = mat.at<uchar>(startPoint.x-1, startPoint.y);
uchar p3 = mat.at<uchar>(startPoint.x-1, startPoint.y+1);
uchar p4 = mat.at<uchar>(startPoint.x, startPoint.y+1);
uchar p5 = mat.at<uchar>(startPoint.x+1, startPoint.y+1);
uchar p6 = mat.at<uchar>(startPoint.x+1, startPoint.y);
uchar p7 = mat.at<uchar>(startPoint.x+1, startPoint.y-1);
uchar p8 = mat.at<uchar>(startPoint.x, startPoint.y-1);
uchar p9 = mat.at<uchar>(startPoint.x-1, startPoint.y-1);
//if(p1==255) segment.push_back(startPoint.x, startPoint.y);
if (p2 == 255) {
p.x=startPoint.x-1;
p.y=startPoint.y;
segment.push_back(p);
}
if(p3==255) {
p.x=startPoint.x-1;
p.y=startPoint.y+1;
segment.push_back(p);
}
if(p4==255) {
p.x=startPoint.x;
p.y=startPoint.y+1;
segment.push_back(p);
}
if(p5==255) {
p.x=startPoint.x+1;
p.y=startPoint.y+1;
segment.push_back(p);
}
if(p6==255) {
p.x=startPoint.x+1;
p.y=startPoint.y;
segment.push_back(p);
}
if(p7==255) {
p.x=startPoint.x+1;
p.y=startPoint.y-1;
segment.push_back(p);
}
if(p8==255){
p.x=startPoint.x;
p.y=startPoint.y-1;
segment.push_back(p);
}
if(p9==255) {
p.x=startPoint.x-1;
p.y=startPoint.y-1;
segment.push_back(p);
}
return segment ;
}
//this method return a vector of points from a skeleton that contains all the points
// between a start point "peak" and an end point
//this method use the idea of Breadth first search
vector<Point> traceLine(Mat img , Point peak)
{
vector<Point> vect1;
vector<Point> vect2;
img.at<uchar>(peak.x,peak.y)=0;//
vect1.push_back(peak);//add peak to vect1
while(vect1.size() != 0)
{
Point p=vect1[0];
vect1.erase(vect1.begin());
vect2.push_back(p);
vector<Point> vectN;
vectN=search8Neighbor(img,p);
vector<Point>::iterator it;
it = vect1.begin();
//cout<<vectN.size()<<endl;
for(int i=0;i<vectN.size();i++)
{
img.at<uchar>(vectN[i].x,vectN[i].y)=0;
vect1.insert(it,vectN[i]);
}
}
return vect2;
}
int main( int argc, char** argv )
{
cv::Mat im = cv::imread("aa.jpg",0);
if (im.empty())
return -1;
cv::Mat img;
cv::threshold(im, img, 155, 255, CV_THRESH_BINARY);
vector<Point> vect1;
Point p;
p.x=20;
p.y=30;
if(mat.at<uchar>(p.x-1, p.y)==255)
vect1=traceLine(img,p);
imshow("image",im);
cv::waitKey();
return 0 ;
}