OpenCV Visual-C++ cv分割问题
我尝试了多种方法来打开图像并分割通道。我只想使用 3 Matrix。我不知道出了什么问题。 这是我的代码:
IplImage* img = cvLoadImage( "C:\\foo.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
std::cout << "Hight: " << img->height << " Width: " << img->width;
CvMat* imgR= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgG= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgB= cvCreateMat(img->width,img->height,CV_8UC1);
cvSplit(&img, imgB, imgG, imgR, NULL);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
问题是行 cvSplit(&img, imgB, imgG, imgR, NULL);
。程序总是崩溃,不知道为什么。
编辑1:异常:
错误 -(206) 无法识别或不受支持的数组类型
Edit2:如果我使用 img
而不是 &img
我得到此异常:
An error occurred.
..\..\..\..\ocv\opencv\src\cxcore\cxconvert.cpp:877: error: (-215) dvec[j].size(
) == src.size() && dvec[j].depth() == src.depth() && dvec[j].channels() == 1 &&
i < src.channels()
解决方案: 我不知道 cv::Mat、cvMat 和 IplImage 之间的区别。 这是解决方案:
IplImage *r = cvCreateImage(cvGetSize(img), img->depth, 1);
IplImage *g = cvCreateImage(cvGetSize(img), img->depth, 1);
IplImage *b = cvCreateImage(cvGetSize(img), img->depth, 1);
cvSplit(img, b, g, r, NULL);
I tried several methods to open an image and split the channels. I just want 3 Matrix to work with. I don't know whats wrong.
Here my code:
IplImage* img = cvLoadImage( "C:\\foo.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
std::cout << "Hight: " << img->height << " Width: " << img->width;
CvMat* imgR= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgG= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgB= cvCreateMat(img->width,img->height,CV_8UC1);
cvSplit(&img, imgB, imgG, imgR, NULL);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
The problem is the line cvSplit(&img, imgB, imgG, imgR, NULL);
. The program always crash and I don't know why.
Edit1: Exception:
error -(206) Unrecognized or unsupported array type
Edit2: If i use img
instead of &img
I get this exception:
An error occurred.
..\..\..\..\ocv\opencv\src\cxcore\cxconvert.cpp:877: error: (-215) dvec[j].size(
) == src.size() && dvec[j].depth() == src.depth() && dvec[j].channels() == 1 &&
i < src.channels()
Solution:
I was a not aware of the differnce between cv::Mat, cvMat and IplImage.
This is the solution:
IplImage *r = cvCreateImage(cvGetSize(img), img->depth, 1);
IplImage *g = cvCreateImage(cvGetSize(img), img->depth, 1);
IplImage *b = cvCreateImage(cvGetSize(img), img->depth, 1);
cvSplit(img, b, g, r, NULL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 cvloadImage 返回指向 iplImage 的指针,那么您不需要 '&'在 img 上它已经是一个指针
cvSplit 采用 cvMat* a iplImage* 不一样,您需要将其转换为 cvmat
请参阅http://opencv.willowgarage.com/documentation/cpp/c++ _cheatsheet.html
if cvloadImage is returning a pointer to an iplImage then you don't need the '&' on img it's already a pointer
cvSplit takes cvMat* a iplImage* isn't the same, you need to convert it to a cvmat
see http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html