Quantcast

WHat is wrong with this code??? Plzz help..URGENT...

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

WHat is wrong with this code??? Plzz help..URGENT...

debayan
#include <vector>
#include <cv.h>
#include <highgui.h>
#include <iostream.h>

using namespace std;

void main(int argc, char *argv[]){

  IplImage* image=0;  /* pointer to an image */
  IplImage* newImg = 0;
  IplImage* grayImg = 0;
  IplImage* contourImg = 0;
  double area=0;
 
    newImg = cvLoadImage("test.jpg", 1); // 1 for color
  if(newImg != 0) {

          //parameters for the contour detection
        CvMemStorage * storage = cvCreateMemStorage(0);
        CvSeq * contour = 0;
        int mode = CV_RETR_CCOMP; //detect both outside and inside contour
       
        //create a single channel 1 byte image (i.e. gray-level image)
        grayImg = cvCreateImage( cvSize(newImg->width, newImg->height), IPL_DEPTH_8U, 1);
        //convert original color image (3 channel rgb color image) to gray-level image
        cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
        cvShowImage( "src", newImg );
        //make a copy of the original image to draw the detected contour
        contourImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 3);
        contourImg=cvCloneImage( newImg );
        //find the contour
        cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode,CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

        //draw the contour
        cvDrawContours(contourImg, contour, CV_RGB(0, 0, 255), CV_RGB(255, 255, 255), 2, 2,8);
       
        // Find area of contour
        double result=0;
        result = fabs(cvContourArea(contour, CV_WHOLE_SEQ));
        //printf("area = %f\n", area);
        cout << "Area: " << result << "\n";

   
        ////////rest area//////////////////

 IplImage* img_in = cvLoadImage("test.jpg",1);


IplImage* img_working = cvCreateImage(cvGetSize(img_in), 8, 1);
cvCvtColor(img_in, img_working, CV_BGR2GRAY);

CvSeq* seq;

vector<CvRect> boxes;

CvMemStorage* storage1 = cvCreateMemStorage(0);
cvClearMemStorage(storage1);

cvFindContours(img_working, storage1, &seq, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(0,0));

CvRect boundbox ;

for(; seq; seq = seq->h_next) {
 boundbox = cvBoundingRect(seq);
 boxes.push_back(boundbox);
}

for (int ii=0; ii<boxes.size(); ii++) {
cout << "x-origin" << boxes[ii].x << endl;
cout << "y-origin" << boxes[ii].y << endl;
cout << "width" << boxes[ii].width << endl;
cout << "height" << boxes[ii].height << endl;
}

double boundaryarea = (boxes[ii].height*boxes[ii].width);

double restarea = abs(boundaryarea - result);

cout << " Rest Area: " << restarea << "\n";

/////////boundary//////////////////

CvPoint* pt1,pt2,pt3,pt4;

pt1.x= boxes[ii].x;
pt1.y= boxes[ii].y;
pt2.x= boxes[ii].x+boxes[ii].width;
pt2.y= boxes[ii].y;
pt3.x= boxes[ii].x;
pt3.y=boxes[ii].y+boxes[ii].height;
pt4.x=boxes[ii].x+boxes[ii].width;
pt4.y=boxes[ii].y+boxes[ii].height;

cvLine( img_working, pt1, pt2, CV_RGB( 255, 0, 0 ), 1,8,0 );
cvLine( img_working, pt2, pt3, CV_RGB( 255, 0, 0 ), 1,8,0 );
cvLine( img_working, pt3, pt4, CV_RGB( 255, 0, 0 ), 1,8,0 );
cvLine( img_working, pt4, pt1, CV_RGB( 255, 0, 0 ), 1,8,0 );
       
cvShowImage("display",img_working);
cvWaitKey(0);


}
}

Loading...