文章目录
一、模板匹配
1.1 概念
**模板匹配:**就是在整个图像区域发现与给与子图像匹配的小块区域。所以模板匹配首先需要一个模板图像T(给与的子图像),另外还需要一个待检测图像-原图像s。
工作方法: 在待检测图像上,从左到右,从上向下计算模板图像与重叠子图像的匹配度,匹配程度越大,两者相同的可能性越大。
1.2 实战cv.matchTemplate
cv.matchTemplate(图片,模板,匹配方法)
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def template_demo():
target=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
tpl=cv.imread(r'D:\Project\Opencv\Learning01\muban.png')
cv.imshow('template',tpl)
cv.imshow('target',target)
'''三种匹配时的度量方法'''
method=[cv.TM_SQDIFF_NORMED,cv.TM_CCORR_NORMED,cv.TM_CCOEFF_NORMED]
th,tw=tpl.shape[0:2]
for md in method:
print(md)
'''模板匹配'''
result=cv.matchTemplate(target,tpl,md)#原图像、模板、匹配方法
min_val,max_val,min_loc,max_loc=cv.minMaxLoc(result)
if md==cv.TM_SQDIFF_NORMED:
tl=min_loc
else:
tl=max_loc
br=(tl[0]+tw,tl[1]+th)
cv.rectangle(target,tl,br,(0,0,255),2)
cv.imshow('result'+str(md),target)
template_demo()
cv.waitKey(10000)
二、图像二值化
2.1 概念
二值图像:
图像中只有两种颜色[0,1]。灰度图像是[0,255]彩色图像是[[0,255],[0,255][0,255]。
2.2 实战
二值化之前要先把图片变为灰度图
2.2.1 全局阈值cv.threshold
ret,binary=cv.threshold()
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
''''''
def threshold_demo():
''''''
target=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
"""变为灰度图像"""
gray=cv.cvtColor(target,cv.COLOR_BGR2GRAY)
"""变为二值图像"""
#########方法1:此时cv.THRESH_OTSU计算阈值,0,255作废
ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)#参数:原图像,0,255,二值化方法
cv.imshow('origin', target)
print('yuzhi:',ret)#自动计算的分割阈值
cv.imshow('binary',binary)
# #########方法2,自动全局阈值,此时cv.THRESH_TRIANGLE计算阈值,0,255作废
# ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
# cv.imshow('origin', target)
# print('yuzhi:', ret) # 自动计算的分割阈值
# cv.imshow('binary', binary)
# #########方法3,自动指定阈值,小于127是黑色,大于127是白色
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY )
# cv.imshow('origin', target)
# print('yuzhi:', ret) # 自动计算的分割阈值
# cv.imshow('binary', binary)
# #########方法4.取反.大于127是黑色,小于127是白色
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY_INV)
# cv.imshow('origin', target)
# print('yuzhi:', ret) # 自动计算的分割阈值
# cv.imshow('binary', binary)
# #########方法5.截断阈值,大于127变成127,
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_TRUNC)
# cv.imshow('origin', target)
# print('yuzhi:', ret) # 自动计算的分割阈值
# cv.imshow('binary', binary)
# #########方法6.小于127变为0
# ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_TOZERO)
# cv.imshow('origin', target)
# print('yuzhi:', ret) # 自动计算的分割阈值
# cv.imshow('binary', binary)
threshold_demo()
cv.waitKey(10000)
2.2.2 局部(自适应)阈值cv.adaptiveThreshold
binary=cv.adaptiveThreshold()
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
''''''
def local_threshold():
''''''
target=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
"""变为灰度图像"""
gray=cv.cvtColor(target,cv.COLOR_BGR2GRAY)
"""变为二值图像"""
#########自适应阈值
###方法1:cv.ADAPTIVE_THRESH_MEAN_C
dst=cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,25,10)
cv.imshow('target', target)
cv.imshow('binary',dst)
###方法2:cv.ADAPTIVE_THRESH_GAUSSIAN_C
# dst=cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,25,10)
# cv.imshow('target', target)
# cv.imshow('binary',dst)
local_threshold()
cv.waitKey(10000)
2.2.3 自定义阈值cv.threshold
ret,binary=cv.threshold()
例使用均值阈值
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
''''''
def custom_threshold():
''''''
target=cv.imread(r'D:\Project\Opencv\Learning01\angelababy.jpg')
"""变为灰度图像"""
gray=cv.cvtColor(target,cv.COLOR_BGR2GRAY)
"""变为二值图像"""
h,w=gray.shape[0:2]
m=np.reshape(gray,[1,w*h])
mean=m.sum()/(w*h)
print('mean',mean)
ret,binary=cv.threshold(gray,mean,255,cv.THRESH_BINARY)
cv.imshow('target',target)
cv.imshow('binary',binary)
custom_threshold()
cv.waitKey(10000)
三、超大图像二值化
二值化:全局阈值、局部阈值、自定义阈值。对于超大图像,通常先图像分块,再局部二值化(常用)
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
''''''
def big_image_binary():
''''''
big_img=cv.imread(r'D:\Project\Opencv\Learning01\bigimage.png')
print(big_img.shape)
#设置分成的小块的大小
cw=125
ch=125
h,w=big_img.shape[0:2]
gray=cv.cvtColor(big_img,cv.COLOR_BGR2GRAY)#转灰度图
for row in range(0,h,ch):
for col in range(0,w,cw):
roi=gray[row:row+ch,col:col+col+cw]#得到一小块图片
"""计算小块图像的二值化的结果"""
dst=cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20)
"""图像块二值化的结果赋值到原图"""
gray[row:row+ch,col:col+col+cw]=dst
print(np.std(dst),np.mean(dst))
cv.imshow('big binary',gray)
big_image_binary()
cv.waitKey(10000)