'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });
We use cv2.threshold() to convert a grayscale image to binary image. To convert a color image to binary image, we first convert the color image to grayscale image using cv2.cvtColor() then apply cv2.threshold() on the grayscale image.
Steps
One could follow the below given steps to convert a color image to a binary image-
Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it.
Read an the input image using cv2.imread(). The RGB image read using this method is in BGR format. Optionally assign the read BGR image to img.
Now convert this BGR image to grayscale image as below using cv2.cvtColor() function. Optionally assign the converted grayscale image to gray.
Apply thresholding cv2.threshold() on the grayscale image gray to convert it to a binary image. Adjust the second parameter (threshValue) for better binary image.
Display the converted binary image.
Let's look at some examples for a clear understanding about the question.
We will use the following image as the Input File in the examples below.
Example
In this Python program, we convert a color image to a binary image. We also display the binary image.
# import required librariesimport cv2# load the input imageimg = cv2.imread('architecture1.jpg')# convert the input image to grayscalegray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# apply thresholding to convert grayscale to binary imageret,thresh = cv2.threshold(gray,70,255,0)# Display the Binary Imagecv2.imshow("Binary Image", thresh)cv2.waitKey(0)cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output window showing the binary image.
Example
In this example, we convert a color image to a binary image. We also display the original, grayscale and binary images.
# import required librariesimport cv2import matplotlib.pyplot as plt# load the input imageimg = cv2.imread('architecture1.jpg')# convert the input image to grayscalegray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# apply thresholding to convert grayscale to binary imageret,thresh = cv2.threshold(gray,70,255,0)# convert BGR to RGB to display using matplotlibimgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# display Original, Grayscale and Binary Imagesplt.subplot(131),plt.imshow(imgRGB,cmap = 'gray'),plt.title('Original Image'), plt.axis('off')plt.subplot(132),plt.imshow(gray,cmap = 'gray'),plt.title('Grayscale Image'),plt.axis('off')plt.subplot(133),plt.imshow(thresh,cmap = 'gray'),plt.title('Binary Image'),plt.axis('off')plt.show()
Output
When you run the above program, it will produce the following output window showing the original, grayscale and binary images.
Notice the difference between the Grayscale image and Binary image. Binary image has only two colors: white and black. The pixel values of the Binary image are either 0 (black) or 255 (white).
Updated on: 02-Dec-2022
20K+ Views
Related Articles
- How to convert a colored image to HLS in OpenCV using Python?
- How to convert a colored image to Sepia image using Java OpenCV library?
- How to convert a colored image to blue/green/red image using Java OpenCV library?
- How to convert a colored image to grayscale using Java OpenCV library?
- How to convert HSV to colored image using Java OpenCV library?
- How to convert HLS to colored image using Java OpenCV library?
- How to convert colored image to HLS using Java OpenCV library?
- How to convert an RGB image to HSV image using OpenCV Python?
- How to convert a negative image to positive image using Java OpenCV library?
- How to create a binary image in OpenCV using C++?
- How to invert a binary image in OpenCV using C++?
- How to Convert RGB Image to Binary Image Using MATLAB?
- How to Convert Three Channels of Colored Image into Grayscale Image in MATLAB?
- How to create a black image and a white image using OpenCV Python?
- How to convert a positive image to Negative to using OpenCV library?
Advertisem*nts
';adpushup.triggerAd(ad_id); });