为了账号安全,请及时绑定邮箱和手机立即绑定

将RGB图像转换为黑白PIL手部识别

将RGB图像转换为黑白PIL手部识别

慕村9548890 2022-06-02 10:41:48
我试图用 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;    endend这就是我写的代码:from PIL import Image as imimage = im.open('/Users/eitan/Desktop/eell.jpg')image = image.resize((50,50), im.NEAREST)grayScale = image.convert(mode='L')width, height = grayScale.sizemid_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] = 0grayScale.show()如果有人可以帮助我用python编写这段代码,那就太好了!
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超13个赞

你可以像这样处理它,我使用的是 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')

生成的色调蒙版:

//img1.sycdn.imooc.com//629824680001e32f03890525.jpg

产生的饱和度遮罩:

//img1.sycdn.imooc.com//629824730001cbbc03880529.jpg

结果值掩码:

//img1.sycdn.imooc.com//629824810001519e03840514.jpg

然后,您可以将掩码与或或组合在一起以获得更复杂的掩码组合。



查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信