【发布时间】:2020-01-22 21:40:46
【问题描述】:
我尝试用 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写这段代码就太好了!
【问题讨论】:
-
正如我在对您的相关question 的评论中所说,我认为您将需要使用像素 color,而不仅仅是灰度强度(因为它会丢弃信息太多),以确定哪些是手的一部分,哪些不是。问题Is there an easy way to compare how close two colors are to each other? 可能会有所帮助。
-
正如@martineau 所说,丢弃颜色可能不是寻找与特定肤色/颜色相似的像素的最佳方法。例如,也许可以考虑找到中心像素的 HSL 值,然后找到具有相似色相和饱和度的像素。
标签: python python-imaging-library