0

I have a 4-level 4-dimensional array contours (result of cv2.findContours) at the end of which I have pairs of coordinates. It looks like this:

print(contours[0][0])
→ [[ 676 4145]]
print(contours[0][0][0])
→ [ 676 4145]
print(contours[0][0][0][1])
→ 4145

I want to edit the axis element 1 of each of the last-level arrays so that the value is bigger by 10. I am aware of the documentation, but I don't know how to apply it so deep without flattening. How to do that?

2
  • 1
    I think you've got your terminology muddled. It's "4-dimensional" or "4-axis", and by "axis 1" I think you mean "element 1" Commented Feb 23, 2017 at 9:36
  • I am pretty sure you are right. Commented Feb 23, 2017 at 10:59

1 Answer 1

1

Any of these would work:

  • contours[:,:,:,1] += 10
  • contours[...,1] += 10
  • contours += [0, 10]
Sign up to request clarification or add additional context in comments.

10 Comments

For contours[:,:,:,1] += 10 it says "Invalid syntax", as expected; for contours = contours[:,:,:,1] + 10 it says "TypeError: list indices must be integers or slices, not tuple", as well as for contours[...,1] + 10, while for contours = contours + [0, 10] it says "OpenCV Error: Assertion failed (npoints >= 0 && (depth == CV_32F || depth == CV_32S)) in cv::pointSetBoundingRect". I haven't know these tricks, so thanks, but they seem to concern Python lists, not NumPy arrays.
There's no way that first one gives a SyntaxError. You must have typed it wrong, or added to it in an invalid way. For the second one, you must have lied in your question - it sounds like contours is a list, not an np.array The third one doesn't sound like it is coming from this line of code...
My bad, contours is a list, but its members are numpy arrays (contours[0], contours[0][0] and contours[0][0][0]) until contours[0][0][0][0], which is numpy int 32. Is there a way to do that in a simple way within a list or arrays, without using for?
I tried contours = [c[:,:,1] + 10 for c in contours], but I get errors; for instance, print(type(contours[0][0][0][1])) returns IndexError: invalid index to scalar variable. Is that because of different type of int for NumPy and Python?
Ignoring the +10, it's the difference between [c[:,:,:] for c in contours] and [c[:,:,1] for c in contours]. The latter is only taking element 1, whereas the former takes both.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.