如何使用Python计算图像上的彩色像素面积? 将numpy导入为np 从matplotlib导入cm 从matplotlib导入pyplot作为plt 导入图像 从scipy导入ndimage 导入图像,图像绘制 进口PIL 导入cv 进口cv2 从scipy.ndimage导入测量,形态学 从PIL导入图像 从numpy进口* 从scipy.ndimage导入筛选器 进口派拉布 img=np.asarray(Image.open('test.tif').convert('L'))#读取并转换图像 img=1*(img

如何使用Python计算图像上的彩色像素面积? 将numpy导入为np 从matplotlib导入cm 从matplotlib导入pyplot作为plt 导入图像 从scipy导入ndimage 导入图像,图像绘制 进口PIL 导入cv 进口cv2 从scipy.ndimage导入测量,形态学 从PIL导入图像 从numpy进口* 从scipy.ndimage导入筛选器 进口派拉布 img=np.asarray(Image.open('test.tif').convert('L'))#读取并转换图像 img=1*(img,python,image,opencv,simplecv,mahotas,Python,Image,Opencv,Simplecv,Mahotas,上面的代码给出了黑色背景上的白色像素,如何计算图像上的白色区域,然后将图像分割成100个矩形,并找到最小、最大和平均像素数的矩形?谢谢因为您的图像是二进制的,所以您只需对这些值求和即可获得白色像素的计数 import numpy as np from matplotlib import cm from matplotlib import pyplot as plt import Image from scipy import ndimage import Image, ImageDr

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

因为您的图像是二进制的,所以您只需对这些值求和即可获得白色像素的计数

 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()
从PIL导入图像
将numpy作为np导入
img=np.asarray(Image.open(“foo.jpg”).convert('L'))
img=1*(img<127)
m、 n=img.shape
#使用np.sum计算白色像素
打印(“{}个白色像素,总共{}个像素。”.format(img.sum(),m*n))
#使用切片对任何子零件进行计数,例如第300-320行和第400-440列
打印(“{}个矩形白色像素。”.format(img[300:320400:440].sum())

使用切片选择任何矩形,并在该部分使用sum()。

您能建议如何计算两个区域之间的距离,其中包含最小和最大白色像素吗?谢谢,我正在读你的书
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()))