簡體   English   中英

如何使用python計算二進制圖像中的黑白像素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM