[英]Thresholding without using 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 圖像來做這件事,你會得到奇怪的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.