此次学习的是csdn学院中《Python+OpenCV计算机视觉》,比较基础,适合初学者。
第一次尝试以博客形式来记录学习笔记,督促自己,提升自我,供自己和大家学习。
目录
图像的读取、显示和保存
图像处理基础
图像运算
类型转换
几何变换
图像阈值
图像平滑处理
形态学处理
图像梯度
Canny边缘检测
图像金字塔
图像轮廓
直方图
傅里叶变换
总结
None = cv2.imshow(窗口名,图像名)
retval = cv2.waitKey(delay) #delay>0 : 等待delay毫秒 delay<0 : 等待键盘单击 delay=0 : 无限等待
cv2.destroyAllWindows() # 删除所有窗口
范例:
import cv2
i = cv2.imread(".\he.jpg")
cv2.imshow("he",i)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(".\me.jpg", i)
返回值 = 图像(位置参数)
灰度图像,返回灰度值
BGR图像,返回值为B、G、R的值
范例:
import cv2
i = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
print(i[100,100])
i[100,100] = [255,255,255]
print(i[100,100])
范例:
import cv2
import numpy
i = cv2.imread(".\me.bmp",cv2.IMREAD_UNCHANGED)
print(i.item(33,33,0))
i.itemset((33,33,0),255)
print(i.item(33,33,0))
import cv2
img1 = cv2.imread(".\he.jpg",cv2.THRESH_BINARY)
print(img1.shape)
##(336, 449)
import cv2
img2 = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
print(img2.shape)
##(336, 449, 3)
import cv2
img1 = cv2.imread(".\he.jpg",cv2.THRESH_BINARY)
print(img1.size)
##150864
彩色 返回:行数*列数*通道数
import cv2
img2 = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
print(img2.size)
##452592
import cv2
img = cv2.imread(".\he.jpg",cv2.THRESH_BINARY)
print(img.dtype)
##uint8
ROI(region of interest),感兴趣区域。
从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区。
可以通过各种算子(Operator)和函数来求得感兴趣区域ROI,并进行图像的下一步处理。
范例:
import cv2
import numpy as np
a = cv2.imread(".\he.jpg")
b = np.ones((101,101,3))
b = a[100:300,200:400]
c = a
c[0:200,0:200] = b
cv2.imshow("original",a)
cv2.imshow("face",b)
cv2.imshow("face2",c)
cv2.waitKey(0)
cv2.destroyAllWindows()
拆分通道
合并
范例:
import cv2
import numpy
img = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
b1, g1, r1 = cv2.split(img)
m = cv2.merge([b,g,r])
cv2.imshow("b",b)
cv2.imshow("b1",b1)
cv2.imshow("m",m)
cv2.waitKey()
cv2.destroyAllWindows()
注意的问题:参与运算的图像大小、类型必须一致
范例:
import cv2
import numpy
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = a
result1 = a + b
result2 = cv2.add(a,b)
cv2.imshow("original",a)
cv2.imshow("result1",result1)
cv2.imshow("result2",result2)
cv2.waitKey()
cv2.destroyAllWindows()
将2张或2张以上的图像信息的融合到1张图像上
融合的图像含有更多的信息、能够更方便人来观察或者计算机处理
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = a
result3 = cv2.addWeighted(a,1,b,1,0)
cv2.imshow("original",a)
cv2.imshow("result3",result3)
cv2.waitKey()
cv2.destroyAllWindows()
OpenCV提供了200多种不同类型之间的转换
范例:彩色变灰度
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
rows,cols = a.shape[:2]
b = cv2.resize(a, (round(cols*0.5),round(rows*0.5)))
cv2.imshow("original",a)
cv2.imshow("resize",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.flip(a,0)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
threshold函数
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
r, b = cv2.threshold(a, 177, 255, cv2.THRESH_BINARY)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.blur(a, (5, 5))
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.boxFilter(a, -1, (5, 5), normalize=1)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.GaussianBlur(a,(3,3),0)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
a = cv2.imread(".\he.jpg",cv2.IMREAD_UNCHANGED)
b = cv2.medianBlur(a,3)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\erode.jpg",0)
kernel = np.ones((50,50),np.uint8)
b = cv2.erode(a,kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\erode.jpg",0)
kernel = np.ones((50,50),np.uint8)
b = cv2.dilate(a,kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\opening.jpg",0)
kernel = np.ones((5,5),np.uint8)
b = cv2.morphologyEx(a, cv2.MORPH_OPEN, kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\closing.jpg",0)
kernel = np.ones((5,5),np.uint8)
b = cv2.morphologyEx(a, cv2.MORPH_CLOSE, kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\gradient.jpg",0)
kernel = np.ones((5,5),np.uint8)
b = cv2.morphologyEx(a, cv2.MORPH_GRADIENT, kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\tophat.jpg",0)
kernel = np.ones((5,5),np.uint8)
b = cv2.morphologyEx(a, cv2.MORPH_TOPHAT, kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\blackhat.jpg",0)
kernel = np.ones((5,5),np.uint8)
b = cv2.morphologyEx(a, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
b = cv2.Sobel(a, cv2.CV_64F, 1, 0)
b = cv2.convertScaleAbs(b) # 转回uint8
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
scharrx = cv2.Scharr(a, cv2.CV_64F, 1, 0)
scharry = cv2.Scharr(a, cv2.CV_64F, 0, 1)
scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0)
b = cv2.convertScaleAbs(scharrxy) # 转回uint8
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
b = cv2.Laplacian(a, cv2.CV_64F)
b = cv2.convertScaleAbs(b) # 转回uint8
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
去噪-梯度-非极大值抑制-滞后阈值
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
b = cv2.Canny(a, 100, 200 )
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
b = cv2.pyrDown(a)
b1 = cv2.pyrDown(b)
b2 = cv2.pyrDown(b1)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.imshow("b1",b1)
cv2.imshow("b2",b2)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
b = cv2.pyrUp(a)
b1 = cv2.pyrUp(b)
b2 = cv2.pyrUp(b1)
cv2.imshow("original",a)
cv2.imshow("b",b)
cv2.imshow("b1",b1)
cv2.imshow("b2",b2)
cv2.waitKey()
cv2.destroyAllWindows()
范例:
import cv2
import numpy as np
G0 = cv2.imread(".\yuan.jpg",0)
G1 = cv2.pyrDown(G0)
L0 = G0 - cv2.pyrUp(cv2.pyrDown(G0))
L1 = G1 - cv2.pyrUp(cv2.pyrDown(G1))
cv2.imshow("original",G0)
cv2.imshow("L0",L0)
cv2.imshow("L1",L1)
cv2.waitKey()
cv2.destroyAllWindows()
#报错
# L1 = G1 - cv2.pyrUp(cv2.pyrDown(G1))
#ValueError: operands could not be broadcast together with shapes (257,274) (258,274)
#说是图像大小出错,但是不太明白哪出错了???
范例:
import cv2
import numpy as np
a = cv2.imread(".\yuan.jpg",0)
gray = cv2.cvtColor(a,cv2.COLOR_BGR2RGB)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
r = cv2.drawContours(a, contours, -1, (0,0,255), 1)
cv2.imshow("original",a)
cv2.imshow("r",r)
cv2.waitKey()
cv2.destroyAllWindows()
#报错
#又不知哪错了???
范例:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg")
plt.hist(img.ravel(),256)
plt.show()
范例:
import cv2
import numpy as np
img = cv2.imread(".\he.jpg")
hist = cv2.calcHist([img],[0],None,[256],[0,255])
print(type(hist))
print(hist.size)
print(hist.shape)
#
#256
#(256, 1)
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg")
hist = cv2.calcHist([img],[0],None,[256],[0,255])
plt.plot(hist,color = 'b')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
mask = np.zeros(img.shape,np.uint8)
mask[200:400,200:400] = 255
hist1 = cv2.calcHist([img],[0],None,[256],[0,255])
hist2 = cv2.calcHist([img],[0],mask,[256],[0,255])
plt.plot(hist1,color='r')
plt.plot(hist2,color='b')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
equ = cv2.equalizeHist(img)
plt.figure()
plt.hist(img.ravel(),256)
plt.figure()
plt.hist(equ.ravel(),256)
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
result = 20*np.log(np.abs(fshift))
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(result,cmap='gray')
plt.title('result')
plt.axis('off')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg,cmap='gray')
plt.title('result')
plt.axis('off')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
rows,cols = img.shape
crow,ccol = int(rows/2),int(cols/2)
fshift[crow-30:crow+30,ccol-30:ccol+30] = 0
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg,cmap='gray')
plt.title('result')
plt.axis('off')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
dft = cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
dftshift = np.fft.fftshift(dft)
result = 20*np.log(cv2.magnitude(dftshift[:,:,0],dftshift[:,:,1]))
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(result,cmap='gray')
plt.title('result')
plt.axis('off')
plt.show()
范例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(".\he.jpg",cv2.IMREAD_GRAYSCALE)
dft = cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
dftshift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftshift)
iimg = cv2.idft(ishift)
iimg = cv2.magnitude(iimg[:,:,0],iimg[:,:,1])
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg,cmap='gray')
plt.title('iimg')
plt.axis('off')
plt.show()
以前学习过数字图像处理,所以对基本的图像处理知识还算了解,这次学习主要是为了熟悉python语言、OpenCV语言。
用博客记录学习的方式,既可以督促自己,又可以为以后复习,这种形式可以继续。
下面开始认真做毕设,争取在中期检查前把系统构建出来,然后在查找文献,改进算法。