0

Here's the code I have:

#include "highgui.h"
#include "cv.h"  
#include <stdio.h>

// this code finds contours

int main (int argc, char* argv[]) {
  cvNamedWindow( argv[0], 1 );

  IplImage* img_8uc1 = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
  IplImage* img_edge = cvCreateImage( cvGetSize(img_8uc1), 8, 1);
  IplImage* img_8uc3 = cvCreateImage( cvGetSize(img_8uc1), 8, 3);

  cvThreshold( img_8uc1, img_edge, 128, 255, CV_THRESH_BINARY );

  CvMemStorage* storage = cvCreateMemStorage();
  CvSeq* first_contour = NULL;
  int Nc = cvFindContours(img_edge, storage, &first_contour,
              sizeof(CvContour), CV_RETR_LIST );

  int n = 0;
  printf("Total Contours Detected: %d\n", Nc);
  CvSeq* c=first_contour;
  for( c; c!=NULL; c=c->h_next ) {
    cvCvtColor( img_8uc1, img_8uc3, CV_GRAY2BGR );
    cvDrawContours(img_8uc3, c, 
           CV_RGB(250,0,0), //red
           CV_RGB(0,0,250), //blue
           0,2,8);
    printf("Contour #%d\n", n);
    cvShowImage( argv[0], img_8uc3 );
    printf(" %d elements:\n", c->total );
    for( int i=0; i<c->total; ++i) {
      CvPoint* p = CV_GET_SEQ_ELEM(CvPoint, c, i);
      printf("     (%d,%d)\n", p->x, p->y); 
    }
    cvWaitKey(0);
    n++;
  }


  // moments
  CvMoments* Moments;
  CvHuMoments *HuMoments;
  // calculate moments; calculate humoments
  cvContourMoments(c, Moments);
  cvGetHuMoments(Moments, HuMoments);

  //print the hu moments
  printf("Hu Moment hu1: %.12f", HuMoments->hu1);
  printf("Hu Moment hu2: %.12f", HuMoments->hu2);
  printf("Hu Moment hu3: %.12f", HuMoments->hu3);
  printf("Hu Moment hu4: %.12f", HuMoments->hu4);
  printf("Hu Moment hu5: %.12f", HuMoments->hu5);
  printf("Hu Moment hu6: %.12f", HuMoments->hu6);
  printf("Hu Moment hu7: %.12f", HuMoments->hu7);


  printf("Finished all contours.\n");
  cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);
  cvShowImage(argv[0], img_8uc3);
  cvWaitKey(0);

  cvDestroyWindow(argv[0]);
  cvReleaseImage(&img_8uc1);
  cvReleaseImage(&img_8uc3);
  cvReleaseImage(&img_edge);

  return 0;
}

Basically, the first half of the code and the last lines of the code are taken directly from the "Learning OpenCV" book, since that does the contour finding already and it's what I want (not plagiarizing, I'm trying something out), and the middle section of the code (as documented) is dedicated to computing Hu Moments. I want to try to see if any moment values vary or not when a particular image with a contour, is oriented differently or when the size of the image changes.

I thought I wrote this the right way, at least the algorithm seemed right to me, but I keep getting this error message:

OpenCV Error: Null pointer () in cvMoments, file /build/buildd/opencv-2.1.0/src/cv/cvmoments.cpp, line 343 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.1.0/src/cv/cvmoments.cpp:343: error: (-27) in function cvMoments

Aborted

Is there an issue of how I used the CvMoments data structure, or did the problem had to do with CvContourMoments() function? The version of OpenCV I used currently is 2.1.0.

I appreciate the help! i can't find solutions like this anywhere on the internet and I am wondering if anyone has a similar problem about this with moment calculations from contours.

3 Answers 3

1

I think you should define a instant of struct instead of a pointer.

TRY this:

CvMoments Moments;
CvHuMoments HuMoments;
// calculate moments; calculate humoments
cvContourMoments(c, &Moments);
cvGetHuMoments(&Moments, &HuMoments);
//print the hu moments
printf("Hu Moment hu1: %.12f", HuMoments.hu1);
printf("Hu Moment hu2: %.12f", HuMoments.hu2);
printf("Hu Moment hu3: %.12f", HuMoments.hu3);
printf("Hu Moment hu4: %.12f", HuMoments.hu4);
printf("Hu Moment hu5: %.12f", HuMoments.hu5);
printf("Hu Moment hu6: %.12f", HuMoments.hu6);
printf("Hu Moment hu7: %.12f", HuMoments.hu7);

By the way, cvContourMoments() seems to be the same as cvMoments(). I can only find cvMoments() at http://www.opencv.org.cn/opencvdoc. And variable "c" should be a pointer to IplImage or CvArr.

I'm New. Wish to help.

2
  • Thanks for the reply! Though I'm think the difference between cvCOntourMoments() and cvMOments is the arguments it takes. The first function takes the actual contour as an argument, whereas the second one takes the image. I just used your suggestion above, and everything else is the same except I tried a different approach: instead of calculating the moment from contour c I called the cvMoments() function with the image img_8uc3 because the contours are drawn on that image. But this time I get an error (I'll type the error on the next post). Any other suggestions?
    – Vincent Lu
    Dec 30, 2011 at 23:20
  • OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 2476 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp:2476: error: (-206) Unrecognized or unsupported array type in function cvGetMat Aborted
    – Vincent Lu
    Dec 30, 2011 at 23:21
0

I think you missed to specify what image will be tested. I'm still a newbie also but you can try the Program that I've tested to get the contours (it works :) ) and from there you can add the function to get the moments of the contours. I hope this helps you.

0

you forget to reset the c;

Try adding

c=first_contour;

before you use it in

cvContourMoments(c, &Moments);; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.