Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用RBG对图像进行阈值设置_Python_Computer Vision_Cv2_Grayscale_Image Thresholding - Fatal编程技术网

Python 使用RBG对图像进行阈值设置

Python 使用RBG对图像进行阈值设置,python,computer-vision,cv2,grayscale,image-thresholding,Python,Computer Vision,Cv2,Grayscale,Image Thresholding,我想阈值的图像,但不是输出的黑色和白色,我希望它是白色和其他一些颜色。我能够使用嵌套for循环实现这一点,但是这很慢,我想知道是否有人知道使用CV2功能有效地实现这一点的方法 img = cv2.imread("Naas.png", 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) # Cha

我想阈值的图像,但不是输出的黑色和白色,我希望它是白色和其他一些颜色。我能够使用嵌套for循环实现这一点,但是这很慢,我想知道是否有人知道使用CV2功能有效地实现这一点的方法

img = cv2.imread("Naas.png", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

# Changing black to green and converting from Grayscale to RGB

lis = []
for i in thresh:
    for j in i:
        if j == 0:
            lis.append((0, 255, 0))
        else:
            lis.append((255, 255, 255))
        
img = np.array(lis, dtype = "uint8")
img = img.reshape(thresh.shape[0], thresh_inv.shape[], 3) 

此循环将阈值图像中的所有黑色像素更改为绿色。

因此绿色通道始终为255,而红色和蓝色通道仅为阈值

所以你看到的是这样的东西

import numpy as np 
img = cv2.imread("Naas.png", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
img_result=np.ones(img.shape)*255 #set all to 255
img_result[:,:,0]=thresh[:,:] #set red channel to threshold
img_result[:,:,2]=thresh[:,:] #set blue channel to threshold

为什么你要把所有的东西都列在一个列表中?只是为了存储,正如我说的,这是一种效率很低的方法,它更能证明我的最终目标,非常有效,谢谢