EDIT:
The pointer/reference usage may be wrong, but this OpenCV behaviour also happens if any of the cv::Mat variables are declared outside of the equalization's block (in my case, in the definiton of the class they are members of).
I'm using the same logic in my code as the one described here, yet I get a weird black and white image as the result, see original and result. I'm using OpenCV 4.0.0 with C++ in Visual Studio 2017 15.8.8. Previous declarations:
cv::Mat *equalized_image;
cv::Mat &original_image = cv::Mat(cv::imread(file_path));
Where file_path is an std::string. Equalization:
if (original_image.channels() >= 3) {
// convert to YCrCb colourspace for luminance channel
cv::cvtColor(original_image, *equalized_image, cv::COLOR_BGR2YCrCb);
// split image by channels
std::vector<cv::Mat> channels;
cv::split(*equalized_image, channels);
// equalize only the histogram of the luminance channel
cv::equalizeHist(channels[0], channels[0]);
// merge back
cv::merge(channels, *equalized_image);
// convert back to BGR colourspace
cv::Mat result;
cv::cvtColor(*equalized_image, result, cv::COLOR_YCrCb2BGR);
cv::namedWindow("Equalized");
cv::imshow("Equalized", result);
}
I'm surprised your code compiles. The logic you use is fine, but the pointers (*equalized_image) and references (&original_image) you have coded are confusing. If you're running in release mode, it can do funny stuff that initializes memory and makes things work. Always check your code in Debug mode first.
Change your code to this and it should work as expected:
cv::Mat equalized_image;
cv::Mat original_image = cv::imread(file_path);
// convert to YCrCb colourspace for luminance channel
cv::cvtColor(original_image, equalized_image, cv::COLOR_BGR2YCrCb);
// split image by channels
std::vector<cv::Mat> channels;
cv::split(equalized_image, channels);
// equalize only the histogram of the luminance channel
cv::equalizeHist(channels[0], channels[0]);
// merge back
cv::merge(channels, equalized_image);
// convert back to BGR colourspace
cv::Mat result;
cv::cvtColor(equalized_image, result, cv::COLOR_YCrCb2BGR);
cv::namedWindow("Equalized");
cv::imshow("Equalized", result);
waitKey(0);