简体   繁体   English

我在使用 opencv python 的坐标对在图像上绘制线条时遇到问题

[英]I have an issue while drawing the lines over an image using pair of coordinates using opencv python

I have a set of coordinates from an image.我有一组来自图像的坐标。 Using those coordinates I wanted to plot the lines over an image.使用这些坐标我想 plot 图像上的线条。 While trying I ended up with an error.在尝试时,我遇到了错误。 Please help me with this issue.请帮我解决这个问题。 Below is the code.下面是代码。

[((line[0, 0], line[0, 1]), (line[1, 0], line[1, 1])) for line in lines[:nlines]]

print(lines[:lines])

for i in lines:
    x1,y1,x2,y2=i[0]
    cv2.line(image,(x1,y1),(x2,y2),(255,0,0),2)

cv2.imshow("Image",image)
cv2.waitKey(0)

Error:错误:

x1,y1,x2,y2=line[0]

ValueError: not enough values to unpack (expected 4, got 2)

Points Output:点 Output:

[[[1150  327]
  [1166  316]]

 [[1146  475]
  [1158  467]]

 [[ 903  322]
  [ 911  320]]

 ...

 [[ 364  403]
  [ 374  402]]

 [[ 644  570]
  [ 649  569]]

 [[ 249  645]
  [ 255  644]]]

The issue is that each line is defined by two points.问题是每条线由两个点定义。

From your printing, you can see the data arrangement.从您的打印中,您可以看到数据排列。

Example:例子:

a = [[[1150  327]
      [1166  316]]

     [[1146  475]
      [1158  467]]]

The data arrangement above is: two arrays, containing two arrays containing two numerical values.上面的数据排列是:两个arrays,包含两个arrays,包含两个数值。
The hierarchy is an array built of arrays of arrays (or list built of list of lists).层次结构是由 arrays 的 arrays 构建的数组(或由列表列表构建的列表)。
Very confusing structure...非常混乱的结构...

As you can see there is an hierarchy of two "groups":如您所见,有两个“组”的层次结构:

  • a[0] equals [[1150 327] [1166 316]] a[0]等于[[1150 327] [1166 316]]
  • a[1] equals [[1146 475] [1158 467]] a[1]等于[[1146 475] [1158 467]]

Separating into sub-groups:分成子组:

  • a[0] equals [[1150 327] [1166 316]] a[0]等于[[1150 327] [1166 316]]
    • a[0][0] equals [1150 327] a[0][0]等于[1150 327]
    • a[0][1] equals [1166 316] a[0][1]等于[1166 316]
  • a[1] equals [[1146 475] [1158 467]] a[1]等于[[1146 475] [1158 467]]
    • a[1][0] equals [1150 327] a[1][0]等于[1150 327]
    • a[1][1] equals [1158 467] a[1][1]等于[1158 467]

Getting the numeric value: a[0][0][0] equals 1150 .获取数值: a[0][0][0]等于1150


x1, y1, x2, y2=line[0] gives you an error because line[0] is built from two arrays/lists: x1, y1, x2, y2=line[0]给你一个错误,因为line[0]是从两个数组/列表构建的:
You are trying to get 4 values, white there are only 2, so you are getting an error.你试图得到 4 个值,白色只有 2 个,所以你得到一个错误。

Example:例子:
line[0] equals [[1150 327] [1166 316]] line[0]等于[[1150 327] [1166 316]]

You can think about is as two points p0 and p1 , and use the syntax:您可以将 is 视为两个点p0p1 ,并使用以下语法:

p0, p1 = line[0]

Python allows a "weird" syntax - getting the values in two tuples: Python 允许“奇怪”的语法 - 获取两个元组中的值:

(x1, y1), (x2, y2) = line[0]

Here is a sample code sample that iterates an NumPy array of lines, and draw the lines:这是一个示例代码示例,它迭代 NumPy 线条数组,并绘制线条:

import cv2
import numpy as np

# Array of lines:
lines = np.array([[[1150, 327], [1166, 316]],
                  [[1146, 475], [1158, 467]],
                  [[ 903, 322], [ 911, 320]],                 
                  [[ 364, 403], [ 374, 402]],
                  [[ 644, 570], [ 649, 569]],
                  [[ 249, 645], [ 255, 644]]])

# Create black image (resolution 1200x1000)
image = np.zeros((1200, 1000, 3), np.uint8)

# Iterate lines:
for line in lines:
    (x1, y1), (x2, y2) = line
    cv2.line(image, (x1,y1), (x2,y2), (255,0,0), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)

Note:笔记:
I took the "Points Output" from your post, and built a NumPy array.我从您的帖子中获取了“点输出”,并构建了一个 NumPy 数组。
It could be that in your case you have to iterate lines[0] (it hard to tell from your post).可能是在您的情况下,您必须迭代lines[0] (很难从您的帖子中看出)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM