import cv2 import numpy as np import matplotlib.pyplot as plt image_path = './Lenna.jpg' image = cv2.imread(image_path) num_row, num_col, num_ch = image.shape # image channels are in BGR B = image[:, :, 0] G = image[:, :, 1] R = image[:, :, 2] # change the channel order from BGR to RGB and restore # CODE HERE image = cv2.merge([R, G, B]) fig = plt.figure(figsize=(11, 9)) fig.suptitle('Color image and RGB channel') ax = fig.add_subplot(2, 2, 1) ax.imshow(image) ax.axis('off') ax.axis('equal') ax.set_title('color image') # display the red channel in grayscale ax = fig.add_subplot(2, 2, 2) ax.imshow(R, cmap='gray') ax.axis('off') ax.axis('equal') ax.set_title('Channel R') # display the green channel in grayscale ax = fig.add_subplot(2, 2, 3) ax.imshow(G, cmap='gray') ax.axis('off') ax.axis('equal') ax.set_title('Channel G') # display the blue channel in grayscale ax = fig.add_subplot(2, 2, 4) ax.imshow(B, cmap='gray') ax.axis('off') ax.axis('equal') ax.set_title('Channel B') plt.pause(0) # calculate the mean value, variance and covirances # CODE HERE # Decomment and complete the following lines corr_RG = corr_GB = corr_BR = # Decomment the following lines print('The correlation between red and green is: ' + str(corr_RG)) print('The correlation between green and blue is: ' + str(corr_GB)) print('The correlation between blue and red is: ' + str(corr_BR)) # total contrast: # CODE HERE # proportions of each channel to the total contrast # Decomment and complete the following lines print('The propotion of red channel is: ' + str(CODE HERE)) print('The propotion of green channel is: ' + str(CODE HERE)) print('The propotion of blue channel is: ' + str(CODE HERE))
时间: 2023-12-08 07:05:18 浏览: 100
代码中有一些缺失的部分需要填写,我来帮你看一下。
```
import cv2
import numpy as np
import matplotlib.pyplot as plt
image_path = './Lenna.jpg'
image = cv2.imread(image_path)
num_row, num_col, num_ch = image.shape
# image channels are in BGR
B = image[:, :, 0]
G = image[:, :, 1]
R = image[:, :, 2]
# change the channel order from BGR to RGB and restore
image = cv2.merge([R, G, B])
fig = plt.figure(figsize=(11, 9))
fig.suptitle('Color image and RGB channel')
ax = fig.add_subplot(2, 2, 1)
ax.imshow(image)
ax.axis('off')
ax.axis('equal')
ax.set_title('color image')
# display the red channel in grayscale
ax = fig.add_subplot(2, 2, 2)
ax.imshow(R, cmap='gray')
ax.axis('off')
ax.axis('equal')
ax.set_title('Channel R')
# display the green channel in grayscale
ax = fig.add_subplot(2, 2, 3)
ax.imshow(G, cmap='gray')
ax.axis('off')
ax.axis('equal')
ax.set_title('Channel G')
# display the blue channel in grayscale
ax = fig.add_subplot(2, 2, 4)
ax.imshow(B, cmap='gray')
ax.axis('off')
ax.axis('equal')
ax.set_title('Channel B')
plt.pause(0)
# calculate the mean value, variance and covirances
mean_R = np.mean(R)
mean_G = np.mean(G)
mean_B = np.mean(B)
var_R = np.var(R)
var_G = np.var(G)
var_B = np.var(B)
cov_RG = np.cov(R.flatten(), G.flatten())[0][1]
cov_GB = np.cov(G.flatten(), B.flatten())[0][1]
cov_BR = np.cov(B.flatten(), R.flatten())[0][1]
# Decomment the following lines
corr_RG = cov_RG / np.sqrt(var_R * var_G)
corr_GB = cov_GB / np.sqrt(var_G * var_B)
corr_BR = cov_BR / np.sqrt(var_B * var_R)
print('The correlation between red and green is: ' + str(corr_RG))
print('The correlation between green and blue is: ' + str(corr_GB))
print('The correlation between blue and red is: ' + str(corr_BR))
# total contrast:
contr = np.sqrt(var_R + var_G + var_B)
# proportions of each channel to the total contrast
prop_R = np.sqrt(var_R) / contr
prop_G = np.sqrt(var_G) / contr
prop_B = np.sqrt(var_B) / contr
print('The propotion of red channel is: ' + str(prop_R))
print('The propotion of green channel is: ' + str(prop_G))
print('The propotion of blue channel is: ' + str(prop_B))
```
你需要填写的部分已经在注释中标出,其中:
- 需要计算各个通道的均值、方差和协方差矩阵,这部分可以使用 numpy 中的相关函数来实现;
- 需要计算各个通道之间的相关系数,通过协方差和各自的方差计算即可;
- 需要计算总对比度和各个通道对总对比度的贡献,这里的总对比度可以使用各个通道方差的和的平方根来计算。
阅读全文