简体   繁体   English

如何在没有for循环的情况下更改python(opencv)中的像素值

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

I'm starting with OpenCV in Python and I have a problem. 我从Python中的OpenCV开始,但遇到了问题。

I have a depth image taken from a kinect camera. 我有一个从kinect相机拍摄的深度图像。 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. 我想将这些值更改为图像的最大值(即2880),而不使用for循环。

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 : 有一个可以创建边框的函数copyMakeBorder这是python教程

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. 使用imread读取图像文件时,该文件存储在numpy数组中。 Therefore, you can use the indexing of numpy arrays. 因此,您可以使用numpy数组的索引。 like this: 像这样:

depthImage[depthImage==0] = 2880

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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