简单的光照校正openCV c ++

我有一些彩色照片,照片中的照明不规则:图像的一侧比另一侧更明亮。

我想通过纠正照明来解决这个问题。 我认为当地的对比将帮助我,但我不知道如何:(

你能帮我一段代码或一条pipe道吗?

将RGB图像转换为Lab色彩空间(例如,任何具有亮度通道的色彩空间都可以正常工作),然后将自适应直方图均衡应用于L通道。 最后将生成的Lab转换回RGB。

你想要的是OpenCV的CLAHE(对比度有限自适应直方图均衡)algorithm。 但是,据我所知,这是没有logging。 python中有一个例子 。 你可以在Graphics Gems IV,pp474-485中看到CLAHE

这里是CLAHE的一个例子: 在这里输入图像描述

这里是基于http://answers.opencv.org/question/12024/use-of-clahe/生成上面的图像的C ++,但扩展颜色。

#include <opencv2/core.hpp> #include <vector> // std::vector int main(int argc, char** argv) { // READ RGB color image and convert it to Lab cv::Mat bgr_image = cv::imread("image.png"); cv::Mat lab_image; cv::cvtColor(bgr_image, lab_image, CV_BGR2Lab); // Extract the L channel std::vector<cv::Mat> lab_planes(3); cv::split(lab_image, lab_planes); // now we have the L image in lab_planes[0] // apply the CLAHE algorithm to the L channel cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(); clahe->setClipLimit(4); cv::Mat dst; clahe->apply(lab_planes[0], dst); // Merge the the color planes back into an Lab image dst.copyTo(lab_planes[0]); cv::merge(lab_planes, lab_image); // convert back to RGB cv::Mat image_clahe; cv::cvtColor(lab_image, image_clahe, CV_Lab2BGR); // display the results (you might also want to see lab_planes[0] before and after). cv::imshow("image original", bgr_image); cv::imshow("image CLAHE", image_clahe); cv::waitKey(); } 

Bull提供的答案是迄今为止我所见过的最好的答案。 我一直在使用它。 这里是相同的Python代码:

 import cv2 #-----Reading the image----------------------------------------------------- img = cv2.imread('Dog.jpg', 1) cv2.imshow("img",img) #-----Converting image to LAB Color model----------------------------------- lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB) cv2.imshow("lab",lab) #-----Splitting the LAB image to different channels------------------------- l, a, b = cv2.split(lab) cv2.imshow('l_channel', l) cv2.imshow('a_channel', a) cv2.imshow('b_channel', b) #-----Applying CLAHE to L-channel------------------------------------------- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8)) cl = clahe.apply(l) cv2.imshow('CLAHE output', cl) #-----Merge the CLAHE enhanced L-channel with the a and b channel----------- limg = cv2.merge((cl,a,b)) cv2.imshow('limg', limg) #-----Converting image from LAB Color model to RGB model-------------------- final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR) cv2.imshow('final', final) #_____END_____# 

基于Bull编写的伟大C ++示例 ,我能够为Android编写此方法。

我用“Core.split”replace了“Core.extractChannel”。 这可以避免已知的内存泄漏问题 。

 public void applyCLAHE(Mat srcArry, Mat dstArry) { //Function that applies the CLAHE algorithm to "dstArry". if (srcArry.channels() >= 3) { // READ RGB color image and convert it to Lab Mat channel = new Mat(); Imgproc.cvtColor(srcArry, dstArry, Imgproc.COLOR_BGR2Lab); // Extract the L channel Core.extractChannel(dstArry, channel, 0); // apply the CLAHE algorithm to the L channel CLAHE clahe = Imgproc.createCLAHE(); clahe.setClipLimit(4); clahe.apply(channel, channel); // Merge the the color planes back into an Lab image Core.insertChannel(channel, dstArry, 0); // convert back to RGB Imgproc.cvtColor(dstArry, dstArry, Imgproc.COLOR_Lab2BGR); // Temporary Mat not reused, so release from memory. channel.release(); } } 

并称之为如此:

 public Mat onCameraFrame(CvCameraViewFrame inputFrame){ Mat col = inputFrame.rgba(); applyCLAHE(col, col);//Apply the CLAHE algorithm to input color image. return col; } 

你有你的回应,但另一个soltuion是使用直方图来纠正图像: http ://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html

对于Bull写的伟大的C ++示例 ,如果使用最新的OpenCV版本,我会build议添加标题

 #include <opencv2/opencv.hpp>