Skip to main content

Posts

Showing posts with the label How to count pixel in Opencv

How to count pixel in Python?

 How to count pixel in Image? Tutorial How to count pixel in image using Python and Opencv? First let's read our image: image=cv2.imread( 'flower.jpg' )  Convert the image to rgb: image=cv2.cvtColor(image , cv2.COLOR_BGR2RGB) Create a numpy array: array = np.array(image) Arrange all pixels into a tall column of 3 RGB values and find unique rows: colours , counts = np.unique(array.reshape(- 1 , 3 ) , axis = 0 , return_counts = 1 ) Prints: print (colours) print (counts) All Code: import numpy as np import cv2 image=cv2.imread( 'flower.jpg' ) cv2.imshow( "Image" , image) image=cv2.cvtColor(image , cv2.COLOR_BGR2RGB) array = np.array(image) colours , counts = np.unique(array.reshape(- 1 , 3 ) , axis = 0 , return_counts = 1 ) print (colours) print (counts) cv2.waitKey( 0 )