Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple illumination correction in images openCV c++

I have some color photos and the illumination is not regular in the photos: one side of the image is brighter than the other side.

I would like to solve this problem by correcting the illumination. I think local contrast will help me but I don't know how :(

Would you please help me with a piece of code or a pipeline ?

like image 510
user3762718 Avatar asked Jun 21 '14 11:06

user3762718


Video Answer


2 Answers

Convert the RGB image to Lab color-space (e.g., any color-space with a luminance channel will work fine), then apply adaptive histogram equalization to the L channel. Finally convert the resulting Lab back to RGB.

What you want is OpenCV's CLAHE (Contrast Limited Adaptive Histogram Equalization) algorithm. However, as far as I know it is not documented. There is an example in python. You can read about CLAHE in Graphics Gems IV, pp474-485

Here is an example of CLAHE in action: enter image description here

And here is the C++ that produced the above image, based on http://answers.opencv.org/question/12024/use-of-clahe/, but extended for color.

#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(); } 
like image 62
Bull Avatar answered Sep 21 '22 19:09

Bull


The answer provided by Bull is the best I have come across so far. I have been using it to. Here is the python code for the same:

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_____# 
like image 37
Jeru Luke Avatar answered Sep 21 '22 19:09

Jeru Luke