We have created this tutorial for dynamic image conversion. python3 app.py -image="data.jpg" -preprocess="thresh" Conclusion Nonetheless, if we pass the value 0, it will wait indefinitely until a key event occurs.įinally, once the user presses the key, we call the destroyAllWindows() function, which will destroy the previously created windows. The waitKey() method receives as input the delay, specified in milliseconds. We will show both images to compare the converted image with the original one.įinally, we will call the waitKey() method, which will wait for a keyboard event. The cv2.imshow() function receives as first input a string with the name to assign to the window and as the second argument the image to show.
To show the images, we have to call the imshow() function of the cv2 module.
#Cv2 rgb to gray code
You just have to append the following code inside the function to open both images. We can also programmatically open both RGB images and Grayscale images using the cv2 module. This kind of filter is useful for correctly OCRing the image using Tesseract. Applying the median blur can help reduce salt and pepper noise. A median blur is applied when the –preprocess flag is set to blur. We do this using both the cv2.THRESH_BINARY and cv2.THRESH_OTSU flags.įor details on Otsu’s method, see “Otsu’s Binarization” in the official OpenCV documentation.Īlternatively, a blurring method may be applied. The if statement inside the function performs a threshold to segment the foreground from the background. This is where you would have to add more advanced preprocessing methods. We will either threshold or blur the image depending on the preprocessing method specified by our command-line argument. python3 app.py -image="data.jpg" -preprocess="thresh" Output # write the grayscale image to disk as a temporary file.Īp.add_argument("-p", "-preprocess", type=str, default="thresh", # check to see if median blurring should be done to remove noise # check to see if we should apply thresholding to preprocess the image # Function to convert Normal Image to GrayScale We can check to see if we should apply thresholding to preprocess the image, else make a check to see if the median blurring should be done to remove noise. Now, we will pass another argument, called preprocess, whose value is one of the following two. Until now, at the time of the command, we have passed only one argument called –image.
My image is on the right side of the following output. You can check your current project folder and look for an image name like 4999.png or any four digits number.png image. Now, go to the terminal and type the following command with the image path. # Fetch the arguments from the command line # Function to convert RGB Image to GrayScale So, our final code looks like the following. Here, the data.jpg image is passed to the convertImageToGray() method. So, when we run the command python3 app.py, we pass the argument path to the image like the following. –image: The path to the image we’re sending to the function.We have the following command-line arguments: # Fetch the arguments from the command lineĪp.add_argument("-i", "-image", required=True, Step 3: Fetch the input image.Īdd the following code inside the app.py file. Since we want to convert the original RGB image from the BGR color space to gray, we use the code COLOR_BGR2GRAY. As a second input, it gets the color space conversion code. To do it, we have to call the cvtColor() function, which allows us to convert the image from one color space to another.Īs the first input, this function receives the original image. Next, we need to parse the image to grayscale. This parameter contains the image we need to convert to grayscale.Īs an additional note, which will be necessary for converting to grayscale, the imread() function has the channels stored in BGR (Blue, Green, and Red) order by default. You can see that we have defined a function called the convertImageToGray() method. Print('The image is converted to Grayscale successfully') Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Nonetheless, you should handle these types of exceptions for robust code implementation. We will read the image using the cv2.imread() method in this function.įor simplicity, we assume that the file exists and everything loads fine, so we will not be making any error or exception checks. Import os Step 2: Define a Grayscale function. And then, import the cv2 module and os module.