我有以下Python测试代码,用于读取、阈值化和显示图像:
import cv2
import numpy as np
from matplotlib import pyplot as plt
# read image
img = cv2.imread('slice-309.png',0)
ret,thresh = cv2.threshold(img,0,230, cv2.THRESH_BINARY)
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
imgplot = plt.imshow(img, 'gray')
plt.show()
我想要计算图像中某个标签的像素数,例如黑色像素。我该怎么做?我查看了OpenCV的教程,但没有找到帮助 :-(
谢谢!
thresh
,它可能不是你想象中的那样。如果这行不通,就在这个问题上标记你喜欢的答案以显示已解决,并提出另一个问题展示你的代码。把那个问题的链接放在这里,我会更多地帮助你。 - Rick SmithcountNonZero(img == scalar_value);
? - Miki