在进行色彩矫正的时候常常会 提取 24色卡中每个色块的像素,下面这个脚本就是一次框选24个patch, 并将均值保存在txt中。
import cv2
import colour
import numpy as np
global img,result
global point1, point2
result = []
def on_mouse(event, x, y, flags, param):
global img, point1, point2, result
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), 2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), 2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左键释放
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 0, 255), 2)
cv2.imshow('image', img2)
min_x = min(point1[0], point2[0])
min_y = min(point1[1], point2[1])
width = abs(point1[0] - point2[0])
height = abs(point1[1] - point2[1])
cut_img = img[min_y:min_y + height, min_x:min_x + width]
#cv2.imwrite('lena3.jpg', cut_img)
ddd = np.mean(cut_img, axis=(0, 1))
print(ddd)
result.append(ddd)
def main(file):
global img
img = cv2.imread(file)
#img = cv2.flip(img, 1)
print(img.shape)
cv2.namedWindow('image')
cv2.setMouseCallback('image', on_mouse)
cv2.imshow('image', img)
cv2.waitKey(0)
if __name__ == '__main__':
file = r'C:\Users\rp\Downloads\RAWimage\CWF_raw_wb.png'
main(file)
print(result)
result = np.array(result)
result = result.reshape([-1,3])
result = result[..., ::-1]
np.savetxt(file[:-4]+'_patch24.txt', result, fmt='%.3f')