opencv-python数图实验(一)线性和非线性校正

时间:2025-01-17 11:58:13
# 关于版本 # numpy 1.16.6 # opencv-python 4.2.0.34 import cv2 as cv import numpy as np # 导入图像 srcImage = cv.imread('C:\\Users\\MRSANG\\Desktop\\CV_test\\test_1\\') if srcImage is None: print('Could not open or find the image: ') exit(0) else: print('loading ... successfully') cv.imshow('origin_win', srcImage) # rgb三通道分离 并显示 ori_channel_r, ori_channel_g, ori_channel_b = cv.split(srcImage) cv.imshow("ori_channel_r", ori_channel_r) cv.imshow("ori_channel_g", ori_channel_g) cv.imshow("ori_channel_b", ori_channel_b) # rgb三通道均衡化 equ_channel_r = cv.equalizeHist(ori_channel_r) equ_channel_g = cv.equalizeHist(ori_channel_g) equ_channel_b = cv.equalizeHist(ori_channel_b) equ_Image = cv.merge((equ_channel_r, equ_channel_g, equ_channel_b)) # 均衡化显示 cv.imshow('equalizeHistImage', equ_Image) # 锐化 sharp_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) sharp_image = cv.filter2D(srcImage, ddepth=-1, kernel=sharp_kernel) # 锐化显示 cv.imshow('sharp_image', sharp_image) # 伽马矫正以及亮度对比度调整 img2 = np.copy(srcImage) img2_winname = 'srcImage2' img3 = np.copy(srcImage) print(img3) img3_winname = 'srcImage3' cv.imshow(img2_winname, img2) cv.imshow(img3_winname, img3) # 伽马和亮度对比度的回调函数 def gamma_function(x): fGamma = cv.getTrackbarPos('gamma', img2_winname) / 50 # 打印gamma的值 # print(('gamma', 'srcImage_copy')) img2_copy = np.array(pow(img2/255, fGamma) * 255, dtype='uint8') cv.imshow(img2_winname, img2_copy) print('gamma:{}'.format(fGamma)) # 输出gamma比例值 print(' - - - - - -') def contrast_function(x): f_alpha = cv.getTrackbarPos('alpha', img3_winname)/10.0 f_beta = cv.getTrackbarPos('beta', img3_winname) # img3_copy = ((f_alpha * img3 + f_beta), dtype='uint8') # 这样会导致>255像素的高位被砍掉,导致图像展示的奇奇怪怪 img3_copy = np.array(np.clip(f_alpha * img3 + f_beta, 0, 255), dtype=np.uint8) cv.imshow(img3_winname, img3_copy) print('alpha:{}'.format(f_alpha)) # 输出alpha比例值 print('beta:{}'.format(f_beta)) # 输出beta值 print(' - - - - - -') # 创建滑动条 cv.createTrackbar('gamma', img2_winname, 50, 100, gamma_function) # gamma cv.createTrackbar('alpha', img3_winname, 10, 30, contrast_function) # contrast cv.createTrackbar('beta', img3_winname, 0, 200, contrast_function) # brightness # RGB2HSV 导入图像 Color_CvtImg = cv.imread('C:\\Users\\MRSANG\\Desktop\\CV_test\\test_1\\') Color_CvtImg2 = cv.cvtColor(Color_CvtImg, cv.COLOR_BGR2HSV) # 色彩空间分离并转换 Color_H, Color_S, Color_V = cv.split(Color_CvtImg) cv.imshow('Cvt_H', Color_H) cv.imshow('Cvt_S', Color_S) cv.imshow('Cvt_V', Color_V) # 伪彩色变化 FColor_Src = cv.imread('C:\\Users\\MRSANG\\Desktop\\CV_test\\test_1\\', cv.IMREAD_GRAYSCALE) FColor_Img = cv.applyColorMap(FColor_Src, cv.COLORMAP_JET) cv.imshow('FColor_Img', FColor_Img) cv.waitKey(0)