-2

我将此代码用于分段数字图像我使用轮廓检测​​但是当我尝试隔离每个我有黑色窗口的人时,有人知道问题是什么吗?

在此处输入图像描述

有代码执行的图像/

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

1 回答 1

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;

}
于 2012-04-29T13:16:16.713 回答