0

I'm starting with OpenCV in Python and I have a problem.

I have a depth image taken from a kinect camera. This image has a border whose pixel values are zero. I want to change those values to the maximum of the image (that is 2880) without using for loops.

The code until here is:

import cv2
 depthImage = cv2.imread('depthImageName',cv2.IMREAD_UNCHANGED)
 if depthImage.any() == 0:
     depthImage = 2880

But it doesn't work and the values equal to zero remain.

Anyone who can help me?

If I've forgotten useful information, let me know.

Thanks in advance guys! :)

2 Answers 2

1

When you read the image file using imread, it is stored in a numpy array. Therefore, you can use the indexing of numpy arrays. like this:

depthImage[depthImage==0] = 2880
Sign up to request clarification or add additional context in comments.

4 Comments

Even if it is true it will iterate anyway, while if this is border it can be done in 4 iterations
yeah, but it's a highly optimized iteration. In addition, the top,left,bottom,right values might not be known or might be inconsistent for different input images. Or one might not want to change the image size.
But OP specified that he wants to replace zeroes in borders only, but his code looks differently.
You're right about that, but my answer is exactly what he/she is trying to do in his/her sample code. That's why I assumed changing all zero values wouldn't be a problem.
1

There is a function that can create borders, copyMakeBorder, here is python tutorial:

img1 = cv2.imread('opencv_logo.png')
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)

Comments

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.