简体   繁体   中英

how to change pixel values in python (opencv) without for loops

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! :)

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM