对比度的调节:
将图像转化到HSV格式下,先选出所有亮度的中值。当要增加对比度的时候,则降低亮度值比中值小的值,增加亮度值比中值更大的值;当要减少对比度的时候,则增加亮度值比中值小的值,降低亮度值比中值更大的值,使所有亮度值向中值靠近。
根据公式(x-avg_R(G/B)*a + x (a 为-1到1 之间,x 是原像素值)计算
亮度的调节:
介绍
将图像转化为HLS格式,通过调整图像中L通道的值来改变图片的亮度
直接在RGB色彩空间下给每个像素都加上或减去一个固定值。
代码
import numpy as np
import cv2 as cv
from PIL import Image
import copy
# change 为改变的数值
def changeL_func(img,changeL):
# cv.imshow("yuan1", img)
changeLight = cv.cvtColor(img, cv.COLOR_RGB2HLS)
for i in range(0, changeLight.shape[0]):
for j in range(0, changeLight.shape[1]):
re = changeLight[i, j, 1] + changeL
if (re + changeL > 255):
changeLight[i, j, 1] = 255
elif (re + changeL < 0):
changeLight[i, j, 1] = 0
else:
changeLight[i, j, 1] = re
img = cv.cvtColor(changeLight, cv.COLOR_HLS2RGB)
# cv.imshow("yuan", img)
return img
def main():
image = cv.imread(r"E:/tu/13.png")
changeL_img = changeL_func(copy.copy(image), 20)
cv.imshow("change_light ",changeL_img)
cv.imshow("yuantu",image)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
main()
色彩饱和度调节:
介绍
将图像转化到HSV格式下,通过调整图像中的S值来改变图片色彩的饱和度。
代码
import numpy as np
import cv2 as cv
from PIL import Image
import copy
# changeS 为改变的数值
def changeS_func(img,changeS):
# cv.imshow("yuan0", img)
changeSaturation = cv.cvtColor(img, cv.COLOR_RGB2HSV)
for i in range(0, changeSaturation.shape[0]):
for j in range(0, changeSaturation.shape[1]):
re = changeSaturation[i, j, 1] + changeS
if ( re > 255):
changeSaturation[i, j, 1] = 255
elif (re < 0):
changeSaturation[i, j, 1] = 0
else:
changeSaturation[i, j, 1] = re
img = cv.cvtColor(changeSaturation, cv.COLOR_HSV2RGB)
# cv.imshow("yuan2", img)
return img
def main():
image = cv.imread(r"E:/tu/13.png")
changeS_img = changeS_func(copy.copy(image), 20)
cv.imshow("change_Saturation",changeS_img)
cv.imshow("yuantu",image)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
main()
灰度化:
当图像的RGB值相同时,该图像即会变为灰度图像,其中R=G=B的值叫做灰度值。灰度值有多种选择方式,如,选取RGB任一通道值作为灰度值,选取RGB中的最大值作为灰度值,选取RGB中的中值作为灰度值,选取RGB中的最小值作为灰度值等。
import cv2 as cv
import math
import numpy as np
def rgb2gray(img):
h=img.shape[0]
w=img.shape[1]
img1=np.zeros((h,w),np.uint8)
for i in range(h):
for j in range(w):
img1[i, j] = np.max(img[i, j])#取最大值,若取最小值则为np.min()
return img1
image= cv.imread(r"1.jpg")
grayimage=rgb2gray(image)
cv.imshow("image",grayimage)
cv.waitKey(0)
cv.destroyAllWindows()