-
1 pièce(s) jointe(s)
Segmentation d'image
bonjour tout le monde j'utilise ce code là pour la segmentation d'une image en se basant sur la détection de contour mais quand je veux isoler chaque contour détecté a l'aide d'une image ROI j'ai eu une fenêtre noir ,quelqu'un peut m'aider a trouver une solution?!!
http://www.developpez.net/forums/att...1&d=1335871825
voilà le code:
IplImage *img_cv = cvLoadImage("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE);
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);
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 );