Plot digitizer for single datapoint Python 单数据点 Python 绘图数字化工具

huangapple go评论74阅读模式
英文:

Plot digitizer for single datapoint Python

问题

我需要分析这900帧图像,这些图像显示了小纸板片与极坐标之间的角度。有没有可以在Python中自动化这个过程的库?我想要将程序提供给文件夹目录,并且它能够返回每个帧中的所有角度的列表。Plot digitizer for single datapoint Python
单数据点 Python 绘图数字化工具

英文:

I need to analyse 900 of these frames, that is giving the angle of the little cardboard piece with respect to the polar. Is there any library that I can use to automate the process in python? I would like to feed the program with the folder directory and that it would give back a list with all the angles in each frame.Plot digitizer for single datapoint Python
单数据点 Python 绘图数字化工具

答案1

得分: 1

OpenCV是图像处理的强大工具。

我尝试为您的问题提出解决方案,但我不知道这个解决方案在900张图像上的稳定性如何。特别是,0° - 180°轴不能显著倾斜,因为我没有对此进行校正。

import cv2 as cv
import numpy as np

#加载图像
img = cv.imread(r'path_to_your_image.jpg', cv.IMREAD_GRAYSCALE)

#检测圆
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 100,
                               param1=100, param2=30,
                               minRadius=50, maxRadius=500)
# 获取圆心(如果未检测到圆,会引发异常)
try:
    center = (int(circles[0,0,0]), int(circles[0,0,1]))
except IndexError:
    raise Exception("无法识别中心。")
# 绘制圆心
cv.circle(img, center, 1, (0, 100, 100), 3)

# 膨胀和二值化图像,只看到矩形作为小点
kernel = np.ones((35, 35), np.uint8)
img_dilate = cv.dilate(img, kernel, iterations=1)
_, img_dilate_thres = cv.threshold(img_dilate,120,255,cv.THRESH_BINARY)
# 获取剩余点的中心位置
rect_x = np.argmin(img_dilate_thres.mean(axis=0))
rect_y = np.argmin(img_dilate_thres.mean(axis=1))
cv.circle(img, (rect_x, rect_y), 1, (0, 100, 100), 3)

# 获取圆心和补丁之间的角度
angle = np.arctan2(center[1] - rect_y, rect_x - center[0])
degree = angle / (2*np.pi) * 360

# 显示角度,等待用户关闭图像
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img, f'{degree:.1f}', (rect_x, rect_y), font, 2, (0, 255, 0), 2, cv.LINE_AA)
cv.imshow("检测到的圆", img)
cv.waitKey(0)
英文:

OpenCV is a powerful tool for image manipulation.

I've tried to come up with a solution for you problem, but I do not know how stable this solution is across the 900 images. In particular, the 0° - 180° axis must not be significantly tilted, as I am not correcting for that.

Plot digitizer for single datapoint Python
单数据点 Python 绘图数字化工具

import cv2 as cv
import numpy as np

#load image
img = cv.imread(r'path_to_your_image.jpg', cv.IMREAD_GRAYSCALE)

#detect circle
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 100,
                               param1=100, param2=30,
                               minRadius=50, maxRadius=500)
# get circle center (raise Exception if circle was not detected)
try:
    center = (int(circles[0,0,0]), int(circles[0,0,1]))
except IndexError:
    raise Exception("Unable to identify center.")
# plot center of circle
cv.circle(img, center, 1, (0, 100, 100), 3)

# dilate and threshold image to only see the rectangle as small dot
kernel = np.ones((35, 35), np.uint8)
img_dilate = cv.dilate(img, kernel, iterations=1)
_, img_dilate_thres = cv.threshold(img_dilate,120,255,cv.THRESH_BINARY)
# get center position of remaining dot
rect_x = np.argmin(img_dilate_thres.mean(axis=0))
rect_y = np.argmin(img_dilate_thres.mean(axis=1))
cv.circle(img, (rect_x, rect_y), 1, (0, 100, 100), 3)

# get angle between circle center and patch
angle = np.arctan2(center[1] - rect_y, rect_x - center[0])
degree = angle / (2*np.pi) * 360

# display angle, wait for user to close image
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img, f'{degree:.1f}', (rect_x, rect_y), font, 2, (0, 255, 0), 2, cv.LINE_AA)
cv.imshow("detected circles", img)
cv.waitKey(0)

huangapple
  • 本文由 发表于 2023年5月10日 22:07:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定