

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. python3 app.py -image="data.jpg" -preprocess="thresh" Outputĭepending on the preprocessing method specified by our command-line argument, we will either threshold or blur the image. # 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, which is 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. Mine image is 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
#Python matrix image convert to grayscale code
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 a 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 the conversion 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 convertImageToGray() method. Print('The image is converted to Grayscale successfully') Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Nonetheless, for robust code implementation, you should handle these types of exceptions. For simplicity, we assume that the file exists, and everything loads fine, so we will not be doing any error or exception check.
