将偏移量添加到轮廓'

发布于 2025-02-07 08:28:16 字数 2191 浏览 3 评论 0 原文

我在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)

Square Recognized in Binary Image

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)]

Contour with Offset

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.

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

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

发布评论

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

评论(1

断念 2025-02-14 08:28:16

您只需要将X和Y偏移添加到每个轮廓中作为元组:

x_offset, y_offset = 5, 3
for contour in contours:
  # contour with new offset is created
  new_contour = contour + (x_offset, y_offset)
  # draw the new contour on the image
  cv2.drawContours(im,new_contour,-1,(0,255,0),3)

我想 Contours 的数据结构使您感到困惑。 轮廓是元组。确定的每个轮廓都存储为单个元组。每个轮廓的点都存储在这些元组中。

You just have to add your X and Y offset to every contour as a tuple:

x_offset, y_offset = 5, 3
for contour in contours:
  # contour with new offset is created
  new_contour = contour + (x_offset, y_offset)
  # draw the new contour on the image
  cv2.drawContours(im,new_contour,-1,(0,255,0),3)

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.

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