You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于圆形特征的图像缩放对齐与叠加实现求助(Python)

圆形图像匹配与对齐问题

现有两张图像:一张为模板图像,另一张为用户上传图像,二者尺寸无固定要求,唯一共同特征是均包含一个圆形。需将用户上传图像的圆形调整至与模板图像圆形尺寸一致,通过缩放、平移操作使两图的圆形精准对齐并叠加。尝试多种方式实现圆形尺寸匹配与对齐均未成功,以下为尝试的Python代码:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# 加载图像
before_image = cv2.imread("before.jpg")
after_image = cv2.imread("center_after_2.jpg")
gray_before = cv2.cvtColor(before_image, cv2.COLOR_BGR2GRAY)
gray_after = cv2.cvtColor(after_image, cv2.COLOR_BGR2GRAY)

# 应用降噪处理以简化圆形检测
gray_before = cv2.medianBlur(gray_before, 5)
gray_after = cv2.medianBlur(gray_after, 5)

# 检测图像中的圆形
circles_before = cv2.HoughCircles(gray_before, cv2.HOUGH_GRADIENT, 1, 20, param1=150, param2=30, minRadius=0, maxRadius=0)
circles_after = cv2.HoughCircles(gray_after, cv2.HOUGH_GRADIENT, 1, 30, param1=150, param2=150, minRadius=0, maxRadius=0)

# 提取图像中圆形的x、y坐标及半径
x1, y1, r1 = int(circles_before[0, 0, 0]), int(circles_before[0, 0, 1]), int(circles_before[0, 0, 2])
x2, y2, r2 = int(circles_after[0, 0, 0]), int(circles_after[0, 0, 1]), int(circles_after[0, 0, 2])

before_image = cv2.cvtColor(before_image, cv2.COLOR_BGR2RGB)
cv2.circle(before_image, (x1, y1), r1, (255,0,0), 2)
before_image = cv2.cvtColor(before_image, cv2.COLOR_BGR2RGB)
cv2.circle(after_image, (x2, y2), r2, (255,0,0), 2)

plt.imshow(before_image)
plt.show()

plt.imshow(after_image)
plt.show()

相关图像

  • 模板与用户图像对比图:before_image and after_image
  • 模板图像:before_image
  • 用户图像:center_after_2

内容的提问来源于stack exchange,提问作者Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 15:55:15