基于上一篇博客https://developer.aliyun.com/article/1624210
虽然可以将所有图片依次输出,但是这样观察效率太低了,我们可以结合cv2或者plt将全部输出的灰度图结合在一起输出,便于观察。下面直接给代码
1.CV2绘制注意事项:
np.vstack不能将不同通道数的图片融合再一起,比如说RGB的彩色图和灰度图像融合结合在一起,灰度图是一通道的,但是还是有方法解决这个问题,把一通到变为三通道就行了,也就是上面的通过merge函数来实现,变为三通道的就行了。
----1.1 CV2实现具体代码
import cv2
import numpy as np
image = cv2.imread('F:/1.jpg')
#zeros = np.zeros(image.shape[:2],dtype="uint8");#创建与image相同大小的零矩阵
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray = cv2.merge([gray,gray,gray])
print(gray.shape)
ret, binary = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY)
# binary = cv2.merge([binary,binary,binary])
print(binary.shape)
ret1, binaryinv = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY_INV)
# binaryinv = cv2.merge([binaryinv,binaryinv,binaryinv])
print(binaryinv.shape)
ret2, trunc = cv2.threshold(gray, 175, 255, cv2.THRESH_TRUNC)
# trunc = cv2.merge([trunc,trunc,trunc])
print(trunc.shape)
ret3, tozero = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO)
# tozero = cv2.merge([tozero,tozero,tozero])
print(tozero.shape)
ret4, tozeroinv = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO_INV)
# tozeroinv = cv2.merge([tozeroinv,tozeroinv,tozeroinv])
print(tozeroinv.shape)
"""上面代码的作用是,将灰度图img2gray中灰度值小于175的点置0,灰度值大于175的点置255"""
cv2.putText(image, 'original',(20,40), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))
cv2.putText(gray,'gray', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (128, 0, 0))
cv2.putText(binary,'binary', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
cv2.putText(binaryinv,'binaryinv', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
cv2.putText(trunc,'trunc', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
cv2.putText(tozero,'tozero', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
cv2.putText(tozeroinv,'tozeroinv', (20,60), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
results = (image,gray,binary,binaryinv,trunc,tozeroinv,tozero)
# np.hstack()将两个数组按列放到一起
line1 = np.hstack(results[:2])
line2 = np.hstack(results[2:4])
line3 = np.hstack(results[4:6])
# line3 = np.hstack(results[2:])
combined = np.vstack([line1, line2,line3]) # 将多个数组按行放到一起
cv2.namedWindow('detect-combined', cv2.WINDOW_NORMAL)
cv2.imshow('detect-combined', combined)
cv2.waitKey(0)
----1.2 运行结果:
2.通过matplotlib来绘制
----2.1 具体代码:
import cv2
import numpy as np
image = cv2.imread('F:/1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# gray = cv2.merge([gray,gray,gray])
print(gray.shape)
ret, binary = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY)
# binary = cv2.merge([binary,binary,binary])
print(binary.shape)
ret1, binaryinv = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY_INV)
# binaryinv = cv2.merge([binaryinv,binaryinv,binaryinv])
print(binaryinv.shape)
ret2, trunc = cv2.threshold(gray, 175, 255, cv2.THRESH_TRUNC)
# trunc = cv2.merge([trunc,trunc,trunc])
print(trunc.shape)
ret3, tozero = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO)
# tozero = cv2.merge([tozero,tozero,tozero])
print(tozero.shape)
ret4, tozeroinv = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO_INV)
# tozeroinv = cv2.merge([tozeroinv,tozeroinv,tozeroinv])
print(tozeroinv.shape)
"""上面代码的作用是,将灰度图img2gray中灰度值小于175的点置0,灰度值大于175的点置255"""
results = (image,gray,binary,binaryinv,trunc,tozeroinv,tozero)
titles = ['image','gray','binary','binaryinv','trunc','tozeroinv','tozero']
import matplotlib.pyplot as plt
for i in range(7):
plt.subplot(2,4,i+1),plt.imshow(results[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
----2.3 运行结果
3.二者比较
- 通过对比两者输出的图片,cv2输出的原图和matplotlib输出的原图并不相同,cv2输出的效果要更好一点
- cv2绘制中的几行几列必须要对齐,如果不对齐就会报错,图片数量必须得刚刚好分布在上面,而matplotlib并不需要,可以输出任意张图片。
4.常见颜色RGB颜色值
128/0/0 深红
255/0/0 红
255/0/255 粉红
255/153/204 玫瑰红
153/51/0 褐色
255/102/0 桔黄
255/153/0 浅桔黄
255/204/0 金色
255/204/153 棕黄
51/51/0 橄榄绿
128/128/0 深黄
153/204/0 酸橙色
255/255/0 黄色
255/255/153 浅黄
0/51/0 深绿
0/128/0 绿色
51/153/102 海绿
0/255/0 鲜绿
204/255/204 浅绿
0/51/102 深灰蓝
0/128/128 青色
51/204/204 宝石蓝
0/255/255 青绿
204/255/255 浅青绿
0/0/128 深蓝
0/0/255 蓝色
51/102/255 浅蓝
0/204/255 天蓝
153/204/255 浅蓝
51/51/153 靛蓝
102/102/153 蓝灰
128/0/128 紫色
153/51/102 梅红
204/153/255 淡紫
51/51/51 80%灰
128/128/128 50%灰
153/153/153 40%灰
192/192/192 25%灰