如何在使用 imwrite 进行阈值处理后将图像保存在 opencv 中

How to save image in opencv after thresholding using imwrite

我尝试在阈值处理后使用 cv2.imwrite 保存图像,但它没有保存图像。以下是我正在使用的代码:

import necessary packages

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--image", required = True,
    help = "Path to the image to be thresholded")

ap.add_argument("-t", "--threshold", type = int, default = 128,
    help = "Threshold value")

args = vars(ap.parse_args())

image = cv2.imread(args["image"])

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

methods = [("THRESH_BINARY", cv2.THRESH_BINARY)]

for (threshName, threshMethod) in methods:

    (T, thresh) = cv2.threshold(gray, args["threshold"], 255, threshMethod)

    cv2.imshow(threshName, thresh)

    cv2.waitKey(0)

cv2.imwrite(gray, args["image"])

我想你想保存脱粒图像。 cv2.imwrite(args["image"], thresh) 应该在 for 循环内。