![]() So, the first input of the function is the gray scale image to which we want to apply the operation.Īs second input, it receives the value of the threshold. Naturally, the threshold function allows us to specify these parameters. Also, since we want to convert the image to black and white, when the pixel is greater than the threshold, the value to which we want it to be converted is 255. ![]() Note that since we are operating over a gray scale image, pixel values vary between 0 and 255. Otherwise, it is set to a user defined value. Note however that OpenCV offers more types of thresholding, as can be seen here.Īs already mentioned, the algorithm for binary thresholding corresponds to the following: for each pixel of the image, if the value of the pixel is lesser than a given threshold, then it is set to zero. To do it, we need to call the threshold function of the cv2 module.įor this tutorial we are going to apply the simplest thresholding approach, which is the binary thresholding. Now, to convert our image to black and white, we will apply the thresholding operation. GrayImage = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY) The first thing we need to do is importing the cv2 module, so we have access to all the functions that will allow us to convert the image to black and white. This tutorial was tested on Windows 8.1, with version 4.0.0 of OpenCV. You can read here about more advanced operations that we can do in OpenCV to obtain better results. Note whoever that this is a very simple approach, which may not give the best results if, for example, the image has different light conditions in different areas. Otherwise, we assign to it the value 255 (white). Then, for each pixel of the gray scale image, if its value is lesser than the threshold, then we assign to it the value 0 (black). We start with a gray scale image and we define a threshold value. In this tutorial we will learn how to convert an image to black and white, using Python and OpenCV.Ĭonverting an image to black and white with OpenCV can be done with a simple binary thresholding operation. In this tutorial we will learn how to convert an image to black and white, using Python and OpenCV.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |