Skip to main content

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)






Comments

Popular posts from this blog

OpenCV Capture Image From Camera

Capture Image From Camera Using OpenCV, you can capture video from your computer's webcam and use these videos for many purposes. OpenCV provides a very simple interface for video capture process. It takes preparation to capture video. To capture a video, you must create a VideoCapture object. Its argument may be the device index. capture = cv2.VideoCapture( 0 )   The device index is just the number that determines which camera to use. Normally 0 is selected if a camera will be connected. You can select the second camera by giving a value of 1. After that, you can shoot frame by frame. ret , frame = capture.read()   cap.read () returns a bool value. If the frame reads correctly, the value will be True. With the Cap.isOpened () method, you can check whether the video capture process is started. If it's true, it's okay. If not, you need to open it using cap.open (). The codes are all as follows: import cv2 capture = cv2.VideoCapture( 0 ) while ( True ):   # C

Opencv Read-Write-Display Image Python

Read-Write-Display Image In this lesson, you will learn how to read, how to view and save. The functions cv2.imread (), cv2.imshow (), cv2.imwrite () will be used for these operations. To read an image, the function cv2.imread () is used. The image to be read must be in the working directory or a full visual path must be given. The second argument is a sign that indicates how the image should be read. cv2.IMREAD_COLOR: Indicates that a color image will be loaded. It is the default flag. The integer value for this flag can be assigned to 1. cv2.IMREAD_GRAYSCALE: Loads the image in grayscale mode. cv2.IMREAD_UNCHANGED: Loads the image as included in the alpha channel. import cv2 # The code below reads the image and assigns it to the image variable. image = cv2.imread( 'bird' ) The function cv2.imshow () is used to display an image. The window opens automatically in image size. The first argument is the name of the window to open. The second argument is the image we re

Show OpenCV Image With Matplotlib

Show Image With Matplotlib Matplotlib is a drawing library that offers a wide variety of drawing methods for Python. Matplotlib; The basic python library we use in data visualization. It allows us to make 2 and 3 dimensional drawings. In order to use Matplotlib, we need to add it to our project as follows: from matplotlib import pyplot as plt The image is read as follows: img = cv2.imread( 'bird.jpg' )   Finally, the captured image is displayed on the screen with the following code: plt.imshow(img) plt.xticks([]) , plt.yticks([]) plt.show() Code and output are as follows: import cv2 from matplotlib import pyplot as plt img = cv2.imread( 'bird.jpg' ) plt.imshow(img) plt.xticks([]) , plt.yticks([]) plt.show() Output: