处理超高分辨率图像(例如9000x7000像素)并从中提取特定形状(例如圆形)是图像处理和计算机视觉中的常见挑战。本文提供一种使用Python和OpenCV库的解决方案,高效准确地提取目标圆形区域。
现有代码存在的问题是:检测到的圆形过多,无法精确选取所需的两处圆形区域。 为了改进,我们将采用以下策略:
import cv2 import numpy as np image_path = r"c:\users\17607\desktop\smls pictures\pic_20231122151507973.bmp" # 读取图像 img = cv2.imread(image_path) # 缩放图像 (调整缩放比例根据实际情况) scale_percent = 10 # 缩放至原图的1/10 width = int(img.shape[1] / scale_percent) height = int(img.shape[0] / scale_percent) dim = (width, height) resized_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) # 灰度转换 gray = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) # 高斯模糊 blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Canny边缘检测 edges = cv2.Canny(blurred, 50, 150)
# 霍夫圆变换 circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 40, param1=50, param2=30, minRadius=0, maxRadius=0) if circles is not None: circles = np.uint16(np.around(circles)) # 选择两个最大的圆 circles = circles[0, :] circles = circles[np.argsort(circles[:, 2])[::-1][:2]] # 选择半径最大的两个圆 for i in circles: center_x, center_y, radius = i # 在缩放后的图像上绘制圆形 cv2.circle(resized_img, (center_x, center_y), radius, (0, 0, 255), 2) cv2.circle(resized_img, (center_x, center_y), 2, (255, 0, 0), 3) cv2.imshow("Detected Circles", resized_img) cv2.waitKey(0) cv2.destroyAllWindows()
通过以上步骤,我们可以有效地从高分辨率图像中提取出两个最大的圆形区域,并通过可视化结果进行验证。 需要注意的是,scale_percent 和霍夫变换的参数需要根据实际图像进行调整,以达到最佳的检测效果。 如果两个圆形大小相近,可能需要根据圆心坐标或其他特征进行更精细的选择。
以上就是如何使用Python和OpenCV从9000x7000像素的图片中提取两个圆形区域?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号