Home > other >  Pixel count of image is three times expected value
Pixel count of image is three times expected value

Time:04-09

I am trying to get the Black and white pixel count from an Image. I am using image of 100X100 pixel with half white and half black.

This is the code I am using.

import cv2
import numpy as np
img = cv2.imread(r"C:\Users\yadav\OneDrive\Desktop\image_complexity\image.png")
white_pix_count = np.sum(img == 255)
print("Number of white pixel:",white_pix_count)
black_pix_count = np.sum(img == 0)
print("Number of black Pixel is :",black_pix_count)
total_pix_count = np.sum(img >= 0)
print("Number of total count:",total_pix_count)

Output I am getting is :

Number of white pixel: 14400
Number of black Pixel is : 15600
Number of total count: 30000

If I am using 100X100 size of image then Total no of Pixel should be 10000 why i am getting 30000 This is the image I am using as input

CodePudding user response:

You are using an image of 100X100 pixel. So the pixel value will be 100*100= 10,000 for single channel. The image has 3 channels so 3 * 10,000 = 30,000 (So you got 30,000) but below code I'm converting colour image to black and image. Hope it will work !

from PIL import Image
import os
import cv2
import numpy as np
img = cv2.imread(r"img path",0)
white_pix_count = np.sum(img == 255)
print("Number of white pixel:",white_pix_count)
black_pix_count = np.sum(img == 0)
print("Number of black Pixel is :",black_pix_count)
total_pix_count = np.sum(img >= 0)
print("Number of total count:",total_pix_count)

CodePudding user response:

The reason you are getting 30000 as the total pixel count is because your image is read in as BGR format, meaning there will be 3 channels for each color.

To see this, you can print out the shape of the image:

import cv2
import numpy as np
img = cv2.imread(r"C:\Users\yadav\OneDrive\Desktop\image_complexity\image.png")
print(img.shape)

Output:

(100, 100, 3)

Or print a small segment of the image that's 2 pixels by 2 pixels in size:

import cv2
import numpy as np
img = cv2.imread(r"C:\Users\yadav\OneDrive\Desktop\image_complexity\image.png")
print(img[:2, :2])

Output:

[[[255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]]]

To fix this, create a grayscale version of the image, and use it instead:

import cv2
import numpy as np
img = cv2.imread(r"C:\Users\yadav\OneDrive\Desktop\image_complexity\image.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
white_pix_count = np.sum(gray == 255)
print("Number of white pixel:",white_pix_count)
black_pix_count = np.sum(gray == 0)
print("Number of black Pixel is :",black_pix_count)
total_pix_count = np.sum(gray >= 0)
print("Number of total count:",total_pix_count)

Output:

Number of white pixel: 4800
Number of black Pixel is : 5200
Number of total count: 10000

Or, if you will not be using the BGR image at all, simply read in the image as grayscale in the first place:

import cv2
import numpy as np
path = r"C:\Users\yadav\OneDrive\Desktop\image_complexity\image.png"
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
white_pix_count = np.sum(img == 255)
print("Number of white pixel:",white_pix_count)
black_pix_count = np.sum(img == 0)
print("Number of black Pixel is :",black_pix_count)
total_pix_count = np.sum(img >= 0)
print("Number of total count:",total_pix_count)
  • Related