这是在做数据挖掘时入的cv2的坑,cv2功能强大,但是由于用的不熟练,踩了很多坑
坑1
cv2默认只读取三通道
import cv2
import matplotlib.pyplot as plt
imgpath = r"D:/picture/original/323-1.png"
img = cv2.imread(imgpath,cv2.IMREAD_COLOR)
img2 = cv2.imread(imgpath,cv2.IMREAD_GRAYSCALE)
img3 = cv2.imread(imgpath,-1)#cv2.IMREAD_UNCHANGED,可用-1替代
plt.subplot(131)
plt.imshow(img)
plt.subplot(132)
plt.imshow(img2)
plt.subplot(133)
plt.imshow(img3)
坑2
cv2默认BGR读取图片,不同于PLT等用RGB读取图片
import cv2
import matplotlib.pyplot as plt
imgpath = r"D:/picture/original/323-1.png"
img = cv2.imread(imgpath)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 颜色空间转换
# b,g,r = cv2.split(img)
# img_rgb = cv2.merge([r,g,b])
plt.subplot(131)
plt.imshow(img)
plt.subplot(132)
plt.imshow(img_rgb)
plt.subplot(133)
img2 = plt.imread(imgpath)
plt.imshow(img2)
plt.show()
坑三
cv2.imshow()好像依旧显示cv2.imread()的默认的读取参数读到的图片,这里建议直接改用plt.imshow()显示图片
import cv2
import matplotlib.pyplot as plt
imgpath = r"D:/picture/original/323-1.png"
img = cv2.imread(imgpath)
b,g,r = cv2.split(img)
img_rgb = cv2.merge([r,g,b])
cv2.namedWindow('2',cv2.WINDOW_FREERATIO)
cv2.imshow("2",img_rgb)
cv2.waitKey()
import cv2
maskPath = r"D:/picture/original/323-1.png"
#cv2cv2.WINDOW_FREERATIO读入图片
mask = cv2.imread(maskPath, -1)
cv2.namedWindow('mask',cv2.WINDOW_FREERATIO)
cv2.resizeWindow('mask', 300, 300)
cv2.imshow('mask', mask)
cv2.waitKey()
坑四
可以用
• cv2.THRESH_BINARY(黑白二值)
• cv2.THRESH_BINARY_INV(黑白二值反转)
• cv2.THRESH_TRUNC (得到的图像为多像素值)
• cv2.THRESH_TOZERO
• cv2.THRESH_TOZERO_INV
嗯,有点问题没理清