python图像对比度增强_如何在Python OpenCV中增加图像的对比度

I am new to Python OpenCV. I have read some documents and answers here but I am unable to figure out what the following code means:

if (self.array_alpha is None):

self.array_alpha = np.array([1.25])

self.array_beta = np.array([-100.0])

# add a beta value to every pixel

cv2.add(new_img, self.array_beta, new_img)

# multiply every pixel value by alpha

cv2.multiply(new_img, self.array_alpha, new_img)

I have come to know that Basically, every pixel can be transformed as X = aY + b where a and b are scalars.. Basically, I have understood this. However, I did not understand the code and how to increase contrast with this.

Till now, I have managed to simply read the image using img = cv2.imread('image.jpg',0)

Thanks for your help

解决方案

I would like to suggest a method using the LAB color channel. Wikipedia has enough information regarding what the LAB color channel is about.

I have done the following using OpenCV 3.0.0 and 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_____#

You can run the code as it is.

To know what CLAHE (Contrast Limited Adaptive Histogram Equalization)is about, you can again check Wikipedia.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值