Skip to content

Instantly share code, notes, and snippets.

@MuhammadFaizanKhan
Last active March 13, 2024 17:08
Show Gist options
  • Save MuhammadFaizanKhan/d2d139e4934196991ae1cbab02d1d782 to your computer and use it in GitHub Desktop.
Save MuhammadFaizanKhan/d2d139e4934196991ae1cbab02d1d782 to your computer and use it in GitHub Desktop.
Coverting image to grayscale in opencv python
import cv2
import sys
# reading image and saving in a var
image = cv2.imread(sys.argv[1]) # The first argument is the image, getting from command line
print("image shape")
print(image.shape)
# Convert to Grayscale
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
'''First, we convert the image to gray. The function that does that is cvtColor().
The first argument is the image to be converted, the second is the color mode.
COLOR_BGR2GRAY stands for Blue Green Red to Gray.
You must have heard of the RGB color scheme. OpenCv does it the other way round-
so blue is first, then green, then red.'''
cv2.imshow("Original Image", image)
cv2.imshow("Gray Image", grayImage)
cv2.waitKey(0)
'''
Note:
Two important functions in image processing are blurring and grayscale.
Many image processing operations take place on grayscale (or black and white) images,
as they are simpler to process (having just two colors).
Similarly, blurring is also useful in edge detection, as we will see in next tut example. .
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment