use*_*718 49 c++ opencv contrast
我有一些彩色照片,照片中的照明不规则:图像的一侧比另一侧更亮.
我想通过纠正照明来解决这个问题.我认为局部对比会帮助我,但我不知道如何:(
你能用一段代码或管道来帮助我吗?
Bul*_*ull 101
将RGB图像转换为Lab颜色空间(例如,任何具有亮度通道的颜色空间都可以正常工作),然后将自适应直方图均衡应用于L通道.最后将生成的Lab转换回RGB.
你想要的是OpenCV的CLAHE(对比度限制自适应直方图均衡)算法.但是,据我所知,它没有记录.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();
}
Run Code Online (Sandbox Code Playgroud)
Jer*_*uke 28
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_____#
Run Code Online (Sandbox Code Playgroud)
基于Bull编写的优秀C++示例,我能够为Android编写此方法.
我已将"Core.extractChannel"替换为"Core.split".这避免了已知的内存泄漏问题.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
public Mat onCameraFrame(CvCameraViewFrame inputFrame){
Mat col = inputFrame.rgba();
applyCLAHE(col, col);//Apply the CLAHE algorithm to input color image.
return col;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59244 次 |
最近记录: |