Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 不使用OpenCV的阈值化_Python_Threshold_Image Thresholding - Fatal编程技术网

Python 不使用OpenCV的阈值化

Python 不使用OpenCV的阈值化,python,threshold,image-thresholding,Python,Threshold,Image Thresholding,我需要在不使用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.s

我需要在不使用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>
thresholdIMG=image[:,:,]>

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

因为StackOverflow不是一个代码编写服务,你应该编辑你的诚实尝试,将其编码到你的问题中。这种尝试不适用于任何服务,我是这个领域的初学者,我正在努力学习一些东西。我写这篇文章只是因为我需要帮助。看起来你仍在计划使用OpenCV来读取/创建图像。那么为什么不也使用OpenCV进行阈值化呢?但是,如果您只想使用较低级别的库,您可以通过枕头和NumPy的组合实现。看看这些,了解一些想法:和