原图:
用opencv读入图像数据并进行灰度处理之后,用 matplot.pyplot 对灰度图进行输出
import matplot.pyplot as plt
import cv2
srcImage = cv2.imread("/home/jinyan/anaconda3/envs/tensorflow/models/research/object_detection/sm_products/111/A0002551.jpg")
grayImage = cv2.cvtColor(srcImage, cv2.COLOR_BGR2GRAY) # 灰度变换
plt.imshow(grayImage)
plt.show()
import cv2
srcImage = cv2.imread("/home/jinyan/anaconda3/envs/tensorflow/models/research/object_detection/sm_products/111/A0002551.jpg")
grayImage = cv2.cvtColor(srcImage, cv2.COLOR_BGR2GRAY) # 灰度变换
cv2.imshow("grayimage", grayImage)
出现上面这个原因是因为 opencv 的接口使用BGR模式,而 matplotlib.pyplot 接口使用的是RGB模式,用opencv读取图像并用 matplotlib.pyplot 输出的效果:
此处,若需要 matplotlib.pyplot 的输出为原图颜色,需要把通道顺序变换一下:
import cv2
import matplotlib.pyplot as plt
srcImage = cv2.imread("/home/jinyan/anaconda3/envs/tensorflow/models/research/object_detection/sm_products/111/A0002551.jpg")
b, g, r = cv2.split(srcImage)
srcImage_new = cv2.merge([r, g, b])
plt.imshow(srcImage_new)
plt.show()
此处用matplotlib.pyplot输出的图片就和原图一样了,但是通道变换之后对灰度图进行输出的图片颜色仍然为绿色
import cv2
import matplotlib.pyplot as plt
srcImage = cv2.imread("/home/jinyan/anaconda3/envs/tensorflow/models/research/object_detection/sm_products/111/A0002551.jpg")
b, g, r = cv2.split(srcImage)
srcImage_new = cv2.merge([r, g, b])
plt.imshow(srcImage_new)
plt.show()
grayImage = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 灰度变换
plt.imshow(grayImage)
plt.show()
这是因为我们还是直接使用plt显示图像,它默认使用三通道显示图像,需要我们在可视化函数里多指定一个参数
plt.imshow(grayImage, cmap="gray")
import cv2
import matplotlib.pyplot as plt
srcImage = cv2.imread("/home/jinyan/anaconda3/envs/tensorflow/models/research/object_detection/sm_products/111/A0002551.jpg")
b, g, r = cv2.split(srcImage)
srcImage_new = cv2.merge([r, g, b])
plt.imshow(srcImage_new)
plt.show()
grayImage = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 灰度变换
plt.imshow(grayImage, camp = "gray")
plt.show()