OpenCV(C):从轮廓计算弯矩

编程入门 行业动态 更新时间:2024-10-23 21:33:51
本文介绍了OpenCV(C):从轮廓计算弯矩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的代码:

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

基本上,代码的前半部分和代码的最后几行直接取自"Learning OpenCV"书,因为这已经进行了轮廓查找,这就是我想要的(不是窃,我正在尝试一些方法),并且代码的中间部分(如记录所示)专用于计算Hu Moments.我想尝试一下,当具有轮廓的特定图像的方向不同或图像的大小发生变化时,时刻值是否发生变化.

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错误:cvMoments中的空指针(),文件/build/buildd/opencv-2.1.0/src/cv/cvmoments.cpp,第343行 抛出'cv :: Exception'实例后调用终止 what():/build/buildd/opencv-2.1.0/src/cv/cvmoments.cpp:343:错误:函数cvMoments中的(-27)

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

中止

是否存在有关如何使用CvMoments数据结构的问题,或者该问题与CvContourMoments()函数有关?我当前使用的OpenCV版本是2.1.0.

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.

推荐答案

我认为您应该定义struct的实例而不是指针.

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

尝试一下:

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()相同.我只能在 www.opencv/opencvdoc 上找到cvMoments() .并且变量"c"应该是指向IplImage或CvArr的指针.

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

我是新来的.希望能提供帮助.

I'm New. Wish to help.

更多推荐

OpenCV(C):从轮廓计算弯矩

本文发布于:2023-05-26 20:21:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弯矩   轮廓   OpenCV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!