Reputation: 43
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.
Upvotes: 4
Views: 13691
Reputation: 3076
For doing histogram equalization to a RGB image...convert it to HSV image and then equalize the V plane...
merge H,S and V
vector<Mat> hsv_planes;
cvtColor(image,image,CV_BGR2HSV);
split(image,hsv_planes);
equalizeHist(hsv_planes[2],hsv_planes[2]);
merge(hsv_planes,image);
cvtColor(image,image,CV_HSV2BGR);
Upvotes: 2
Reputation: 16796
You are using the cvEqualizeHist
function incorrectly in the following lines:
cvEqualizeHist( img, r ); //equalise r
cvEqualizeHist( img, g ); //equalise g
cvEqualizeHist( img, b ); //equalise b
img
is a 3 channel image while r
,g
and b
are single channel images. cvEqualizeHist
doesn't work on 3 channel images.
You have to do the following:
cvEqualizeHist( r, r ); //equalise r
cvEqualizeHist( g, g ); //equalise g
cvEqualizeHist( b, b ); //equalise b
Upvotes: 4