python - 如何使用Python计算图像上的彩色像素区域?

标签 python image opencv simplecv mahotas

 import numpy as np
 from matplotlib import cm
 from matplotlib import pyplot as plt
 import Image
 from scipy import ndimage
 import Image, ImageDraw
 import PIL
 import cv
 import cv2
 from scipy.ndimage import measurements, morphology
 from PIL import Image
 from numpy import *
 from scipy.ndimage import filters
 import pylab

 img = np.asarray(Image.open('test.tif').convert('L')) #read and convert image
 img = 1 * (img < 127) # threshold

 plt.imshow(img, cmap=cm.Greys_r) # show as black and white
 plt.show()

上面的代码在黑色背景上给出了白色像素,如何计算图像上的白色区域,然后将图像分成100个矩形并找到具有最小,最大和平均像素数的矩形?谢谢

最佳答案

由于您的图像是二进制图像,因此您可以对这些值求和以得到白色像素的计数。

from PIL import Image
import numpy as np

img = np.asarray(Image.open("foo.jpg").convert('L'))
img = 1 * (img < 127)

m,n = img.shape

# use np.sum to count white pixels
print("{} white pixels, out of {} pixels in total.".format(img.sum(), m*n))

# use slicing to count any sub part, for example from rows 300-320 and columns 400-440
print("{} white pixels in rectangle.".format(img[300:320,400:440].sum()))

使用 slice 来挑选任何矩形,然后在该部分上使用sum()。

关于python - 如何使用Python计算图像上的彩色像素区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035046/

相关文章:

python - Pandas 根据每行的现有列获取新列的 bool 值

python - Sqlite 认为我缺少绑定(bind)

python - defaultdict无法识别python中的2位数字

c# - 我应该将图像作为二进制信息还是作为 FS 上的路径保存到数据库本身?

image - 需要一点数学帮助才能调整图像大小

python - 在Python openCV中获取视频每一帧的耗时

python - Spark groupByKey 替代方案

php - 在 Codeigniter 中将缩略图裁剪为正方形

c# - C# QueryFrame 中的 EmguCV 2.3.0 返回上一个查询的帧

android - OpenCV 高斯模糊在安卓上运行缓慢