Python数字图像处理——OpenCV实例解析⽬录 import cv2 as cv
# 读取彩⾊图像
img_cai = cv.imread("E:/1.png")
# 读取灰度图像
img_hui = cv.imread("E:/1.png",cv.IMREAD_GRAYSCALE)
print(img_cai.shape)
print(img_hui.shape)
哺乳睡衣# 注意:opencv读取的彩⾊图像默认的颜⾊是BGR matplotlib默认的彩⾊图像是RGB
import matplotlib.pyplot as plt
plt.imshow(img_cai) # 全图展⽰
plt.imshow(img_cai[30:160,200:320]) # 截取部分
# 保存图像
cv.imwrite("duqu.png",img_cai[30:160,200:320])
修改像素值
# 修改像素值
img[30:50,:] = [255,0,0]
铁盒制作
plt.imshow(img)
图像融合
飞行鞋
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img1 = cv.imread('E:/4.png')
img2 = cv.imread('E:/3.png')
dst = cv.addWeighted(img1,0.7,img2[:233,:230,:],0.3,0)
cv.imshow('Original Image 1', img1)
cv.imshow('Original Image 2', img2)
cv.imshow('Fusion Image', dst)
cv.waitKey(0)
cv.destroyAllWindows()
图像的⼏何变换
import cv2 as cv
import numpy as np
紧急切断装置from matplotlib import pyplot as plt
lq = cv.imread('E:/6.png')
# 调整图⽚⼤⼩
i2 = cv.resize(lq,(200,500),interpolation=cv.INTER_CUBIC) # 平移图⽚
M = np.float64([[1,0,100],[0,1,50]])
i3 = cv.warpAffine(lq,M,(300,300))
# 图⽚的旋转
M = cv.getRotationMatrix2D((250,250),30,1)
i4 = cv.warpAffine(lq,M,(300,300))
# 图⽚的倾斜
d1 = np.float32([[100,10],[300,10],[100,100]])
d2 = np.float32([[100,30],[200,30],[150,100]])
M = cv.getAffineTransform(d1,d2)
i5 = cv.warpAffine(lq,M,(300,300))
# 透视变化
d1 = np.float32([[100,100],[400,100],[100,400],[400,400]]) d2 = np.float32([[200,200],[300,200],[200,300],[300,300]]) d3 = np.float32([[0,0],[500,0],[500,0],[0,500]])
M = cv.getPerspectiveTransform(d1,d2)
i6 = cv.warpPerspective(lq,M,(300,300))
cv.imshow('Original Image', lq)
cv.imshow('Sizing Image', i2)
cv.imshow('Translation Image', i3)
cv.imshow('Rotate Image', i4)
cv.imshow('Tilt Image', i5)
cv.imshow('Perspective change Image', i6)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('E:/4.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] images = [img,thresh1,thresh2,thresh3,thresh4,thresh5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.show()
cv.imshow('Original Image',img)
cv.waitKey(0)
cv.destroyAllWindows()
⾃适应阈值——⽤于解决光照问题
cv.ADAPTIVE_THRESH_MEAN_C:该阈值是平均值的附近区域减去恒定的Ç。cv.ADAPTIVE_THRESH_GAUSSIAN_C:阈值是邻域值减去常数C的⾼斯加权和。 # ⽤于解决光照问题
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('E:/3.png',0)
#常⽤来去除椒盐噪声
#卷积核使⽤奇数
dianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# cv.ADAPTIVE_THRESH_MEAN_C:该阈值是平均值的附近区域减去恒定的Ç。
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
# cv.ADAPTIVE_THRESH_GAUSSIAN_C:阈值是邻域值减去常数C的⾼斯加权和。
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding(v=127)','Adaptive Mean Thresholding','Adaptive Gaussian Thresholding'] images = [img,th1,th2,th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.show()
cv.imshow('medianBlur',blur)
cv.waitKey(0)
cv.destroyAllWindows()
Otsu's Binarization⼆值化
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('E:/5.png',0)
# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)多向指示牌
# plot all the image and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding(v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding" ]
for i in range(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]),icks([]),icks([])
消音片
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]),icks([]), icks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2], 'gray')
plt.title(titles[i*3+2]),icks([]), icks([])
plt.show()
cv.imshow('Original Image',img)
cv.waitKey(0)
cv.destroyAllWindows()
调⽤摄像头