如何以编程方式(最好在python中使用PIL)计算具有剥离背景的对象的像素总数? [英] How to programmatically (preferably using PIL in python) calculate the total number of pixels of an object with a stripped background?

查看:159
本文介绍了如何以编程方式(最好在python中使用PIL)计算具有剥离背景的对象的像素总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多张图片,每张图片都有一个对象,并且背景被删除了.图片的尺寸为500x400像素.

我正在寻找一种以编程方式(最好使用python)计算图片内部(没有背景的空间内)的图像像素总数的方法.

我在Python中使用了PIL包来获取图像对象的尺寸,如下所示:

print(image.size)

此命令成功生成了整个图片的尺寸(500x400像素),但未生成图片内感兴趣对象的尺寸.

有人知道如何使用python计算图片内对象的尺寸吗?图片的示例嵌入在下面.

解决方案

您可以使用一些图像中不存在的颜色来填充背景像素,例如洋红色,然后计算洋红色像素,然后从图像的像素数量(宽x高)中减去该数量.

这里是一个例子:

#!/usr/bin/env python3

from PIL import Image, ImageDraw
import numpy as np

# Open the image and ensure RGB
im = Image.open('man.png').convert('RGB')

# Make all background pixels magenta
ImageDraw.floodfill(im,xy=(0,0),value=(255,0,255),thresh=50)

# Save for checking
im.save('floodfilled.png')

# Make into Numpy array
n = np.array(im)

# Mask of magenta background pixels
bgMask =(n[:, :, 0:3] == [255,0,255]).all(2)
count = np.count_nonzero(bgMask)

# Report results
print(f"Background pixels: {count} of {im.width*im.height} total")

示例输出

Background pixels: 148259 of 199600 total

不确定手臂和身体之间的封闭区域对您有多重要...如果不使用洪水填充技术而只更换所有灰色,则会冒出洋红色衬衫并将其计为背景的风险. /p>

I have multiple pictures, each of which has an object with its background removed. The pictures are 500x400 pixels in size.

I am looking for a way to programmatically (preferably using python) calculate the total number of pixels of the image inside the picture (inside the space without the background).

I used the PIL package in Python to get the dimensions of the image object, as follows:

print(image.size)

This command successfully produced the dimensions of the entire picture (500x400 pixels) but not the dimensions of the object of interest inside the picture.

Does anyone know how to calculate the dimensions of an object inside a picture using python? An example of a picture is embedded below.

解决方案

You could floodfill the background pixels with some colour not present in the image, e.g. magenta, then count the magenta pixels and subtract that number from number of pixels in image (width x height).

Here is an example:

#!/usr/bin/env python3

from PIL import Image, ImageDraw
import numpy as np

# Open the image and ensure RGB
im = Image.open('man.png').convert('RGB')

# Make all background pixels magenta
ImageDraw.floodfill(im,xy=(0,0),value=(255,0,255),thresh=50)

# Save for checking
im.save('floodfilled.png')

# Make into Numpy array
n = np.array(im)

# Mask of magenta background pixels
bgMask =(n[:, :, 0:3] == [255,0,255]).all(2)
count = np.count_nonzero(bgMask)

# Report results
print(f"Background pixels: {count} of {im.width*im.height} total")

Sample Output

Background pixels: 148259 of 199600 total

Not sure how important the enclosed areas between arms and body are to you... if you just replace all greys without using the flood-filling technique, you risk making, say, the shirt magenta and counting that as background.

这篇关于如何以编程方式(最好在python中使用PIL)计算具有剥离背景的对象的像素总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆