Home > other >  Adding Offset to 'contours'
Adding Offset to 'contours'

Time:06-15

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 (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.

CodePudding user response:

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.

  • Related