如何使用 OpenCV Python 绘制一组点

发布于 2025-01-16 21:45:47 字数 1156 浏览 0 评论 0原文

我有一组由数学表达式生成的点(坐标 X 和 Y),我想在屏幕的特定位置绘制结果图形(我想确定绘制图形的中心位置) )。

我尝试使用以下代码来测试公式是否得出正确的数字。但现在我需要在特定位置的预先存在的图像上绘制相同的轮廓。

B = 185
L = 250
W = (L-B)/6
D = (L/2)-L/4

x = np.linspace(-L/2, L/2, 500)
y1 = []
y2 = []

for X in x:
    termo1 = sqrt((L**2 - 4*X**2) / (L**2 + 8*W*X + 4*W**2))
    termo2 = ((sqrt(5.5*L**2 + 11*L*W + 4*W**2) * (sqrt(3)*B*B - 2*D*sqrt(L**2 + 2*W*L + 4*W**2))
               ) / (sqrt(3)*B*L*(sqrt(5.5*L**2 + 11*L*W + 4*W**2) - 2*sqrt(L**2 + 2*W*L + 4*W**2))))
    termo3 = 1 - sqrt((L*(L**2 + 8*W*X + 4*W**2)) / (2*(L - 2*W)*X**2 +
                      (L**2 + 8*L*W - 4*W**2)*X + 2*L*W**2 + L**2*W + L**2*W + L**3))

    calculo = B/2 * termo1 * (1-termo2 * termo3)
    y1.append(calculo)

    calculo = -B/2 * termo1 * (1-termo2 * termo3)
    y2.append(calculo)



fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.plot(x, y1, 'r')
plt.plot(x, y2, 'r')


plt.show()

I've got a set of points (coordinates X and Y) generated by a mathematical expression and I want to draw the resulting figure in a specific position of the screen (I'd like to determine the position in which to center the drawn figure).

I tried using the following code to test if the formula resulted in the correct figure. But now I need to draw the same contour on a pre-existing image at a specific position.

B = 185
L = 250
W = (L-B)/6
D = (L/2)-L/4

x = np.linspace(-L/2, L/2, 500)
y1 = []
y2 = []

for X in x:
    termo1 = sqrt((L**2 - 4*X**2) / (L**2 + 8*W*X + 4*W**2))
    termo2 = ((sqrt(5.5*L**2 + 11*L*W + 4*W**2) * (sqrt(3)*B*B - 2*D*sqrt(L**2 + 2*W*L + 4*W**2))
               ) / (sqrt(3)*B*L*(sqrt(5.5*L**2 + 11*L*W + 4*W**2) - 2*sqrt(L**2 + 2*W*L + 4*W**2))))
    termo3 = 1 - sqrt((L*(L**2 + 8*W*X + 4*W**2)) / (2*(L - 2*W)*X**2 +
                      (L**2 + 8*L*W - 4*W**2)*X + 2*L*W**2 + L**2*W + L**2*W + L**3))

    calculo = B/2 * termo1 * (1-termo2 * termo3)
    y1.append(calculo)

    calculo = -B/2 * termo1 * (1-termo2 * termo3)
    y2.append(calculo)



fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.plot(x, y1, 'r')
plt.plot(x, y2, 'r')


plt.show()

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

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

发布评论

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

评论(1

糖果控 2025-01-23 21:45:47

您可以通过创建 onclick 事件来做到这一点;单击时它会占用鼠标线并将其用作偏移量...我想这就是您所要求的?尽管使用当前绘图的 x/y 限制,但根据您单击的位置,它不会显示,因此我将它们添加到绘图的配置中。

import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
import os
import matplotlib.image as mpimg


def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    plt.plot(x + ix, y1+ iy, 'r')
    plt.plot(x + ix, y2+ iy, 'r')
    plt.show()
    fig.canvas.mpl_disconnect(cid)

    return 


B = 185
L = 250
W = (L-B)/6
D = (L/2)-L/4

x = np.linspace(-L/2, L/2, 500)
y1 = []
y2 = []

for X in x:
    termo1 = sqrt((L**2 - 4*X**2) / (L**2 + 8*W*X + 4*W**2))
    termo2 = ((sqrt(5.5*L**2 + 11*L*W + 4*W**2) * (sqrt(3)*B*B - 2*D*sqrt(L**2 + 2*W*L + 4*W**2))
               ) / (sqrt(3)*B*L*(sqrt(5.5*L**2 + 11*L*W + 4*W**2) - 2*sqrt(L**2 + 2*W*L + 4*W**2))))
    termo3 = 1 - sqrt((L*(L**2 + 8*W*X + 4*W**2)) / (2*(L - 2*W)*X**2 +
                      (L**2 + 8*L*W - 4*W**2)*X + 2*L*W**2 + L**2*W + L**2*W + L**3))

    calculo = B/2 * termo1 * (1-termo2 * termo3)
    y1.append(calculo)

    calculo = -B/2 * termo1 * (1-termo2 * termo3)
    y2.append(calculo)


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim([-500,500])
ax.set_ylim([-500,500])
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')


coords = []
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

输入图片此处描述

You can do this by creating an onclick event; it will take the mouse cords when click and use them as an offset...I think that's what you are asking for? Though with the current plot x/y limits, it won't show up depending on where you click so I added those in the configuration of the plot.

import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
import os
import matplotlib.image as mpimg


def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    plt.plot(x + ix, y1+ iy, 'r')
    plt.plot(x + ix, y2+ iy, 'r')
    plt.show()
    fig.canvas.mpl_disconnect(cid)

    return 


B = 185
L = 250
W = (L-B)/6
D = (L/2)-L/4

x = np.linspace(-L/2, L/2, 500)
y1 = []
y2 = []

for X in x:
    termo1 = sqrt((L**2 - 4*X**2) / (L**2 + 8*W*X + 4*W**2))
    termo2 = ((sqrt(5.5*L**2 + 11*L*W + 4*W**2) * (sqrt(3)*B*B - 2*D*sqrt(L**2 + 2*W*L + 4*W**2))
               ) / (sqrt(3)*B*L*(sqrt(5.5*L**2 + 11*L*W + 4*W**2) - 2*sqrt(L**2 + 2*W*L + 4*W**2))))
    termo3 = 1 - sqrt((L*(L**2 + 8*W*X + 4*W**2)) / (2*(L - 2*W)*X**2 +
                      (L**2 + 8*L*W - 4*W**2)*X + 2*L*W**2 + L**2*W + L**2*W + L**3))

    calculo = B/2 * termo1 * (1-termo2 * termo3)
    y1.append(calculo)

    calculo = -B/2 * termo1 * (1-termo2 * termo3)
    y2.append(calculo)


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim([-500,500])
ax.set_ylim([-500,500])
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')


coords = []
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

enter image description here

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