抱歉,如果这个问题太基本了,我是 OpenCV 的新手。
我想将直方图均衡应用于 RGB 图像。HE 仅适用于单通道,所以我认为我必须将图像分成 3 个不同的通道,将 HE 应用于每个通道,然后将它们合并在一起以形成输出均衡图像。
所以我就这么做了,这是我真正相信应该有效的程序:
#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
我不知道这意味着什么或下一步该做什么。