OpenCV(C):根据轮廓计算力矩

发布于 2024-12-23 10:39:59 字数 2739 浏览 1 评论 0原文

这是我的代码:

#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;
}

基本上,代码的前半部分和代码的最后几行直接取自“学习 OpenCV”书,因为它已经进行了轮廓查找,这就是我想要的(不是抄袭,我'我正在尝试一些东西),代码的中间部分(如文档所示)专门用于计算 Hu Moments。我想尝试看看当具有轮廓的特定图像的方向不同或图像的大小发生变化时,任何矩值是否会发生变化。

我认为我以正确的方式写了这个,至少该算法对我来说似乎是正确的,但我不断收到此错误消息:

OpenCV Error: Null point () in cvMoments, file /build/buildd/opencv-2.1.0/src/简历/cvmoments.cpp,第 343 行 抛出“cv::Exception”实例后调用终止 What(): /build/buildd/opencv-2.1.0/src/cv/cvmoments.cpp:343: error: (-27) in function cvMoments

Aborted

是否存在我如何使用 CvMoments 数据结构的问题,或者做了什么问题与 CvContourMoments() 函数有关吗?我目前使用的OpenCV版本是2.1.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

◇流星雨 2024-12-30 10:39:59

我认为你应该定义一个结构实例而不是指针。

试试这个:

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

顺便说一句,cvContourMoments() 似乎与 cvMoments() 相同。我只能在 http://www.opencv.org.cn/opencvdoc 找到 cvMoments() 。变量“c”应该是指向 IplImage 或 CvArr 的指针。

我是新人。希望提供帮助。

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.

一影成城 2024-12-30 10:39:59

我认为您错过了指定要测试的图像。我仍然是新手,但您可以尝试我已经测试过的 程序轮廓(它有效:)),然后您可以添加函数来获取轮廓的矩。我希望这对你有帮助。

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.

帅冕 2024-12-30 10:39:59

你忘记重置c;

添加

c=first_contour;

尝试在使用它之前

cvContourMoments(c, &Moments);; 

you forget to reset the c;

Try adding

c=first_contour;

before you use it in

cvContourMoments(c, &Moments);; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文