[英]Thresholding without using OpenCV
I need to threshold my image without using OpenCV function.我需要在不使用 OpenCV 函数的情况下对图像进行阈值处理。
I know this way only, but in this case I am using cv2.threshold function from OpenCV:我只知道这种方式,但在这种情况下,我使用 OpenCV 中的 cv2.threshold 函数:
img = cv2.imread('filename', 0)
_, thresh = cv2.threshold(img,127,255,cv.THRESH_BINARY)
How can I code thresholding without using cv2.threshold function.如何在不使用 cv2.threshold 函数的情况下编码阈值。
I tried this one:我试过这个:
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
Where, n is equal to 127其中,n 等于 127
Thanks in advance.提前致谢。
You can use numpy to threshold in Python without OpenCV.您可以在没有 OpenCV 的情况下在 Python 中使用 numpy 来设置阈值。
image[image>127] = 255
image[image!=255] = 0
if image is a grayscale image.如果图像是灰度图像。
You can threshold like this:你可以像这样阈值:
thresholdIMG = image[:, :, <color channel>] > <threshold>
But if you are going to do that with a RGB image you will get weird results.但是如果你打算用 RGB 图像来做这件事,你会得到奇怪的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.