我目前正在学校的项目中使用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 ;
}
答案 0 :(得分:2)
你正在对img_rgb进行阈值处理,那时候是空的。
另外,为什么从灰色到rgb的转换呢?
(顺便说一下,如果你在rgb图像上循环,mat.at<uchar>(y,x)
错了,应该是mat.at<Vec3b>(y,x)
)
我会说,跳过gray2rgb转换,然后阈值加载的灰度图像:
cv::Mat im = cv::imread("IMG.jpg",0);
if (im.empty())
return -1;
cv::Mat thresh;
cv::threshold(im, thresh, 155, 255, CV_THRESH_BINARY);
然后使用该垫进行进一步处理:
for (int i = 1; i < thresh.rows-1; i++)
{
for (int j = 1; j < thresh.cols-1; j++)
{
uchar p1 = thresh.at<uchar>(i, j);
if(255 == p1)
{
uchar p2 = thresh.at<uchar>(i-1, j);
uchar p3 = thresh.at<uchar>(i-1, j+1);
// ...