如何分析两张图片的对应关系?

一. 情景引入

当我们要分析两张图片的关系,或者两张图片中特定区域的对应关系时,可用该方法。
可视化效果图如下:在这里插入图片描述

二. 原理介绍与代码实现

原理:把两张图的像素映射到范围为[0,1]的区间,以第一张图的像素值为x坐标,另一张图的像素值为y坐标,从而组成平面中的点,将这些点显示在幕布中就是上面的效果。由于图片一般有三个通道(BGR),所以用三种不同颜色的点表示,三种颜色的点表示每个通道的关系。
我们分析下面两张图的关系:
在这里插入图片描述
在这里插入图片描述

import math
import cv2
import numpy as np


def draw_axis(image):
    white_color = (255, 255, 255)
    w, h = image.shape[1], image.shape[0]
    cv2.line(image, (0, 0), (0, h), white_color, thickness=3)
    cv2.line(image, (0, h), (w, h), white_color, thickness=3)
    axis_height = 8
    xs = (np.linspace(0, 1, 11) * (w - 1)).astype(np.int32)
    for x in xs:
        cv2.line(image, (x, h-1), (x, h-1-axis_height), white_color, thickness=3)

    ys = (np.linspace(0, 1, 11) * (h - 1)).astype(np.int32)
    for y in ys:
        cv2.line(image, (0, y), (axis_height, y), white_color, thickness=3)
    return image


def draw_coord(image, x, y, show_cn=None):
    if show_cn is None:
        show_cn = [0, 1, 2]
    elif not isinstance(show_cn,(tuple,list)):
        show_cn = [show_cn]

    if x.ndim == 3:
        x = np.reshape(x, (-1, x.shape[-1]))

    if y.ndim == 3:
        y = np.reshape(y, (-1, y.shape[-1]))

    bgr_colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
    min_color = 80
    max_color = 255

    w, h = image.shape[1], image.shape[0]
    image = draw_axis(image)
    for cn in show_cn:
        points = {}
        for px, py in zip(x[:, cn], y[:, cn]):
            point = (int(round(px * (w-1))), int(round((1 - py) * (h-1))))
            points.setdefault(point, 0)
            points[point] += 1
        point_nums = [num for num in points.values()]
        min_point_nums = np.min(point_nums)
        max_point_nums = np.max(point_nums)
        for point, num in points.items():
            color = min_color + (max_color - min_color) * (num - min_point_nums) / (max_point_nums - min_point_nums)
            color = np.array(bgr_colors)[cn] * color
            color = np.clip(color, 0, 255)
            cv2.circle(image, point, 1, color.tolist(), -1)

    cv2.imwrite("analysis_result.jpg", image)


if __name__ == "__main__":
    img1 = cv2.imread("img1.png")
    img1 = cv2.resize(img1, (512, 512))

    img2 = cv2.imread("img2.png")
    img2 = cv2.resize(img2, (512,512))

    x = img1.astype(np.float32) / 255
    y = img2.astype(np.float32) / 255
    coord_img = np.zeros_like(img1)
    draw_coord(coord_img, x, y)

运行上面的代码后,就会生成上面的分析结果图片。

每天进步一点,关注博主,下期更精彩!!!

OpenCV是一个强大的计算机视觉库,可以用于图像处理和分析,包括物体检测、追踪等任务。如果你想检测两张图片之间的位移,你可以使用OpenCV中的模板匹配技术或者特征匹配方法。 1. **模板匹配**:如果你想要精确测量两个相似区域的位置变化,可以先选择一张图片作为模板,然后在第二张图片中搜索该模板。OpenCV的`matchTemplate()`函数可以完成这个工作,它会返回每个位置上模板和原图匹配度的信息,通过查找峰值(通常是最高匹配值),可以找到最佳匹配位置,从而计算出位移。 ```python import cv2 template = cv2.imread('template.jpg', 0) # 模板图片,灰度化处理 search_img = cv2.imread('search_img.jpg', 0) res = cv2.matchTemplate(search_img, template, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) top_left = max_loc bottom_right = (max_loc[0] + template.shape[1], max_loc[1] + template.shape[0]) ``` 位移就是`(bottom_right - top_left)`。 2. **特征匹配**:另一种方法是利用SIFT、SURF、ORB或其他特征点提取算法来识别图像中的关键点,并计算它们在两张图片中的对应关系。如ORB特征匹配: ```python orb = cv2.ORB_create() kp1, des1 = orb.detectAndCompute(template, None) kp2, des2 = orb.detectAndCompute(search_img, None) bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) good_matches = [m for m in matches if m.distance < threshold] ``` 通过匹配的特征点对,可以计算特征点的坐标差异来得到位移。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值