图像增强处理

xiaoxiao2021-02-28  30

亮度公式:g(x,y) = a*f(x,y)+b

import cv2 import time #亮度公式:g(x,y) = a*f(x,y)+b print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) zz = time.time() img=cv2.imread('img\yuantu.jpg') cv2.imshow('img',img) rows,cols,channels=img.shape dst=img.copy() a=1.2 b=100 for i in range(rows): for j in range(cols): for c in range(3): color=img[i,j][c]*a+b if color>255: dst[i,j][c]=255 elif color<0: dst[i,j][c]=0 cv2.namedWindow('dst',0) cv2.resizeWindow("dst", 550, 980) cv2.imshow('dst',dst) cv2.waitKey(0) cv2.destroyAllWindows() cc = time.time() print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) print(cc-zz)

对比度增强

from PIL import Image from PIL import ImageEnhance import time print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) a = time.time() #原始图像 image = Image.open('img\yuantu.jpg') #image.show() # 保存图片 #img.save('qq_image_thumb.jpg', 'JPEG') #亮度增强 #enh_bri = ImageEnhance.Brightness(image) #brightness = 1.5 #image_brightened = enh_bri.enhance(brightness) #image_brightened.show() #色度增强 #enh_col = ImageEnhance.Color(image) #color = 1.5 #image_colored = enh_col.enhance(color) #image_colored.show() #对比度增强 enh_con = ImageEnhance.Contrast(image) contrast = 2 image_contrasted = enh_con.enhance(contrast) image_contrasted.show() #锐度增强 #enh_sha = ImageEnhance.Sharpness(image) #sharpness = 3.0 #image_sharped = enh_sha.enhance(sharpness) #image_sharped.show() b = time.time() print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) print(b-a)

调整灰度值:dst=C*log(1+src)

import cv2 import numpy as np import time print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) a = time.time() fn="img\yuantu.jpg" #dst=C*log(1+src) myimg=cv2.imread(fn) #img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) jg_img=np.array(40*np.log(myimg+1),np.uint8) cv2.namedWindow('src',0) cv2.resizeWindow("src", 550, 980) cv2.imshow('src',myimg) cv2.namedWindow('dst',0) cv2.resizeWindow("dst", 550, 980) cv2.imshow('dst',jg_img) b = time.time() print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) print(b-a)
转载请注明原文地址: https://www.6miu.com/read-2619985.html

最新回复(0)