不使用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图像进行操作,则会得到奇怪的结果。