Search code examples
pythonopencvthreshold

How to save image in opencv after thresholding using imwrite


I'm trying to save an image using cv2.imwrite after thresholding but it doesnt save the image. below is the code I'm using:

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"])

Solution

  • I think you want to save the thresh image. cv2.imwrite(args["image"], thresh) should be inside the for loop.