如何用python计算二值图像中的黑白像素

2024-04-20 05:28:01 发布

您现在位置: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)

当我运行这段代码时,我得到以下结果错误:-

^{pr2}$

我的图像已经是一个二值图像了。我想计算几个二值图像中的黑白像素数。。。我是新手..愿意接受你能提供的任何帮助。。


Tags: ofthefrom图像imageimportimgsize
1条回答
网友
1楼 · 发布于 2024-04-20 05:28:01

错误是因为img.shape返回的元组大小大于或小于2,正如您在height, width = img.shape中所假设的那样。在图像为numpy数组的上下文中,.shape()在RGB图像的情况下返回3个值,因此您可以将其更改为

height, width, channels = img.shape

但是在灰度图像的情况下,height, width = img.shape可以工作。在

相关问题 更多 >