Sorry if this question is so basic, I’m new with OpenCV.
I want to apply histogram equalisation to an RGB image. HE only works for single channel so I figured that I have to split the image into 3 different channels, apply HE to each one of them, then merge them all together to form output equalised image.
So I did just that and here’s my program which I REALLY believe should work:
#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;
}
Apologies if the //comments are a bit annoying, but it shows my ‘reasoning’ I guess. Xcode didn’t complain until I hit the ‘Run’ button. It basically crashed (input image shows, but not output image).
Plus an error message at the bottom:
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
And I have no idea what it means or what to do next.
You are using the
cvEqualizeHistfunction incorrectly in the following lines:imgis a 3 channel image whiler,gandbare single channel images.cvEqualizeHistdoesn’t work on 3 channel images.You have to do the following: