OpenCV - "Histogram Operation" and "Template Matching"

1. Histogram equalization

img = cv2.imread('clahe.jpg',0)
plt.hist(img.ravel(),256)
plt.show()
#旨在使得图像整体效果均匀,黑与白之间的各个像素级之间的点更均匀一点。
equ = cv2.equalizeHist(img)
plt.hist(equ.ravel(),256)
plt.show()
#进行对比,均值化之后的,没有均值化之后的。
res = np.hstack((img,equ))
cv_show('res',res)

The fish after histogram equalization has not been equalized for comparison.
Please add a picture description
This is not equalized.
Please add a picture description
This is obtained after equalization.
Please add a picture description

2. Adaptive histogram equalization

#============自适应直方图均衡化============
#clipLimit:颜色对比度的阈值,可选项,默认值 8
#titleGridSize:局部直方图均衡化的模板(邻域)大小,可选项,默认值 (8,8)
clache = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
res_clahe = clache.apply(img)
res = np.hstack((img,equ,res_clahe))#进行水平叠加
cv_show('res',res)

Effect comparison chart
Please add a picture description

3. Template matching

img = cv2.imread('lena.jpg',0)
template = cv2.imread('face.jpg',0)
h,w, = template.shape
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
           'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF)
#找出矩阵中最大值和最小值,即其对应的(x, y)的位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
for meth in methods:
    img2 = img.copy()

    #匹配方法的真值
    method = eval(meth)
    print(method)
    res = cv2.matchTemplate(img, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # 如果是平方差匹配TM_SQDIFF或归一化平方差匹配TM_SQDIFF_NORMED,取最小值
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right =  (top_left[0]+w,top_left[1]+h)

    #画矩形
    cv2.rectangle(img2,top_left,bottom_right,255,2)
    plt.subplot(121), plt.imshow(res, cmap='gray')
    plt.xticks([]), plt.yticks([])  # 隐藏坐标轴
    plt.subplot(122), plt.imshow(img2, cmap='gray')
    plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()

Parameter meaning:
image: source image, the image to be matched, 8bit integer type, 32bit floating point type, can be single channel or multi-channel; templ
: template image, the same type of image, the size must be smaller than the source image;
method: matching method;
mask: mask;
result: return result, 32bit floating point type, the source image is W×H, the template image is w×h, and the generated image object is (W−w+1)×(H−h+1); The result returned by it can be understood in disguise as a

cv2.rectangle(img2, top_left, bottom_right, 255, 2) draws this rectangle on the original image, and the coordinate axes on the original image are displayed.
Please add a picture description

Please add a picture description

4. Multi-template matching

img_rgb = cv2.imread('mario.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('mario_coin.jpg', 0)
h, w = template.shape[:2]

res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
# 取匹配程度大于%80的坐标
loc = np.where(res >= threshold)#返回的是索引
for pt in zip(*loc[::-1]):  # *号表示可选参数
    bottom_right = (pt[0] + w, pt[1] + h)
    cv2.rectangle(img_rgb, pt, bottom_right, (0, 0, 255), 2)

cv2.imshow('img_rgb', img_rgb)
cv2.waitKey(0)

Please add a picture description

Reference link:
https://blog.csdn.net/qq_29023939/article/details/81023062
https://blog.csdn.net/sinat_41104353/article/details/85171185

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/131324925
Recommended