Coordinates of an object - Matlab - matlab

How can I find the top, button, left, right x,y coordinates of this object?
Code:
clc;
clear all;
Image = rgb2gray(imread('https://upload-icon.s3.us-east-2.amazonaws.com/uploads/icons/png/1606078271536061993-512.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
BW = padarray(BW,60,60,'both')
BW = imcomplement(BW);
imshow(BW)

The red pixels on the coordinate plot indicate the furthest most pixels to the left, right, top and bottom of the icon/image. This method similarly uses for-loops and break() statements to scan the image until a pixel of logical value "1" is found.
The scanning patterns include:
• Top → Scan image from top to bottom
• Bottom → Scan image from bottom to top
• Left → Scan image from left to right
• Right → Scan image from right to left
clc;
clear all;
Image = rgb2gray(imread('https://upload-icon.s3.us-east-2.amazonaws.com/uploads/icons/png/1606078271536061993-512.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
BW = padarray(BW,60,60,'both');
BW = imcomplement(BW);
[Image_Height,Image_Width] = size(BW);
%Top (scanning from top to bottom)%
for Row_Scanner = 1: +1: Image_Height
for Column_Scanner = 1: +1: Image_Width
Pixel_Value = BW(Row_Scanner,Column_Scanner);
if(Pixel_Value == 1)
Top = [Row_Scanner Column_Scanner];
break;
end
end
end
%Bottom (scanning from bottom to top)%
for Row_Scanner = Image_Height: -1: 1
for Column_Scanner = 1: +1: Image_Width
Pixel_Value = BW(Row_Scanner,Column_Scanner);
if(Pixel_Value == 1)
Bottom = [Row_Scanner Column_Scanner];
break;
end
end
end
%Left (scanning from left to right)%
for Column_Scanner = 1: +1: Image_Width
for Row_Scanner = 1: +1: Image_Height
Pixel_Value = BW(Row_Scanner,Column_Scanner);
if(Pixel_Value == 1)
Left = [Row_Scanner Column_Scanner];
break;
end
end
end
%Right (scanning from right to left)%
for Column_Scanner = Image_Width: -1: 1
for Row_Scanner = 1: +1: Image_Height
Pixel_Value = BW(Row_Scanner,Column_Scanner);
if(Pixel_Value == 1)
Right = [Row_Scanner Column_Scanner];
break;
end
end
end
Coordinates_Image = zeros(Image_Height,Image_Width,3);
Coordinates_Image(Top(1,1),Top(1,2),1) = 255;
Coordinates_Image(Bottom(1,1),Bottom(1,2),1) = 255;
Coordinates_Image(Left(1,1),Left(1,2),1) = 255;
Coordinates_Image(Right(1,1),Right(1,2),1) = 255;
Top_X = Top(1,2);
Top_Y = Top(1,1);
Bottom_X = Bottom(1,2);
Bottom_Y = Bottom(1,1);
Left_X = Left(1,2);
Left_Y = Left(1,1);
Right_X = Right(1,2);
Right_Y = Right(1,1);
fprintf("Top coordinate is (x,y) -> (%d,%d)\n",Top_X,Top_Y);
fprintf("Bottom coordinate is (x,y) -> (%d,%d)\n",Bottom_X,Bottom_Y);
fprintf("Left coordinate is (x,y) -> (%d,%d)\n",Left_X,Left_Y);
fprintf("Right coordinate is (x,y) -> (%d,%d)\n",Right_X,Right_Y);
subplot(1,2,1); imshow(BW);
title('Image');
subplot(1,2,2); imshow(Coordinates_Image);
title('Coordinate Plot');
xlabel('X-Axis'); ylabel('Y-Axis');

You can do it using a loop. Find the first/last rows and columns whose sum is non-zero and break the loop once the condition is met.
BW = imcomplement(BW);
[r,c] = size(BW);
% Top cordinate: Find first row with non-zero sum
for i=1:r
top = i;
if sum(BW(i,:))~= 0
break
end
end
% Bottom cordinate: Find last row with non zero sum
for i=r:-1:1
bottom = i;
if sum(BW(i,:))~= 0
break
end
end
% Left cordinate: Find first column with non zero sum
for i=1:c
left = i;
if sum(BW(:,i))~= 0
break
end
end
% Right cordinate: find last column with non-zero sum
for i=c:-1:1
right = i;
if sum(BW(:,i))~= 0
break
end
end

Related

How do I rotate image within an image?

I am trying to develop a program that will superimpose a 100x100 pixel image onto a 200x200 pixel background. The user will be prompted to move the smaller image (left,right,up,down) and/or rotate CCW/CW by an arbitrary theta val. My question is simply, 'how do you rotate the smaller image within the larger?'. I have tried using imrotate on the smaller image, and having the larger equal to the smaller vals.
Thanks
a = zeros(15);
b = a(7:9,7:9);
b(:) = 1; %initialize b matrix to ones
a(7:9,7:9) = b; %center matrix
n = 1;
while n ~= 0
n = input('PLLRAFM Aligner\n Please enter a command to align image.\n 8: up\n 2: down\n 4: left\n 6: right\n 7: rotate CCW\n 9: rotate CW\n 0: save image\n');
switch n
case 8 %up
index = sub2ind(size(a),find(a == 1));
[row, col] = ind2sub(size(a),index);
a = zeros(15);
row = row - 1;
a(row,col) = 1;
figure(2)
imagesc(a)
case 2 %down
index = sub2ind(size(a),find(a == 1));
[row, col] = ind2sub(size(a),index);
a = zeros(15);
row = row + 1;
a(row,col) = 1;
figure(2)
imagesc(a)
case 4 %left
index = sub2ind(size(a),find(a == 1));
[row, col] = ind2sub(size(a),index);
a = zeros(15);
col = col - 1;
a(row,col) = 1;
figure(2)
imagesc(a)
case 6 %right
index = sub2ind(size(a),find(a == 1));
[row, col] = ind2sub(size(a),index);
a = zeros(15);
col = col + 1;
a(row,col) = 1;
figure(2)
imagesc(a)
case 7 %rotate CCW
index = sub2ind(size(a),find(a == 1));
theta = 45; %temporary rotation of 1 degree
imrotate(b,theta);
a(b) = 1;
figure(2)
imagesc(a)
case 9 %rotate CW
% index = sub2ind(size(a),find(a == 1));
% [row, col] = ind2sub(size(a),index);
% theta = 45; %temporary rotation of 1 degree
% b = imrotate(a(row,col),theta);
% figure(2)
% imagesc(a)
otherwise
fprintf('Please try again.');
end
end
I would like to rotate this yellow block by 45 degrees for testing.
This should do what you want, if I understood you correctly:
a=zeros(15);
b=ones(3);
b=imrotate(b,45);
a(ceil(length(a)/2)-floor(length(b)/2):ceil(length(a)/2)+floor(length(b)/2),...
ceil(length(a)/2)-floor(length(b)/2):ceil(length(a)/2)+floor(length(b)/2))=b;
imagesc(a);

How can I discard some lines detected by Hough?

The following is my original picture,
This is my Hough code,
function [hough_lines, H, T, R, P] = get_hough_lines( input_image , peaks)
binary_image = edge(input_image,'canny');
[H,T,R] = hough(binary_image);
P = houghpeaks(H, peaks, 'threshold', ceil(0.3*max(H(:))));
hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);
end
Usage
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines(input_image, max_peaks);
draw_hough_lines(input_image, hough_lines);
The following is my line-detection picture,
As you can see, my Hough source code detects 3 of the most prominent lines.
I want to discard the bottom line.
So, I have written the following code to discard some area from the image,
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 125;
% discard bottom
output_image((height-pixel_count):height, :) = 125;
% discard left
output_image(:,1:pixel_count) = 125;
% discard right
output_image(:,(width-pixel_count):width) = 125;
end
Usage
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines( discard_image_area(input_image,
discard_pixels), max_peaks);
draw_hough_lines(input_image, hough_lines);
But, the result becomes worse,
Relevant Source Code,
function draw_hough_lines(rotI, lines )
imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
end
I solved this problem.
I used 0 in place of 125.
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 0;
% discard bottom
h = height - pixel_count;
output_image(h:height, :) = 0;
% discard left
output_image(:,1:pixel_count) = 0;
% discard right
output_image(:,(width-pixel_count):width) = 0;
end

how to find the corners of rotated object in matlab?

I want to find the corners of objects.
I tried the following code:
Vstats = regionprops(BW2,'Centroid','MajorAxisLength','MinorAxisLength',...
'Orientation');
u = [Vstats.Centroid];
VcX = u(1:2:end);
VcY = u(2:2:end);
[VcY id] = sort(VcY); % sorting regions by vertical position
VcX = VcX(id);
Vstats = Vstats(id); % permute according sort
Bv = Bv(id);
Vori = [Vstats.Orientation];
VRmaj = [Vstats.MajorAxisLength]/2;
VRmin = [Vstats.MinorAxisLength]/2;
% find corners of vertebrae
figure,imshow(BW2)
hold on
% C = corner(VER);
% plot(C(:,1), C(:,2), 'or');
C = cell(size(Bv));
Anterior = zeros(2*length(C),2);
Posterior = zeros(2*length(C),2);
for i = 1:length(C) % for each region
cx = VcX(i); % centroid coordinates
cy = VcY(i);
bx = Bv{i}(:,2); % edge points coordinates
by = Bv{i}(:,1);
ux = bx-cx; % move to the origin
uy = by-cy;
[t, r] = cart2pol(ux,uy); % translate in polar coodinates
t = t - deg2rad(Vori(i)); % unrotate
for k = 1:4 % find corners (look each quadrant)
fi = t( (t>=(k-3)*pi/2) & (t<=(k-2)*pi/2) );
ri = r( (t>=(k-3)*pi/2) & (t<=(k-2)*pi/2) );
[rp, ip] = max(ri); % find farthest point
tc(k) = fi(ip); % save coordinates
rc(k) = rp;
end
[xc,yc] = pol2cart(tc+1*deg2rad(Vori(i)) ,rc); % de-rotate, translate in cartesian
C{i}(:,1) = xc + cx; % return to previous place
C{i}(:,2) = yc + cy;
plot(C{i}([1,4],1),C{i}([1,4],2),'or',C{i}([2,3],1),C{i}([2,3],2),'og')
% save coordinates :
Anterior([2*i-1,2*i],:) = [C{i}([1,4],1), C{i}([1,4],2)];
Posterior([2*i-1,2*i],:) = [C{i}([2,3],1), C{i}([2,3],2)];
end
My input image is :
I got the following output image
The bottommost object in the image is not detected properly. How can I correct the code? It fails to work for a rotated image.
You can get all the points from the image, and use kmeans clustering and partition the points into 8 groups. Once partition is done, you have the points in and and you can pick what ever the points you want.
rgbImage = imread('your image') ;
%% crop out the unwanted white background from the image
grayImage = min(rgbImage, [], 3);
binaryImage = grayImage < 200;
binaryImage = bwareafilt(binaryImage, 1);
[rows, columns] = find(binaryImage);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
% Crop
croppedImage = rgbImage(row1:row2, col1:col2, :);
I = rgb2gray(croppedImage) ;
%% Get the white regions
[y,x,val] = find(I) ;
%5 use kmeans clustering
[idx,C] = kmeans([x,y],8) ;
%%
figure
imshow(I) ;
hold on
for i = 1:8
xi = x(idx==i) ; yi = y(idx==i) ;
id1=convhull(xi,yi) ;
coor = [xi(id1) yi(id1)] ;
[id,c] = kmeans(coor,4) ;
plot(coor(:,1),coor(:,2),'r','linewidth',3) ;
plot(c(:,1),c(:,2),'*b')
end
Now we are able to capture the regions..the boundary/convex hull points are in hand. You can do what ever math you want with the points.
Did you solve the problem? I Looked into it and it seems that the rotation given by 'regionprops' seems to be off. To fix that I've prepared a quick solution: I've dilated the image to close the gaps, found 4 most distant peaks of each spine, and then validated if a peak is on the left, or on the right of the centerline (that I have obtained by extrapolating form sorted centroids). This method seems to work for this particular problem.
BW2 = rgb2gray(Image);
BW2 = imbinarize(BW2);
%dilate and erode will help to remove extra features of the vertebra
se = strel('disk',4,4);
BW2_dilate = imdilate(BW2,se);
BW2_erode = imerode(BW2_dilate,se);
sb = bwboundaries(BW2_erode);
figure
imshow(BW2)
hold on
centerLine = [];
corners = [];
for bone = 1:length(sb)
x0 = sb{bone}(:,2) - mean(sb{bone}(:,2));
y0 = sb{bone}(:,1) - mean(sb{bone}(:,1));
%save the position of the centroid
centerLine = [centerLine; [mean(sb{bone}(:,1)) mean(sb{bone}(:,2))]];
[th0,rho0] = cart2pol(x0,y0);
%make sure that the indexing starts at the dip, not at the corner
lowest_val = find(rho0==min(rho0));
rho1 = [rho0(lowest_val:end); rho0(1:lowest_val-1)];
th00 = [th0(lowest_val:end); th0(1:lowest_val-1)];
y1 = [y0(lowest_val:end); y0(1:lowest_val-1)];
x1 = [x0(lowest_val:end); x0(1:lowest_val-1)];
%detect corners, using smooth data to remove noise
[pks,locs] = findpeaks(smooth(rho1));
[pksS,idS] = sort(pks,'descend');
%4 most pronounced peaks are where the corners are
edgesFndCx = x1(locs(idS(1:4)));
edgesFndCy = y1(locs(idS(1:4)));
edgesFndCx = edgesFndCx + mean(sb{bone}(:,2));
edgesFndCy = edgesFndCy + mean(sb{bone}(:,1));
corners{bone} = [edgesFndCy edgesFndCx];
end
[~,idCL] = sort(centerLine(:,1),'descend');
centerLine = centerLine(idCL,:);
%extrapolate the spine centerline
yDatExt= 1:size(BW2_erode,1);
extrpLine = interp1(centerLine(:,1),centerLine(:,2),yDatExt,'spline','extrap');
plot(centerLine(:,2),centerLine(:,1),'r')
plot(extrpLine,yDatExt,'r')
%find edges to the left, and to the right of the centerline
for bone = 1:length(corners)
x0 = corners{bone}(:,2);
y0 = corners{bone}(:,1);
for crn = 1:4
xCompare = extrpLine(y0(crn));
if x0(crn) < xCompare
plot(x0(crn),y0(crn),'go','LineWidth',2)
else
plot(x0(crn),y0(crn),'ro','LineWidth',2)
end
end
end
Solution

Changing color of plot lines in a loop

I have a question about changing the color of lines in a plot in MATLAB. I have written a code where I want to change the color of lines in a plot that is embedded in several for loops (the code is shown below). In the code, a new plot is created (with its own figure) when the "if loop" condition is satisfied. I want each plot to have lines with different colors (from other plots), so I created a variable = "NewColor" to increment and change the line colors. However, the following problem has been occurring:
Suppose I am in debug mode and I have stopped at the plot command. I run the next step and a plot is created with a blue line. I check the value of NewColor and find NewColor = 0.1. Next, I use the "run to cursor" command to step to the next time the plot command is activated. When I do this I am still within the "i for loop", so NewColor has not changed. I check the editor to find that NewColor = 0.1. Therefore, when I run the next step a blue line should show up on the current plot. To the contrary and my disbelief an orange line shows up (in addition to the blue line). I don't understand since in both steps of the debugger NewColor = 0.1, and the code is written so the color of the lines = [0,NewColor,0]. If anyone can find the error of my ways it would be greatly appreciated. Thanks
ThetaPlot = [40,50]; % Put incident angle as input
Count1 = 0;
Count2 = 0;
NewColor = 0;
for m = 1:length(ThetaPlot)
NewColor = 0.1;
Title = sprintf('Angle(%d)',ThetaPlot(m));
figure('name',Title)
Count1 = 0;
for i = 1:length(xrange)-1 % X Coordinate of Start Node
for j = 1:length(yrange)-1 % Y Coordinate of Start Node
Count1 = Count1+1;
for k = 2:length(xrange) % X Coordinate of End Node
for l = 2:length(yrange) % Y Coordinate of End Node
Count2 = Count2+1;
if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
x = [xrange(i),xrange(k)];
y = [yrange(j),yrange(l)];
plot(x,y,['-',[0,NewColor,0],'o']);
hold on;
end
end
Count2 = 0;
end
end
end
NewColor = NewColor + 0.02;
end
Full Code:
%% Calculating Angles of Reflection
run = 1; % Set run = 1 for calculations
if run == 1
xrange = [0:1:14.5]'; % Coordinates to try for Panel Geometry (in)
yrange = [0:1:36]'; % Coordinates to try for Panel Geometry (in)
ThetaIncident = [-90:1:90]'; % Incident Angle of Ray (measured relative to normal direction with clockwise postive)
OvenGlassXrange = [14.5:0.1:36.5]; %Range of X coordinates for Oven Glass
ReflRayData = zeros((length(xrange)-1)*(length(yrange)-1),length(ThetaIncident),(length(xrange)-1)*(length(yrange)-1)); % Matrix containing Reflected Ray Data
Count1 = 0;
Count2 = 0;
for i = 1:length(xrange)-1 % X Coordinate of Start Node
for j = 1:length(yrange)-1 % Y Coordinate of Start Node
Count1 = Count1+1;
for k = 2:length(xrange) % X Coordinate of End Node
for l = 2:length(yrange) % Y Coordinate of End Node
Count2 = Count2+1;
for m = 1:length(ThetaIncident)
xStart = xrange(i);
yStart = yrange(j);
xEnd = xrange(k);
yEnd = yrange(l);
m1 = (yEnd - yStart)/(xEnd - xStart); % Slope between Start and End Nodes
b1 = yStart - m1*xStart;
m2 = 1/m1; % Slope of normal direction
b2 = (yEnd - 0.5*(yEnd - yStart)) - m2*(xEnd - 0.5*(xEnd - xStart));
ArbXCoor = 1; % Arbitary Point X Coordinate on Normal Line
ArbYCoor = m2*ArbXCoor+b2; % Arbitary Point Y Coordinate on Normal Line
ThetaReflected = -ThetaIncident(m); % Reflected Angle
ArbXCoorRot = ArbXCoor*cosd(ThetaReflected) - ArbYCoor*sind(ThetaReflected); % Arbitary Point X Coordinate on Reflected Line
ArbYCoorRot = ArbYCoor*cosd(ThetaReflected) + ArbXCoor*sind(ThetaReflected); % Arbitary Point Y Coordinate on Reflected Line
m3 = (ArbYCoorRot - (yEnd - 0.5*(yEnd - yStart)))/(ArbXCoorRot - (xEnd - 0.5*(xEnd - xStart))); % Slope of Reflected Line
b3 = (yEnd - 0.5*(yEnd - yStart)) - m3*(xEnd - 0.5*(xEnd - xStart));
ElemLength = sqrt((yEnd - yStart)^2 + (xEnd - xStart)^2);
if min(OvenGlassXrange) < -b3/m3 && -b3/m3 < max(OvenGlassXrange) && -1 < m1 && m1 < 0 && m1 ~= -Inf && m1 ~= Inf && ElemLength < 3
ReflRayData(Count2,m,Count1) = 1;
end
end
end
end
Count2 = 0;
end
end
%% Plotting
ThetaPlot = [40,50]; % Put incident angle as input
Count1 = 0;
Count2 = 0;
NewColor = 0;
for m = 1:length(ThetaPlot)
NewColor = 0.1;
Title = sprintf('Angle(%d)',ThetaPlot(m));
figure('name',Title)
Count1 = 0;
for i = 1:length(xrange)-1 % X Coordinate of Start Node
for j = 1:length(yrange)-1 % Y Coordinate of Start Node
Count1 = Count1+1;
for k = 2:length(xrange) % X Coordinate of End Node
hold on;
for l = 2:length(yrange) % Y Coordinate of End Node
Count2 = Count2+1;
if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
x = [xrange(i),xrange(k)];
y = [yrange(j),yrange(l)];
plot(x,y,['-',[0,NewColor,0],'o']);
hold on;
end
end
Count2 = 0;
end
end
end
NewColor = NewColor + 0.02;
end
instead of plot(x,y,['-',[0,NewColor,0],'o']); try:
plot(x,y,'linestyle','-','marker','o','color',[0,NewColor,0])
According to the Matlab documentation, by calling hold on Matlab uses the next color.
hold on retains plots in the current axes so that new plots added to the axes do not delete existing plots. New plots use the next colors and line styles based on the ColorOrder and LineStyleOrder properties of the axes.
Therefore, in your code when you call hold on inside the for, it just uses the next color.
My solution is to put figure; hold on; before for loop and remove that one in you loop

Finger peak detection using MATLAB

I have to create an algorithm with Matlab that, with a image of a hand, can know the form of the hand by the number of raised fingers and the presence or absence of the thumb. So far, the algorithm is almost complete but I don't know what more I can do that could find the peaks that represents the fingers. We tried a lot of things but nothing works. The idea is to find when there is a sudden increasement but as the pixels are never completely aligned, nothing that we tried worked. Someone has any idea? Here is the code so far.
The image that he is reading is this one:
To know if the finger is relevant or not, we already have an idea that might work... but we need to find the fingers first.
clear all
close all
image=imread('mao2.jpg');
YCBCR = rgb2ycbcr(image);
image=YCBCR;
cb = image(:,:,2);
cr = image(:,:,3);
imagek(:,1) = cb(:);
imagek(:,2) = cr(:);
imagek = double(imagek);
[IDX, C] = kmeans(imagek, 2, 'EmptyAction', 'singleton');
s=size(image);
IDX= uint8(IDX);
C2=round(C);
imageNew = zeros(s(1),s(2));
temp = reshape(IDX, [s(1) s(2)]);
for i = 1 : 1 : s(1)
for j = 1 : 1 : s(2)
imageNew(i,j,:) = C2(temp(i,j));
end
end
imageNew=uint8(imageNew);
[m,n]=size(imageNew);
for i=1:1:m
for j = 1:1:n
if(imageNew(i,j)>=127)
pretobranco(i,j)=0;
else
pretobranco(i,j)=1;
end
end
end
I2=imfill(pretobranco);
imshow(I2);
imwrite(I2, 'mao1trab.jpg');
[m,n]=size(I2);
B=edge(I2);
figure
imshow(B);
hold on;
stats=regionprops(I2,'BoundingBox');
rect=rectangle('position', [stats(1).BoundingBox(1), stats(1).BoundingBox(2), stats(1).BoundingBox(3), stats(1).BoundingBox(4)], 'EdgeColor', 'r');
stats(1).BoundingBox(1)
stats(1).BoundingBox(2)
stats(1).BoundingBox(3)
stats(1).BoundingBox(4)
figure
Bound = B( stats(1).BoundingBox(2): stats(1).BoundingBox(2)+stats(1).BoundingBox(4)-1, stats(1).BoundingBox(1):stats(1).BoundingBox(1)+stats(1).BoundingBox(3)-1);
imshow(Bound)
y1 = round(stats(1).BoundingBox(2))
y2 = round(stats(1).BoundingBox(2)+stats(1).BoundingBox(4)-1)
x1 = round(stats(1).BoundingBox(1))
x2 = round(stats(1).BoundingBox(1)+stats(1).BoundingBox(3)-1)
% Bounding box contida em imagem[M, N].
[M,N] = size(Bound)
vertical=0;
horizontal=0;
if M > N
vertical = 1 %imagem vertical
else
horizontal = 1 %imagem horizontal
end
%Find thumb
MaoLeft = 0;
MaoRight = 0;
nPixelsBrancos = 0;
if vertical==1
for i = x1:1:x2
for j= y1:1:y2
if I2(j,i) == 1
nPixelsBrancos = nPixelsBrancos + 1; %Numero de pixels da mão
end
end
end
for i=x1:1:x1+30
for j=y1:1:y2
if I2(j,i) == 1
MaoLeft = MaoLeft + 1; %Number of pixels of the hand between the 30 first colums
end
end
end
for i=x2-30:1:x2
for j=y1:1:y2
if I2(j,1) == 1
MaoRight = MaoRight + 1; %Number of pixels of the hand between the 30 last colums
end
end
end
TaxaBrancoLeft = MaoLeft/nPixelsBrancos
TaxaBrancoRight = MaoRight/nPixelsBrancos
if TaxaBrancoLeft <= (7/100)
if TaxaBrancoRight <= (7/100)
Thumb = 0 %Thumb in both borders is defined as no Thumb.
else
ThumbEsquerdo = 1 %Thumb on left
end
end
if TaxaBrancoRight <= (7/100) && TaxaBrancoLeft >= (7/100)
ThumbDireito = 1 %Thumb on right
end
end
if horizontal==1
for i = x1:1:x2
for j= y1:y2
if I2(i,j) == 1
nPixelsBrancos = nPixelsBrancos + 1; %Numero de pixels da mão
end
end
end
for i=x1:1:x2
for j=y1:1:y1+30
if I2(i,j) == 1
MaoLeft = MaoLeft + 1; %Numero de pixels da mão entre as 30 primeiras colunas
end
end
end
for i=x1:1:x2
for j=y2-30:1:y2
if I2(j,1) == 1
MaoRight = MaoRight + 1; %Numero de pixels da mão entre as 30 ultimas colunas
end
end
end
TaxaBrancoLeft = MaoLeft/nPixelsBrancos
TaxaBrancoRight = MaoRight/nPixelsBrancos
if TaxaBrancoLeft <= (7/100)
if TaxaBrancoRight <= (7/100)
Thumb = 0 %Polegar nas duas bordas. Definimos como sem polegar.
else
ThumbEsquerdo = 1 %Polegar na borda esquerda
end
end
if TaxaBrancoRight <= (7/100) && TaxaBrancoLeft >= (7/100)
ThumbDireito = 1 %Polegar na borda direita
end
end
figure
imshow(I2);
%detecção da centroid
Ibw = im2bw(I2);
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'centroid');
figure
imshow(I2); hold on;
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end
centroid = [stat(x).Centroid(1) stat(x).Centroid(2)] %coordenadas x e y da centroid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Seemed like an interesting problem, so I gave it a shot. Basically you start with a Sobel filter to find the edges in your image (after slight denoising). Then clean up the resulting lines, use them to separate regions within your binary mask of the hand, use a watershed transform to find the wrist, some distance transforms to find other landmarks, then remove the palm. What you're left with is separate regions for each finger and thumb. You can count those regions easily enough or find which way they are pointing, or whatever you'd like.
imgURL = 'https://encrypted-tbn2.gstatic.com/imgs?q=tbn:ANd9GcRQsqJtlrOnSbJNTnj35Z0uG9BXsecX2AXn1vV0YDKodq-zSuqnnQ';
imgIn=imread(imgURL);
gaussfilt = fspecial('gaussian', 3, .5); % Blur starting image
blurImg = imfilter(double(img(:,:,1)), gaussfilt);
edgeImg = edge(blurImg, 'sobel'); % Use Sobel edge filter to pick out contours of hand + fingers
% Clean up contours
edgeImg = bwmorph(edgeImg, 'close', 1);
edgeImg = bwmorph(edgeImg, 'thin', Inf);
% Clean up rogue spots in corners
edgeImg([2 end-1], 2) = 0;
edgeImg([2 end-1], end-1) = 0;
% Extend lines to edge of image (correct for 'close' operation above
edgeImg([1 end],:) = edgeImg([2 end-1],:);
edgeImg(:, [1 end]) = edgeImg(:, [2 end-1]);
% Remove all but the longest line
regs = regionprops(edgeImg, 'Area', 'PixelIdxList');
regs(vertcat(regs.Area) ~= max(vertcat(regs.Area))) = [];
lineImg = false(size(edgeImg, 1), size(edgeImg, 2));
lineImg(regs.PixelIdxList) = 1;
fillImg = edgeImg;
% Close in wrist
if any(fillImg(1,:))
fillImg(1,:) = 1;
end
if any(fillImg(end,:))
fillImg(end,:) = 1;
end
if any(fillImg(:,1))
fillImg(:,1) = 1;
end
if any(fillImg(:,end))
fillImg(:,end) = 1;
end
fillImg = imfill(fillImg, 'holes');
fillImg([1 end], :) = 0;
fillImg(:, [1 end]) = 0;
fillImg([1 end],:) = fillImg([2 end-1],:);
fillImg(:, [1 end]) = fillImg(:, [2 end-1]);
% Start segmenting out hand + fingers
handBin = fillImg;
% Set lines in above image to 0 to separate closely-spaced fingers
handBin(lineImg) = 0;
% Erode these lines to make fingers a bit more separate
handBin = bwmorph(handBin, 'erode', 1);
% Segment out just hand (remove wrist)
distImg = bwdist(~handBin);
[cDx, cDy] = find(distImg == max(distImg(:)));
midWrist = distImg;
midWrist = max(midWrist(:)) - midWrist;
midWrist(distImg == 0) = Inf;
wristWatershed = watershed(imerode(midWrist, strel('disk', 10)));
whichRegion = wristWatershed(cDx, cDy);
handBin(wristWatershed ~= whichRegion) = 0;
regs = regionprops(handBin, 'Area', 'PixelIdxList');
regs(vertcat(regs.Area) ~= max(vertcat(regs.Area))) = [];
handOnly = zeros(size(handBin, 1), size(handBin, 2));
handOnly(regs.PixelIdxList) = 1;
% Find radius of circle around palm centroid that excludes wrist and splits
% fingers into separate regions.
% This is estimated as D = 1/3 * [(Centroid->Fingertip) + 2*(Centroid->Wrist)]
% Find Centroid-> Wrist distance
dist2w = wristWatershed ~= whichRegion;
dist2w = bwdist(dist2w);
distToWrist = dist2w(cDx, cDy);
% Find Centroid-> Fingertip distance
dist2FE = zeros(size(handOnly, 1), size(handOnly, 2));
dist2FE(cDx, cDy) = 1;
dist2FE = bwdist(dist2FE).*handOnly;
distToFingerEnd = max(dist2FE(:));
circRad = mean([distToFingerEnd, distToWrist, distToWrist]); % Estimage circle diameter
% Draw circle
X = bsxfun(#plus,(1:size(handOnly, 1))',zeros(1,size(handOnly, 2)));
Y = bsxfun(#plus,(1:size(handOnly, 2)),zeros(size(handOnly, 1),1));
B = sqrt(sum(bsxfun(#minus,cat(3,X,Y),reshape([cDx, cDy],1,1,[])).^2,3))<=circRad;
% Cut out binary mask within circle
handOnly(B) = 0;
% Label separate regions, where each now corresponds to a separate digit
fingerCount = bwlabel(handOnly);
% Display overlay image
figure()
imshow(imgIn)
hold on
overlayImg = imshow(label2rgb(fingerCount, 'jet', 'k'));
set(overlayImg, 'AlphaData', 0.5);
hold off
Results:
http://imgur.com/ySn1fPy