I have a task to convert a grayscale image to binary and then take it back to its original form. I am using threshold function from opencv to convert the gray image into a binary. Is there any way to reconvert the binary image to the gray one?
You can follow the below steps to convert gray scale image to binary image :
i- read a grayscale image by importing cv2
import cv2
im_gray = cv2.imread('path_of_grayscale_image.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
ii- convert grayscale image to binary
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
which determines the threshold automatically from the image using Otsu's method, or if you already know the threshold you can use:
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
iii- save
cv2.imwrite('binary_image.png', im_bw)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.