【笔记】出自【强推】B站公认讲的最好的openCV计算机视觉教程的openCV函数汇总3,视频P23-P35

# 高斯金字塔:向上采样(放大)填充0,放大再缩小会损失一些信息,图像比原图像更模糊
# 向下采样(将这张图片缩小)
down = cv2.pyrDown(img)
# 向上采样(将这张图片放大)
up = cv2.pyrUp(img)

# 拉普拉斯金字塔
# 1.低通滤波 2.缩小图像 3.再放大图像 4.原始图像-得到的图像
down = cv2.pyrDown(img)
down_up = cv2.pyrUp(down)
res = img - down_up

# 轮廓检测
# 1.原图像转灰度图像 2.再转二值图像 3.进行检测
img22 = cv2.imread("D:/pythonProject1/image/22.png")
gray_img22 = cv2.imread("D:/pythonProject1/image/22.png", cv2.IMREAD_GRAYSCALE)
ret, thresh = cv2.threshold(gray_img22, 127, 255, cv2.THRESH_BINARY)
contours, hierary= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#  在原图上绘制轮廓,原图,轮廓, 轮廓索引, 颜色模式, 线条厚度
res = cv2.drawContours(img22, contours, -1, (0, 0, 255), 2 )

# 轮廓近似
b, c= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = b[2]
epsilon = 0.01*cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
draw_img = img22.copy()
red = cv2.drawContours(draw_img, [approx],-1, (0, 0, 255), 2)

# 画轮廓矩形
b, c= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = b[2]
draw_img = img22.copy()
x, y, w, h = cv2.boundingRect(cnt)
ange = cv2.rectangle(draw_img,(x, y),(x+w,y+h),(0,255,0),2)
ress = cv2.drawContours(draw_img,[cnt],-1,(0, 0, 255), 2)

# 模板匹配,t是原图,tt是特征图
t = cv2.imread('D:/pythonProject1/image/lenaNoise.png',cv2.IMREAD_GRAYSCALE)
tt = cv2.imread('D:/pythonProject1/image/img.png',cv2.IMREAD_GRAYSCALE)
h, w = tt.shape[:2]
print(t.shape)
print(tt.shape)
# 进行模板匹配,得到最小位置
res = cv2.matchTemplate(t, tt, cv2.TM_SQDIFF)
print(res.shape)
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(res)
print(minVal, maxVal, minLoc, maxLoc)
top = minLoc
bottom = (top[0] + w,top[1]+h)
res = cv2.rectangle(t,top,bottom,255,2)
cv_show('res',res)

# 统计图像所有相同像素值的数量的直方图
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.hist(img.ravel(), 256)
plt.show()
# 统计BRG通道图像所有相同像素值的数量的折线图
color = ('b', 'g', 'r')
for i, col in enumerate(color):
    histr = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(histr, color = col)
    plt.xlim([0, 256])
plt.show()

# 截图:1.全变黑 2.截取的位置为白,3.与原图像拼接
mask = np.zeros(gray_img.shape[:2], np.uint8)
mask[40:200,100:260] = 255
mask_img = cv2.bitwise_and(gray_img, gray_img, mask=mask)

# 像素均衡化
equ = cv2.equalizeHist(gray_img)

# 傅里叶变换
img_float32 = np.float32(gray_img)
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude = 20*np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
plt.subplot(122), plt.imshow(magnitude, cmap='gray')
plt.show()
img_float32 = np.float32(gray_img)
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

rows, cols = gray_img.shape
crow, ccol = int(rows/2), int(cols/2)
# 低通滤波
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
# 高通滤波
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
# IDFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.show()











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值