不使用OpenCV进行阈值设置



我需要在不使用OpenCV函数的情况下对我的图像进行阈值设置。

我只知道这种方式,但在这种情况下,我使用的是OpenCV:中的cv2.threshold函数

img = cv2.imread('filename', 0)
_, thresh = cv2.threshold(img,127,255,cv.THRESH_BINARY)

如何在不使用cv2.threshold函数的情况下对阈值进行编码。

我试过这个:

def thresholdimg(img, n):
img_shape = img.shape
height = img_shape[0]
width = img_shape[1]
for row in range(width):
for column in range(height):
if img[column, row] > s:
img[column, row] = 0
else:
img[column, row] = 255
return 

其中,n等于127

提前谢谢。

您可以在没有OpenCV的Python中使用numpy来设置阈值。

image[image>127] = 255
image[image!=255] = 0

如果图像是灰度图像。

您可以这样设置阈值:

thresholdIMG = image[:, :, <color channel>] > <threshold>

但如果你要用RGB图像来做这件事,你会得到奇怪的结果。

最新更新