2012-04-29 113 views
-2

我使用此代碼segmentaion數字圖像我使用輪廓檢測​​,但當我試圖隔離每一個我有黑色的窗口,有人有任何想法是什麼問題是?圖像分割與邊緣檢測

enter image description here

有執行代碼的圖像/

IplImage *img_cv = cvLoadImage("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE); 

if (!img_cv) 
{ 
    printf("Failed to load image.\n"); 
    exit(1); 
} 
//cvSetImageROI(img_cv,cvRect(8,10,70,35)); 
IplImage *img_pl = cvCreateImage(cvGetSize(img_cv),img_cv->depth,img_cv->nChannels); 

cvCopy(img_cv,img_pl, NULL); 

//Smooth image 
cvSmooth(img_pl, img_pl, CV_GAUSSIAN, 3, 0, 0, 0);   

// threshold image 
cvThreshold(img_pl, img_pl, 150, 255, CV_THRESH_BINARY_INV); 

//Morfologic filters 
//Ouverture :érosion suivie d'une dilatation 
cvErode(img_pl, img_pl, NULL,1); 
cvDilate(img_pl, img_pl, NULL,1); 



//Init variables for countours 

    CvSeq* contour2; 



contour2 = 0; 



IplImage* img_contornos; 
CvSeq* contourLow; 
contourLow = 0; 

//Duplicate image for countour 
img_contornos=cvCloneImage(img_pl); 

//Create storage needed for contour detection 
CvMemStorage* storage2 = cvCreateMemStorage(0); 

//Search countours in preprocesed image 
cvFindContours(img_contornos, storage2, &contour2, sizeof(CvContour), 
          CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 

//Optimize contours, reduce points 
contourLow=cvApproxPoly(contour2, sizeof(CvContour), storage2,CV_POLY_APPROX_DP,0,1); 

//For each contour found 
for(; contourLow != 0; contourLow = contourLow->h_next){ 

//We can draw the contour of object 
cvDrawContours(img_contornos, contourLow, CV_RGB(0,255,0), CV_RGB(0,255,0), -1, 0, 8, cvPoint(0,0)); 

printf(" %d elements:\n", contourLow -> total); 
for(int i = 0; i < contourLow -> total; ++i) 
{ 
     CvPoint* pt = CV_GET_SEQ_ELEM(CvPoint, contourLow, i); 
     // printf(" %d, %d", pt -> x, pt -> y); 

     CvRect rect; 
     CvPoint pt1, pt2; 
     rect=cvBoundingRect(contourLow, NULL); 
     pt1.x = rect.x; 
     pt2.x = (rect.x+rect.width); 
     pt1.y = rect.y; 
     pt2.y = (rect.y+rect.height); 
     cvRectangle(img_cv, pt1,pt2, CV_RGB(0,255,0), 1, 8, 0); 
     IplImage *img_rect = cvCreateImage(cvGetSize(img_contornos),img_contornos->depth,img_contornos->nChannels); 

     cvSetImageROI(img_contornos,cvRect(pt1.x,pt1.y,50,80)); 
      //cvCopy(img_contornos, img_rect, NULL); 
      cvNamedWindow("segment",CV_WINDOW_AUTOSIZE); 
       cvShowImage("segment",img_rect); 
       cvResetImageROI(img_contornos); 

}} 


cvNamedWindow("pla",CV_WINDOW_AUTOSIZE); 
cvShowImage("pla",img_cv); 
cvResetImageROI(img_cv); 
cvReleaseImage(&img_cv); 
+0

的Merci德海報EN英語uniquement,_y compris樂titre_。 – Mat 2012-04-29 13:26:53

+0

好吧謝謝:) – Butterflay 2012-04-29 13:35:28

回答

0
#include <cv.h> 

#include <highgui.h> 

int main() 
{ 
    IplImage* img_cv = cvLoadImage(your file name); 

    if (!img_cv) 
    { 
     printf("Failed to load image.\n"); 

     exit(1); 
    } 
    IplImage *img_pl = cvCreateImage(cvGetSize(img_cv),8, 3); 

    cvCopy(img_cv,img_pl, NULL); 

    //Smooth image 
    cvSmooth(img_pl, img_pl, CV_GAUSSIAN, 3, 0, 0, 0);   

    // threshold image 
    IplImage *img_threshold_pl = cvCreateImage(cvGetSize(img_cv),8, 1); 

    cvCvtColor(img_pl, img_threshold_pl, CV_BGR2GRAY); 

    cvThreshold(img_threshold_pl, img_threshold_pl, 150, 255, CV_THRESH_BINARY_INV); 

    cvErode(img_threshold_pl, img_threshold_pl, NULL,1); 

    cvDilate(img_threshold_pl, img_threshold_pl, NULL,1); 


CvSeq* contour2; 

contour2 = 0; 

IplImage* img_contornos = cvCreateImage(cvGetSize(img_cv), 8, 1); 

CvSeq* contourLow; 

contourLow = 0; 

img_contornos=cvCloneImage(img_pl); 

CvMemStorage* storage2 = cvCreateMemStorage(0); 

cvFindContours(img_threshold_pl, storage2, &contour2, sizeof(CvContour), 
          CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 

contourLow=cvApproxPoly(contour2, sizeof(CvContour), storage2,CV_POLY_APPROX_DP,0,1); 

for(; contourLow != 0; contourLow = contourLow->h_next){ 


cvDrawContours(img_contornos, contourLow, CV_RGB(0,255,0), CV_RGB(0,255,0), -1, 0, 8, cvPoint(0,0)); 

printf(" %d elements:\n", contourLow -> total); 

for(int i = 0; i < contourLow -> total; ++i) 
{ 
     CvPoint* pt = CV_GET_SEQ_ELEM(CvPoint, contourLow, i); 

     // printf(" %d, %d", pt -> x, pt -> y); 

     CvRect rect; 

     CvPoint pt1, pt2; 

     rect=cvBoundingRect(contourLow, NULL); 

     pt1.x = rect.x; 

     pt2.x = (rect.x+rect.width); 

     pt1.y = rect.y; 

     pt2.y = (rect.y+rect.height); 

     cvRectangle(img_cv, pt1,pt2, CV_RGB(0,255,0), 1, 8, 0); 

     cvSetImageROI(img_contornos,cvRect(pt1.x,pt1.y,50,80)); 

IplImage *img_rect = cvCreateImage(cvGetSize(img_contornos),img_contornos->depth,img_contornos->nChannels); 

     cvCopy(img_contornos, img_rect, NULL); 

      cvNamedWindow("segment",CV_WINDOW_AUTOSIZE); 

       cvShowImage("segment",img_rect); 

       cvResetImageROI(img_contornos); 

} 
} 


cvNamedWindow("pla",CV_WINDOW_AUTOSIZE); 

cvShowImage("pla",img_cv); 

cvResetImageROI(img_cv); 

cvReleaseImage(&img_cv); 

cvWaitKey(0); 

return 0; 

} 
+0

當我添加cvcopy它crach! – Butterflay 2012-04-29 13:24:56

+0

你有沒有爲img_contornos創建一個IplImage? – Mzk 2012-04-29 13:29:16

+0

還有所有的代碼 – Butterflay 2012-04-29 13:33:24