我目前正在学校项目中使用 opencv 和 c++。我的问题是如何在骨架图像中提取连接点。我曾尝试开发此代码,但它不起作用,我也没有发现错误。提前致谢 。
int main( int argc, char** argv )
{
cv::Mat im = cv::imread("IMG.jpg",0);
if (im.empty())
return -1;
cv::Mat img_rgb(im.size(), CV_8UC3);
cv::threshold(img_rgb, img_rgb, 155, 255, CV_THRESH_BINARY);
cv::cvtColor(im, img_rgb, CV_GRAY2RGB);
int s=0;
for (int i = 1; i < img_rgb.rows-1; i++)
{
for (int j = 1; j < img_rgb.cols-1; j++)
{
if(255 == img_rgb.at<uchar>(i,j))
{
uchar p1 = img_rgb.at<uchar>(i, j);
uchar p2 = img_rgb.at<uchar>(i-1, j);
uchar p3 = img_rgb.at<uchar>(i-1, j+1);
uchar p4 = img_rgb.at<uchar>(i, j+1);
uchar p5 = img_rgb.at<uchar>(i+1, j+1);
uchar p6 = img_rgb.at<uchar>(i+1, j);
uchar p7 = img_rgb.at<uchar>(i+1, j-1);
uchar p8 = img_rgb.at<uchar>(i, j-1);
uchar p9 = img_rgb.at<uchar>(i-1, j-1);
// s = abs(p9-p8)+abs(p8-p7)+abs(p7-p6)+abs(p6-p5)+abs(p5-p4)+abs(p4-p3)+abs(p3-p2)+abs(p2-p9);
// if (s == 1530)
int same=(p9==p1);
same+=(p8==p1);
same+=(p7==p1);
same+=(p6==p1);
same+=(p5==p1);
same+=(p4==p1);
same+=(p3==p1);
same+=(p2==p1);
if (same==2)
{
// cout<<i<<" "<<j<<" " <<same<<endl;
circle(img_rgb,Point(i,j),1,Scalar(0, 255, 0),1);
}
}
}
}
cv::imshow("a",img_rgb);
cv::waitKey();
return 0 ;
}