1

我目前正在学校项目中使用 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 ;

}

4

1 回答 1

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);

             // ...
于 2013-05-10T08:54:42.960 回答