如果这个问题太基础了,对不起,我还不熟悉OpenCV。
我想对一张RGB图像应用直方图均衡化。直方图均衡仅适用于单通道图像,因此我认为我必须将图像分成三个不同的通道,对每个通道应用直方图均衡,然后将它们合并在一起以形成输出均衡化图像。
所以我按照上述方法编写了程序,我真的相信它应该能工作:
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char * argv[])
{
IplImage* img = cvLoadImage("/Users/Documents/red.jpg"); //Load image file
cvNamedWindow("Trans-in", CV_WINDOW_AUTOSIZE ); //Create input window
cvNamedWindow("Trans-out", CV_WINDOW_AUTOSIZE ); //Create output window
cvShowImage("Trans-in", img ); //Show input image in input window
//Create space for outputs rgb and its separate channels, r, g and b
IplImage* img0 = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3); //rgb
IplImage* r = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); //r
IplImage* g = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); //g
IplImage* b = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); //b
//cvZero(r);
//cvZero(g);
//cvZero(b);
cvSplit(img, b, g, r, NULL); //OpenCV likes it in BGR format
cvEqualizeHist( img, r ); //equalise r
cvEqualizeHist( img, g ); //equalise g
cvEqualizeHist( img, b ); //equalise b
cvMerge(b, g, r, NULL, img0); //merge all separate channels together to output image rgb
//cvReleaseImage(&r);
//cvReleaseImage(&g);
//cvReleaseImage(&b);
cvShowImage("Trans-out", img0); //Show output image in output window
cvWaitKey(0);
cvReleaseImage( &img);
cvReleaseImage( &img0);
cvDestroyWindow( "Trans-in");
cvDestroyWindow( "Trans-out");
return 0;
}
如果这些//注释有点烦人,我道歉,但这展示了我的“推理”吧。在我点击“运行”按钮之前,Xcode没有投诉。它基本上崩溃了(输入图像显示出来,但输出图像没有)。
底部还有一个错误消息:
OpenCV Error: Assertion failed (CV_ARE_SIZES_EQ(src, dst) && CV_ARE_TYPES_EQ(src, dst) && CV_MAT_TYPE(src->type) == CV_8UC1) in cvEqualizeHist, file /opt/local/var/macports/build/_Volumes_work_mports_dports_graphics_opencv/opencv/work/OpenCV-2.4.3/modules/imgproc/src/histogram.cpp, line 2414 terminate called throwing an exception
我完全不知道这意味着什么,下一步该怎么做。