返回目录
1. 亮度调整
import cv2
import numpy as np
import matplotlib.pyplot as plt
import hsi_bgr
img_file = './monarch.png'
# Read in BGR format
bgr = cv2.imread(img_file, cv2.IMREAD_COLOR)
# Increase the Red channel by 50%, decrease the blue channel by 50%
gamma_b = 0.5
gamma_g = 1.5
gamma_r = 1.2
B,G,R = cv2.split(bgr.astype(np.float32))
B *= gamma_b
G *= gamma_g
R *= gamma_r
G[G>255] = 255
R[R>255] = 255
bgr_new = cv2.merge((B, G, R))
bgr_new = bgr_new.astype(np.uint8)
plt.figure(figsize=(4,3))
plt.subplot(321), plt.imshow(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)), plt.title('RGB'), plt.axis('off')
plt.subplot(322), plt.imshow(cv2.cvtColor(bgr_new, cv2.COLOR_BGR2RGB)), plt.title('Intensity adj'), plt.axis('off')
plt.show()
2. 图像相加
像素运算 5:图像叠加
3. 图像相减
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Subtraction
# Images are from https://www.sohu.com/a/198697213_160408
img_gray1 = cv2.imread('diff_1.jpeg').astype(np.float32)
img_gray2 = cv2.imread('diff_2.jpeg').astype(np.float32)
img_diff = np.abs(img_gray1 - img_gray2)
print(np.max(img_diff))
img_diff = 255*img_diff/np.max(img_diff)
plt.figure(figsize=(4,2))
plt.subplot(131), plt.imshow(img_gray1.astype(np.uint8), cmap='gray'), plt.title('1'), plt.axis('off')
plt.subplot(132), plt.imshow(img_gray2.astype(np.uint8), cmap='gray'), plt.title('2'), plt.axis('off')
plt.subplot(133), plt.imshow(img_diff.astype(np.uint8), cmap='gray'), plt.title('diff'), plt.axis('off')
plt.show()
4. 图像相除
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Subtraction
# Images are from https://www.sohu.com/a/198697213_160408
img_gray1 = cv2.imread('diff_1.jpeg').astype(np.float32)
img_gray2 = cv2.imread('diff_2.jpeg').astype(np.float32)
img_diff = np.where(img_gray2>0, img_gray1/img_gray2, 0)
img_diff[img_diff<0.1] = 0
img_diff[img_diff==1] = 0
mask = np.logical_and(img_diff<1, img_diff>0)
img_diff[mask] = 1/img_diff[mask]
img_diff = 255*img_diff/np.mean(img_diff[img_diff>0])
img_diff[img_diff>255] = 255
plt.figure(figsize=(4,2))
plt.subplot(131), plt.imshow(img_gray1.astype(np.uint8), cmap='gray'), plt.title('1'), plt.axis('off')
plt.subplot(132), plt.imshow(img_gray2.astype(np.uint8), cmap='gray'), plt.title('2'), plt.axis('off')
plt.subplot(133), plt.imshow(img_diff.astype(np.uint8), cmap='gray'), plt.title('diff'), plt.axis('off')
plt.show()
-
图像相乘
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('monarch.png', cv2.IMREAD_COLOR)
mask_ori = cv2.imread('mask_star.png', cv2.IMREAD_GRAYSCALE)/255
img_h, img_w,_ = img.shape
mask_h, mask_w= mask_ori.shape
mask = np.zeros((img_h, img_w)).astype(np.uint8)
x0 = ((img_w-mask_w)>>1)
y0 = ((img_h-mask_h)>>1)
mask[y0:(y0+mask_h), x0:(x0+mask_w)] = mask_ori
# img_mask = np.multiply(img, cv2.merge([mask, mask,mask]))
img_mask = img.copy()
img_mask[mask==0]=0
img_mask2 = img.copy()
img_mask2[(1-mask)==0]=0
plt.figure('Mask')
plt.subplot(221), plt.imshow(img, cmap='gray')
plt.title('Monarch'), plt.axis('off')
plt.subplot(222), plt.imshow(mask, cmap='gray')
plt.title('Mask'), plt.axis('off')
plt.subplot(223), plt.imshow(img_mask, cmap='gray')
plt.title('Monarch*Mask'), plt.axis('off')
plt.subplot(224), plt.imshow(img_mask2, cmap='gray')
plt.title('Monarch*(1-Mask)'), plt.axis('off')
plt.show()
返回目录