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}$我的图像已经是一个二值图像了。我想计算几个二值图像中的黑白像素数。。。我是新手..愿意接受你能提供的任何帮助。。
错误是因为
img.shape
返回的元组大小大于或小于2,正如您在height, width = img.shape
中所假设的那样。在图像为numpy数组的上下文中,.shape()
在RGB图像的情况下返回3个值,因此您可以将其更改为但是在灰度图像的情况下,
height, width = img.shape
可以工作。在相关问题 更多 >
编程相关推荐