MATLAB: Index exceeds matrix dimensions.I am working on hand gesture recognition.

uigetfile

When I take image from img from commands =
imread('C:\Users\Administrator\Desktop\Detect\F04U2MPIXQG15WQS.LARGE.jpg');
figure,imshow(img)
It works fine but when I use
[filename, pathname] = …
uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'Select Image File');
img=strcat(pathname,filename);
error shown is Error in skinDetect2Func (line 8)
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
Error in bwFingers1 (line 39)
out=skinDetect2Func(img);
Anyone can help me out I am just stuck in here. 🙁

Best Answer

It seems your image is a gray image..it doesn't have R, G and B channels. See to it that, you use a color image i.3 3D matrix as input. Try this function:
function region=skinDetect2func(img)
img = imread('peppers.png') ;
[m,n,p] = size(img) ;
if p ~=3
error('Please input a color image')
end
imshow(img);
sz=size(img);
r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
yuv(:,:,u) = img(:,:,r)-img(:,:,g);
yuv(:,:,v)=img(:,:,b)-img(:,:,g);
region = (yuv(:,:,u)>20 & yuv(:,:,v)<74) .* 255;