2013-05-10 51 views
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 ; 

}

回答

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

      // ... 
+0

它的作品就像一個魅力。感謝很多。 – user138957 2013-05-10 09:05:29

+0

嘿,快樂;) – berak 2013-05-10 09:06:44