使用直方图进行图像匹配
前言
图像直方图是用来表现图像中亮度分布的直方图,给出的是图像中某个亮度或者某个范围亮度下共有几个像素,即统计一幅图某个亮度像素数量。
提示:以下是本篇文章正文内容,下面案例可供参考
一、直方图匹配(使用函数)
使用cv2.calcHist()、cv2.compareHist()函数
代码如下(示例):
import cv2
import numpy as np
# 读取图像
whole_image = cv2.imread("/Pictures/hawen.jpg")
part_image = cv2.imread("/Pictures/cut_hawen.jpg")
# 将图像转换为灰度图像
gray_whole_image = cv2.cvtColor(whole_image, cv2.COLOR_BGR2GRAY)
gray_part_image = cv2.cvtColor(part_image, cv2.COLOR_BGR2GRAY)
# print(gray_part_image.shape)
# 计算部分图像的直方图
hist_part = cv2.calcHist([gray_part_image], [0], None, [256], [0, 256])
# print(hist_part)
# 获取部分图像的尺寸
part_height, part_width = gray_part_image.shape[:2]
# print(part_height, part_width)
# 获取全部图像的尺寸
whole_height, whole_width = gray_whole_image.shape[:2]
# print(whole_height, whole_width)
best_np = []
best_similarity = -100
x, y = 0, 0
# 循环切割图片
for i in range(whole_height - part_height):
for j in range(whole_width - part_width):
# 切割图片
cut_image = whole_image[i:i + part_height, j:j + part_width]
# 转为灰度
gray_cut_image = cv2.cvtColor(cut_image, cv2.COLOR_BGR2GRAY)
# 计算切割图像的直方图
hist_cut = cv2.calcHist([gray_cut_image], [0], None, [256], [0, 256])
# 使用巴氏距离比较直方图相似度
similarity = cv2.compareHist(hist_part, hist_cut, cv2.HISTCMP_CORREL)
if similarity > best_similarity:
best_similarity = similarity
best_np = cut_image
x = j
y = i
# 画图
cv2.rectangle(whole_image, (x, y), (x + part_width, y + part_height), (0, 255, 0), 1)
# 显示结果
cv2.imshow('Matching Result', whole_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
二、直方图匹配(函数实现版)
自己编写cv2.calcHist()、cv2.compareHist()函数
代码如下(示例):
import cv2
import numpy as np
import math
def calcHist(image):
hist = np.zeros(256)
hist.shape = (256, 1)
for data in image:
# print(data)
for num in data:
hist[num] = hist[num] + 1.0
return hist
def compareHist(h1, h2):
sum1 = 0
sum2 = 0
for data1 in h1:
# print(data)
for num1 in data1:
sum1 = sum1 + num1
avg1 = sum1 / h1.shape[0]
# print(avg1)
for data2 in h2:
# print(data)
for num2 in data2:
sum2 = sum2 + num2
avg2 = sum2 / h2.shape[0]
# print(h1[0][0])
up = 0
for i in range(h1.shape[0]):
up = up + (h1[i][0] - avg1) * (h2[i][0] - avg2)
down = 0
one = 0
two = 0
for i in range(h1.shape[0]):
one = one + (h1[i][0] - avg1) * (h1[i][0] - avg1)
two = two + (h2[i][0] - avg2) * (h2[i][0] - avg2)
re = one * two
down = math.sqrt(re)
return up / down
# 读取图像
whole_image = cv2.imread("/Pictures/hawen.jpg")
part_image = cv2.imread("/Pictures/cut_hawen.jpg")
# 将图像转换为灰度图像
gray_whole_image = cv2.cvtColor(whole_image, cv2.COLOR_BGR2GRAY)
gray_part_image = cv2.cvtColor(part_image, cv2.COLOR_BGR2GRAY)
# 计算部分图像的直方图
hist_part = calcHist([gray_part_image])
# print(hist_part.shape)
# print(type(hist_part))
# 获取部分图像的尺寸
part_height, part_width = gray_part_image.shape[:2]
# print(part_height, part_width)
# 获取全部图像的尺寸
whole_height, whole_width = gray_whole_image.shape[:2]
# print(whole_height, whole_width)
best_np = []
best_similarity = -100
x, y = 0, 0
# 循环切割图片
for i in range(whole_height - part_height):
for j in range(whole_width - part_width):
print(i,j)
# 切割图片
cut_image = whole_image[i:i + part_height, j:j + part_width]
# 转为灰度
gray_cut_image = cv2.cvtColor(cut_image, cv2.COLOR_BGR2GRAY)
# 计算切割图像的直方图
hist_cut = calcHist([gray_cut_image])
# print(hist_cut.shape)
# print(type(hist_cut))
# 使用巴氏距离比较直方图相似度
similarity = compareHist(hist_part, hist_cut)
if similarity > best_similarity:
best_similarity = similarity
best_np = cut_image
x = j
y = i
# 画图
cv2.rectangle(whole_image, (x, y), (x + part_width, y + part_height), (0, 255, 0), 1)
# 显示结果
cv2.imshow('Matching Result', whole_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
总结
以上就是今天要讲的内容,本文仅仅简单介绍了使用直方图进行图像匹配,请各路大佬多多指正!