如何减少两点之间的长度OpenCV - Python

时间:2017-01-28 00:51:39

标签: python opencv

我正在使用OpenCV 3.1,我试图以相同的角度降低(x2,y2)坐标。例如,下面的图片Old coordinatesP1new coordinatedP2;我正在尝试将P1移至P2

enter image description here

我为此编写了以下代码:

import cv2
import math


def new_coordinates(point_one, point_two):
    print(("x1", point_one[0], "y1", point_one[1]), ("x2", point_two[0], "y2", point_two[1]))
    distance = math.sqrt((point_two[0] - point_one[0]) ** 2 + (point_two[1] - point_one[1]) ** 2)
    print("distance between point one and two", distance)

    slope = (point_two[1] - point_one[1]) / (point_two[0] - point_one[0])
    print("Slope", slope)

    angle = math.degrees(math.atan(slope))
    print("angle", angle)

    a = math.cos(angle) * (distance/0.5)
    print("a", a)
    b = math.sin(angle) * (distance/0.5)
    print("b", b)

    x_a = point_one[0] - a
    y_b = point_one[1] - b

    print("New points", (int(x_a), int(y_b)))
    new_distance = math.sqrt((int(x_a) - point_two[0]) ** 2 + (int(y_b) - point_two[1]) ** 2)
    print("new distance", new_distance)
    return int(x_a), int(y_b)

new_co = new_coordinates((400,400), (600,200))

img = cv2.imread('test_images/solidWhiteRight.jpg')
cv2.line(img, (400,400), (600,200), [0,255,0], 7)
cv2.line(img, (400,400), new_co, [204,255,204], 7)
cv2.imshow('image', img)
cv2.waitKey(0)

我假设有两个坐标A = (400,400)B = (600, 200)作为参考线,保持点A不变,我尝试使用new_coordinates函数移动点,但结果是:

enter image description here

我不确定我做错了什么。 Cany有人帮助我吗?

1 个答案:

答案 0 :(得分:2)

我不确定但是更简单的事情应该起作用

def new_coordinates(point_one, point_two):

    dx = (point_two[0] - point_one[0])
    dy = (point_two[1] - point_one[1])

    x_a = point_one[0] - dx/0.5
    y_b = point_one[1] - dy/0.5

    print("New points", (int(x_a), int(y_b)))

    return (int(x_a), int(y_b))

基于拦截定理,也称为Thales' theorem

编辑:您的原始版本

  • 跳过math.degrees()因为sin()cos()需要radians
  • 使用atan2()代替atan()

对两种功能进行简单测试的代码:

import math

def new_coordinates_1(point_one, point_two):

    dx = (point_two[0] - point_one[0])
    dy = (point_two[1] - point_one[1])

    distance = math.sqrt(dx**2 + dy**2)

    angle = math.atan2(dy, dx)

    a = math.cos(angle) * (distance/0.5)
    b = math.sin(angle) * (distance/0.5)

    x_a = point_one[0] - a
    y_b = point_one[1] - b

    return (round(x_a), round(y_b))

def new_coordinates_2(point_one, point_two):

    '''
    Based on "The intercept theorem", also known as "Thales' theorem"
    https://en.wikipedia.org/wiki/Intercept_theorem
    '''

    dx = (point_two[0] - point_one[0])
    dy = (point_two[1] - point_one[1])

    x_a = point_one[0] - dx/0.5
    y_b = point_one[1] - dy/0.5

    return (round(x_a), round(y_b))

# --- test ---

data = [
    ((400,400), (600,200)),
    ((400,400), (600,100)),
    ((400,400), (600,400)),
    ((400,400), (400,100)),
    ((400,400), (200,100)),
    ((400,400), (200,500)),
]

for p1, p2 in data:
    print(p1, p2)
    print("#1 New points", new_coordinates_1(p1, p2))
    print("#2 New points", new_coordinates_2(p1, p2))
    print('---')

结果:

(400, 400) (600, 200)
#1 New points (0, 800)
#2 New points (0, 800)
---
(400, 400) (600, 100)
#1 New points (0, 1000)
#2 New points (0, 1000)
---
(400, 400) (600, 400)
#1 New points (0, 400)
#2 New points (0, 400)
---
(400, 400) (400, 100)
#1 New points (400, 1000)
#2 New points (400, 1000)
---
(400, 400) (200, 100)
#1 New points (800, 1000)
#2 New points (800, 1000)
---
(400, 400) (200, 500)
#1 New points (800, 200)
#2 New points (800, 200)
---