如何将二维环表面的点映射到矩形

发布于 2025-01-13 00:07:07 字数 429 浏览 0 评论 0原文

输入图片此处描述

我的问题是我有一条线(左侧红色),我想对其进行转换以得到如右图所示的内容。 (这是一个二值化图像,所以一切都是黑色的,除了这里的(红色)线,它在图片上是白色的。)黑点象征着灰色圆圈的中心。所以很明显,红线的点不能适合1个圆的边缘,但存在2个具有r1,r2(半径)的圆,它们一起创建一个环,其表面(图中的灰色)可以包含所有红线的点。对于不同的图片,r1和r2可以不同,但​​保证r2>r1>0。

所以我需要一个映射,它可以将红线的点(x,y)(numpy矩阵)从灰色环转换为矩形。 “没有公式”我会想象这就像在环的某处切割,然后将其折叠成矩形。

enter image description here

My problem is that I have a line (red on the left), and I want to transform it to get something like on the right picture. (This is a binarized image, so everything is black, except the (red) line here which is white on the picture.) The black dot simbolizes the center of the gray circle. So it is obvious, that the points of the red line can not fit to the edge of 1 circle, but exist 2 circles with r1, r2 (radius) which together create a ring and its surface(gray on the picture) can contain all of the points of the red line. For different pictures r1 and r2 can be different, but it is guaranteed that r2>r1>0.

So I need a mapping, which can transform the points(x,y) (numpy matrix) of the red line from the gray ring to a rectangle.
"Without formulas" I would imagine this like cutting somewhere the ring, and folding it into a rectangle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2025-01-20 00:07:07

正如@fmw42 敏锐指出的,您可以使用cv2.warpPolar。这是一个例子。

import math

import cv2

ring = cv2.imread("so71416458-crop.png")
size = ring.shape[0]  # assumes square image

outer_radius = size // 2
inner_radius_factor = 0.7  # 0.70 measured empirically from image

# Unwarp ring
warped = cv2.warpPolar(ring, (size, int(size * math.pi)), (outer_radius, outer_radius), outer_radius, 0)
# Rotate 90 degrees
straightened = cv2.rotate(warped, cv2.ROTATE_90_COUNTERCLOCKWISE)
# Crop to ring only
cropped = straightened[: int(straightened.shape[0] * (1 - inner_radius_factor)), :]

cv2.imwrite("so71416458-straight.png", cropped)

输入:so71416458-crop.png

在此处输入图像描述

这里明显的“摆动”是我草率裁剪的结果 - 环的中心点并不完全位于图像的中点。

输出:so71416458-straight.png

在此处输入图像描述

As astutely pointed out by @fmw42, you can use cv2.warpPolar. Here's an example.

import math

import cv2

ring = cv2.imread("so71416458-crop.png")
size = ring.shape[0]  # assumes square image

outer_radius = size // 2
inner_radius_factor = 0.7  # 0.70 measured empirically from image

# Unwarp ring
warped = cv2.warpPolar(ring, (size, int(size * math.pi)), (outer_radius, outer_radius), outer_radius, 0)
# Rotate 90 degrees
straightened = cv2.rotate(warped, cv2.ROTATE_90_COUNTERCLOCKWISE)
# Crop to ring only
cropped = straightened[: int(straightened.shape[0] * (1 - inner_radius_factor)), :]

cv2.imwrite("so71416458-straight.png", cropped)

input: so71416458-crop.png

enter image description here

The "wobble" evident in here is a result of my sloppy cropping – the center point of the ring isn't exactly at the midpoint of the image.

output: so71416458-straight.png

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文