- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图用 python 编写与此处所写相同的代码,但我的代码没有产生好的结果。我的目标是获取 RGB 图像,调整大小并转换为 YCbCr,然后将背景像素值设置为 0,将手部像素值设置为 1。有人可以帮我使用 PIL 在 Python 中编写这段代码吗?
(我正在尝试复制的代码,我在执行步骤 3-6 时遇到了一些问题)
function image_out = processSkinImage(filename)
Step 1...
% Read the image
original = imread(filename);
...
Step 2...
% Resize the image to 50x50
image_resized = imresize(original, scale);
[M N Z] = size(image_resized);
% Initialize the output image
image_out = zeros(height,width);
image_out = zeros(M,N);
...
Step 3...
% Convert the image from RGB to YCbCr
img_ycbcr = rgb2ycbcr(image_resized);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);
...
Step 4...
% Get the central color of the image
% Expected the hand to be in the central of the image
central_color = img_ycbcr(int32(M/2),int32(N/2),:);
Cb_Color = central_color(:,:,2);
Cr_Color = central_color(:,:,3);
% Set the range
Cb_Difference = 15;
Cr_Difference = 10;
...
Step 5...
% Detect skin pixels
[r,c,v] = find(Cb>=Cb_Color-Cr_Difference & Cb<=Cb_Color+Cb_Difference & Cr>=Cr_Color-Cr_Difference & Cr<=Cr_Color+Cr_Difference);
...
Step 6...
% Mark detected pixels
for i=1:match_count
image_out(r(i),c(i)) = 1;
end
end
这是我写的代码:
from PIL import Image as im
image = im.open('/Users/eitan/Desktop/eell.jpg')
image = image.resize((50,50), im.NEAREST)
grayScale = image.convert(mode='L')
width, height = grayScale.size
mid_pixel=grayScale.getpixel((width/2,height/2))
print (mid_pixel)
pixels = grayScale.load()
for i in range(grayScale.size[0]): # for every col:
for j in range(grayScale.size[1]): # For every row
if grayScale.getpixel((i,j)) < mid_pixel+40 and grayScale.getpixel((i,j)) > mid_pixel-15:
pixels[i,j] = 255
else:
pixels[i, j] = 0
grayScale.show()
This is an example of an image the code would get
And this is what the result should look like
如果有人能帮我用 python 编写这段代码,那就太好了!
最佳答案
您可以这样处理,我使用的是 HSV 色彩空间而不是 YCbCr 色彩空间:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open image and convert to HSV colourspace
im = Image.open('hand.png').convert('HSV')
# Convert to Numpy array
ni = np.array(im)
# Get H, S and V of central pixel - consider taking a median of a larger area here
h,s,v = ni[int(ni.shape[0]/2), int(ni.shape[1]/2)]
# Separate each channel to own array
H = ni[:,:,0]
S = ni[:,:,1]
V = ni[:,:,2]
# Permissible +/- tolerances on each channel
deltah = 20
deltas = 80
deltav = 50
# Make masks of pixels with acceptable H, S and V
hmask = np.where((H > h-deltah) & (H < h+deltah), 255, 0).astype(np.uint8)
smask = np.where((S > s-deltas) & (S < s+deltas), 255, 0).astype(np.uint8)
vmask = np.where((V > v-deltav) & (V < v+deltav), 255, 0).astype(np.uint8)
# Save as images for inspection
Image.fromarray(hmask).save('hmask.png')
Image.fromarray(smask).save('smask.png')
Image.fromarray(vmask).save('vmask.png')
生成的色相 mask :
生成的饱和度蒙版:
结果值掩码:
然后您可以将蒙版与或或组合在一起以获得更复杂的蒙版组合。
关于python - 将RGB图像转换为黑白PIL手部识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58051929/
在 python 解释器中: >>> import PIL >>> PIL.Image Traceback (most recent call last): File "", line 1, in
我在 Pillow 中遇到过这个奇怪的错误,其中导入名称 PIL 需要全部小写而不是全部大写,但我从未见过 pil在任何地方都使用小写字母。这意味着我使用的导入 PIL 的所有 python 包和文件
有没有办法使用 PIL 截取驻留在我的服务器上的指定 HTML/Javascript 页面的屏幕截图? 我想编写一个脚本来更改该 HTML 页面上的一些参数,然后让 PIL 对其进行屏幕截图。 有什么
这是我所做的描述。 我正在尝试使用 PIL 编写程序,但是在尝试导入它时(如下所示),出现错误(也如下所示)。 from PIL import Image Here is the error. Tra
我正在尝试对图像进行简单的裁剪。这是代码 from PIL.Image import Image def get_image_half(image, half="upper"): if hal
我是一名新的Python用户,也是“Stack Overflow”中的新用户,当我尝试编译 tensorflow 代码时,我遇到了一些问题,并且我无法从网站上找到答案,所以我想从这里获得一些帮助,先谢
我知道 stackoverflow 上已经有很多与此问题相关的问题,我已经阅读了所有问题,但我仍然没有成功解决此问题。我希望有人能帮我解决这个问题。 我已经安装并重新安装了 Pillow 10 次。我
我得到错误: --------------------------------------------------------------------------- ImportError
我是机器学习的初学者,所以我试图创建一个模型来识别 Keras 博客中引用的图像。我已经在 Windows 10 上安装了 Anaconda 3 以及所有软件包,如tensorflow、keras、s
我正在尝试使用过滤器 FIND_EDGES 从图片中获取边缘,它在我的 Windows PC 上工作,但是当我在我的 Raspberry Pi 上运行相同的代码时,它给出了图像模式错误的错误。 最佳答
这个问题在 Python 环境中有一些答案,但这些解决方案不适用于我的 RStudio 环境。这是我的代码: library(keras) library(tensorflow) use_condae
我正在使用 Mac OS x 10.10.3 Yosemite 和 Python 2.7.9 |Anaconda 2.2.0 (x86_64) 来处理很多 python 的东西。我正在使用 Eclip
我正在遵循这个 SageMaker 指南并使用 1.12 cpu docker 文件。 https://github.com/aws/sagemaker-tensorflow-serving-cont
`from PIL import Image, ImageDraw, ImageFont image = Image.new('RGB', (950, 250), color=(255, 255, 2
我知道如何从图片中找到边缘。但我希望轮廓边缘更厚,例如宽度 9。 from PIL import Image, ImageFilter image = Image.open('your
我有多个白色背景的 PNG 图像,并且图像的某些部分充满了图案(可能是不同的颜色,黑色、蓝色、红色、黄色等)。 如何使用 Python PIL 库将所有这些图像合并为一张图像,以便所有非白色部分出
目前我正在尝试裁剪以下地址下文件夹内的所有图像:C:\\Users\\xie\\Desktop\\tiff\\Bmp然后将它们重新保存到同一个文件夹中。下面是我试图试验的代码,两者都运行没有错误但什么
虽然它们非常相似,但我确信 Pearson 相关相似度和调整余弦相似度之间存在一些差异,因为所有的论文和网页都将它们分为两种不同的类型。 然而,它们都没有提供明确的定义。 Here是其中一页。 谁能说
这是我的forms.py: class UploadImageForm(forms.ModelForm): class Meta: model = UserImages
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!