3/1/2023 0 Comments Cv2 rgb to gray![]() ![]() This code displays both the RGB and Grayscale images side-by-side on a graph. This method imports the Matplotlib and Scikit-Learn libraries to convert an RGB image to a Grayscale Representation. Method 4: Use Matplotlib and Sci-Kit-Learn Then, the Grayscale representation is plotted as a graph and output as shown below: This function, when called later converts the argument passed ( img_rgb) into a Grayscale representation and returns the same. Next, a function is created accepting an object as an argument. Plt.imshow(img_gray, cmap=plt.get_cmap('gray'))Ībove, two (2) libraries and called in: NumPy to use the np.dot() function and Matplotlib to handle the other calls. ![]() ![]() This method uses both the NumPyand Matplotliblibraries to read an RGB image, convert it to a Grayscale representation, plot, and display the image on a graph. Once to display the original RGB image (underneath), and once to display the Grayscale representation (top). □ Note: Notice the RGB color changes when img_rgb is output to the terminal compared to img_gray.įinally, image.show() is called twice. If img_gray was sent to the terminal, the following would display (snippet only): Then, the following line converts the above image to Grayscale ( cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)) and saves to img_gray. If img_rgb was sent to the terminal, the following would display as follows (snippet only): Next, erock_rgb.jpg is read in using cv2.imread('erock_rgb.jpg'). Img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) This method imports the OpenCVlibrary to call in and use various functions to convert an RGB image to a Grayscale representation. Img_rgb = Image.open('erock_rgb.jpg').convert('L') The results save as erock_gray.jpg.Ī compact way to perform the same task is to append convert('L') to the end of the second line: reducing the code by one (1) full line. Using img_rgb.convert('L'), converts the RGB object to a Grayscale representation of the same. This object allows us access to and manipulation of this particular image. This returns an object img_rgb similar to below. Next, the image saved earlier is opened ( Image.open('erock_rgb.jpg')) and passed the image name as an argument. Click here to install the pillow library, if required. from PIL import ImageĪbove, imports the PIL ( pillow) library. This function converts an RGB image to a Grayscale representation. This method imports the PIL ( pillow) library allowing access to the img.convert() function. Method 4: Use Matplotlib and Scikit-Learn libraries.Method 3: Use NumPyand Matplotliblibraries.We can accomplish this task by one of the following options: □ Question: How would we write Python code to convert an RGB image to a Grayscale representation? □ Note: To follow along, right-click on the above image and save it as erock_rgb.jpg, then move to the current working directory. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |