python下使用openCV3,如何在一幅灰度图中,为所有灰度为某特定值的点赋另一灰度值

2025-03-13 01:48:23
推荐回答(1个)
回答1:

你好,我觉得用np.where是可以实现的,下面是相关的实现代码:

import cv2
import numpy as np
image = np.zeros((400,400,3), dtype="uint8")
raw = image.copy()
image[np.where((image==[0,0,0]).all(axis=2))] = [255,255,255]
cv2.imshow('Test0', image)
lower_black = np.array([0,0,0], dtype = "uint16")
upper_black = np.array([70,70,70], dtype = "uint16")
black_mask = cv2.inRange(image, lower_black, upper_black)
image[np.where((image == [0,0,0]).all(axis = 2))] = [155,255,155]
black_mask[np.where(black_mask == [0])] = [155]
cv2.imshow('Test', image)
cv2.imshow('Test2', raw)
cv2.imshow('Test3', black_mask)