![](/img/trans.png)
[英]How to count number of white and black pixels in color picture in python? How to count total pixels using numpy
[英]how to count black and white pixels in binary image using python
from PIL import Image
import PIL.ImageOps
import cv2
import numpy as np
from matplotlib import pyplot as plt
# read image
img = cv2.imread('bnw11.png')
height, width = img.shape
print "height and width : ",height, width
size = img.size
print "size of the image in number of pixels", size
# plot the binary image
cv2.imshow('binary',img)
当我运行此代码时,出现以下错误:
Traceback (most recent call last):
File "C:/Python27/BnW.py", line 9, in <module>
height, width = img.shape
ValueError: too many values to unpack
我的图像已经是二进制图像。我不希望在几个二进制图像中计数黑白像素... im newbie..am向您提供任何帮助。
该错误是因为img.shape
返回的元组大小大于或小于2,这是您在height, width = img.shape
。 在图像为numpy数组的情况下,如果是RGB图像, .shape()
返回3个值,因此您可以将其更改为
height, width, channels = img.shape
但如果GrayScale变成height, width = img.shape
可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.