我在Windows 10中工作,使用Python 3.8和OpenCV 4.5,并创建一个人工数据集来培训卷积神经网络。在一步中,我需要翻译轮廓
结构,但是我遇到了这个问题,但无法解决。
我需要通过 x_offset
和 y_offset
翻译轮廓
。但是,我很难使用 np.array
类型和OpenCV组织 CONTOURS
的方式。
我在opencv文档中阅读(
error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'drawContours'
> Overload resolution failed:
> - contours is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'contours'
所以我去了一个更简单的文件并重新创建了第一个错误在该网站:
import numpy as np
import cv2
im = cv2.imread('test.jpg',cv2.IMREAD_COLOR)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for data in contours:
print (f"The contours have this data: {data}")
cv2.drawContours(im,contours,-1,(0,255,0),3)
cv2.imshow('output',im)
cv2.waitKey(0)
当我打印轮廓
时,我将其作为结果得到:
[array([[[207, 146]],
[[207, 455]],
[[603, 455]],
[[603, 146]]], dtype=int32)]
我正在尝试保持结构,但是使用> x_offset
和 y_offset
,所以我的结果应该是:
[array([[[207 + x_offset, 146 + y_offset]],
[[207 + x_offset, 455 + y_offset]],
[[603 + x_offset, 455 + y_offset]],
[[603 + x_offset, 146 + y_offset]]], dtype=int32)]
< img src =“ https://i.sstatic.net/zlgkk.png” alt =“带有偏移的轮廓”>
有人可以帮助您使用算法来执行此操作而不会失去 CONTOUR的结构
?因此,我可以使用 drawContours
绘制此翻译轮廓
。
I'm working in Windows 10, with Python 3.8 and OpenCV 4.5, and creating an artificial dataset to train a Convolutional Neural Network. In one step I need to translate a contour
structure, but I'm stucked at this problem and I can't fix.
I need to translate a contour
by x_offset
and y_offset
. But I'm having a hard time with np.array
types and the way OpenCV organize contours
.
I read in the OpenCV docs (findContours()) that contours are stored as vectors, this shows us that these points need to be arranged somehow, and on my tries I'm probably losing the arrangement, because I'm getting this error:
error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'drawContours'
> Overload resolution failed:
> - contours is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'contours'
So I went to a much simpler file and recreated the first example on that Getting Started with Contours site:
import numpy as np
import cv2
im = cv2.imread('test.jpg',cv2.IMREAD_COLOR)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for data in contours:
print (f"The contours have this data: {data}")
cv2.drawContours(im,contours,-1,(0,255,0),3)
cv2.imshow('output',im)
cv2.waitKey(0)

When I print contours
, I get this as result:
[array([[[207, 146]],
[[207, 455]],
[[603, 455]],
[[603, 146]]], dtype=int32)]
I'm trying to keep the structure, but remap the points using x_offset
and y_offset
, so my result should be something like:
[array([[[207 + x_offset, 146 + y_offset]],
[[207 + x_offset, 455 + y_offset]],
[[603 + x_offset, 455 + y_offset]],
[[603 + x_offset, 146 + y_offset]]], dtype=int32)]

Can someone help with an algorithm to perform this operation without losing the structure of the contour
? So I can use drawContours
to draw this translated contour
.
发布评论
评论(1)
您只需要将X和Y偏移添加到每个轮廓中作为元组:
我想
Contours
的数据结构使您感到困惑。轮廓
是元组。确定的每个轮廓都存储为单个元组。每个轮廓的点都存储在这些元组中。You just have to add your X and Y offset to every contour as a tuple:
I guess the data structure of
contours
got you confused.contours
is a tuple. Every contour identified is stored as an individual tuple. The points for each contour is stored inside these tuples.