I'm trying to count the number of red pixels, blue pixels, and green pixels in an image. How can I do that using python
CodePudding user response:
step 1:-write the code to read the colored image and display it using matplotlib. like this:-
import cv2
import numpy as np
from matplotlib import pyplot as plt
b,g,r = cv2.split(img)
rgb_img = cv2.merge([r,g,b])
plt.imshow(rgb_img)
step 2:-Now we will break down the three dimensional matrix rgb_img in to three different components Red, Green and Blue.Below are the steps to do the same:
x,y,z = np.shape(img)
red = np.zeros((x,y,z),dtype=int)
green = np.zeros((x,y,z),dtype=int)
blue = np.zeros((x,y,z),dtype=int)
for i in range(0,x):
for j in range(0,y):
red[i][j][0] = rgb_img[i][j][0]
green[i][j][1]= rgb_img[i][j][1]
blue[i][j][2] = rgb_img[i][j][2]
plt.imshow(red)
plt.imshow(green)
plt.imshow(blue)
CodePudding user response:
Look at the package PILLOW for Python.
https://pillow.readthedocs.io/en/stable/reference/Image.html https://pillow.readthedocs.io/en/stable/reference/ImageColor.html