How can I detect 8 corners of 2 squares by using harris algorithm?

Hi, I am using the following program to detect the 8 corners of 2 squares of the hand movement. The selection of images and Harris algorithm is working automatically for the images.
% Demo to find the squares % By ImageAnalyst % clc; % Clear command window. close all; % Close all figure windows except those created by imtool. imtool close all; % Close all figure windows created by imtool. workspace; % Make sure the workspace panel is showing. fontSize = 16;
% Read in a standard MATLAB color demo image. folder = 'D:\Documents\Research\December 1st week/Select Image with loop';
N = 15; IMAGES = cell(1,N); FNAMEFMT = 'image_%d.jpg';
% Load images for i=1:N; IMAGES{i} = imread(sprintf(FNAMEFMT, i));
figure,imshow(IMAGES{i}) end
for i=1:N IMAGES{i}= imread(sprintf(FNAMEFMT, i)); %baseFileName=IMAGES{i}; end
for i= 1:N baseFileName = IMAGES{i};
%baseFileName = 'image_2.jpg';
%fullFileName = fullfile(folder, baseFileName); % Get the full filename, with path prepended. %fullFileName = fullfile(folder, baseFileName); %if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it. fullFileName = baseFileName; % No path this time. % if ~exist(fullFileName, 'file') % Still didn't find it. Alert user. %errorMessage = sprintf('Error: %s does not exist.', fullFileName); %uiwait(warndlg(errorMessage)); %return; % end %end %rgbImage = imread(fullFileName); rgbImage=IMAGES{i}; % Get the dimensions of the image. numberOfColorBands should be = 3. [rows columns numberOfColorBands] = size(rgbImage); % Display the original color image. %subplot(2, 2, 1); %imshow(rgbImage, []); %title('Original Color Image', 'FontSize', fontSize); % Enlarge figure to full screen. % set(gcf, 'Position', get(0,'Screensize')); set(gcf, 'units','normalized','outerposition',[0 0 1 1])
% Extract the individual red, green, and blue color channels. redChannel = rgbImage(:, :, 1); % greenChannel = rgbImage(:, :, 2); % blueChannel = rgbImage(:, :, 3);
% Threshold the red channel image binaryImage = redChannel < 75; % Display the image. %subplot(2, 2, 2); %imshow(binaryImage, []); %title('Binary Image', 'FontSize', fontSize);
% Clean up. First remove stuff touching the border. binaryImage2 = imclearborder(binaryImage); % Display the image. %subplot(2, 2, 3); %imshow(binaryImage2, []); %title('Binary Image 2', 'FontSize', fontSize);
% Still some debris. Find out how big it is. labeledImage = bwlabel(binaryImage2); measurements = regionprops(labeledImage, 'Area'); allBlobAreas = [measurements.Area] % Hmmm ... Looks like squares are bigger than 700 pixels. % Extract those out. allowableAreaIndexes = allBlobAreas > 700; % Take the big objects. keeperIndexes = find(allowableAreaIndexes); % Extract only those blobs that meet our criteria, and % eliminate those blobs that don't meet our criteria. % Note how we use ismember() to do this. keeperBlobsImage = ismember(labeledImage, keeperIndexes);
% Display the image. %subplot(2, 2, 4); figure,imshow(keeperBlobsImage, []); title('Final Binary Image', 'FontSize', fontSize); % Re-measure this final binary image to get the % intensity of the squares. % labeledImage = bwlabel(keeperBlobsImage); measurements = regionprops(keeperBlobsImage, ... redChannel, 'MeanIntensity'); allBlobIntensities = [measurements.MeanIntensity] % Harris detector % The code calculates % the Harris Feature Points(FP) % % When u execute the code, the test image file opened % and u have to select by the mouse the region where u % want to find the Harris points, % then the code will print out and display the feature % points in the selected region. % You can select the number of FPs by changing the variables % max_N & min_N % A. Ganoun for i=1:N frame = keeperBlobsImage; end %I=rgb2gray(frame); %figure,imshow(I); %save Imag frame; %load Imag %pointtest=I(200,100)
I =double(frame); %**************************** %imshow(frame); %k = waitforbuttonpress; %point1 = get(gca,'CurrentPoint'); %button down detected point1 =[50,20,1;50,20,0]; rectregion = rbbox; %%%return figure units %point2 = get(gca,'CurrentPoint');%%%%button up detected point2 = [600,450,1;600,450,0]; point1 = point1(1,1:2); %%% extract col/row min and maxs point2 = point2(1,1:2); lowerleft = min(point1, point2); upperright = max(point1, point2); ymin = round(lowerleft(1)); %%% arrondissement aux nombrs les plus proches ymax = round(upperright(1)); xmin = round(lowerleft(2)); xmax = round(upperright(2)); %*********************************** Aj=6; cmin=xmin-Aj; cmax=xmax+Aj; rmin=ymin-Aj; rmax=ymax+Aj; min_N=4;max_N=4; %%%%%%%%%%%%%%Intrest Points %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% sigma=2; Thrshold=20; r=6; disp=1; dx = [-1 0 1; -1 0 1; -1 0 1]; % The Mask dy = dx'; %%%%%% Ix = conv2(I(cmin:cmax,rmin:rmax), dx,'same'); Iy = conv2(I(cmin:cmax,rmin:rmax), dy,'same'); g = fspecial('gaussian',max(1,fix(6*sigma)), sigma); %%%%%% Gaussien Filter
%%%%%
Ix2 = conv2(Ix.^2, g, 'same');
Iy2 = conv2(Iy.^2, g, 'same');
Ixy = conv2(Ix.*Iy, g,'same');
%%%%%%%%%%%%%%
k = 0.04;
R11 = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
R11=(1000/max(max(R11)))*R11;
R=R11;
ma=max(max(R));
sze = 2*r+1;
MX = ordfilt2(R,sze^2,ones(sze));
R11 = (R==MX)&(R>Thrshold);
count=sum(sum(R11(5:size(R11,1)-5,5:size(R11,2)-5)));
loop=0;
while (((count<min_N)|(count>max_N))&(loop<30))
if count>max_N
Thrshold=Thrshold*1.5;
elseif count < min_N
Thrshold=Thrshold*0.5;
end
R11 = (R==MX)&(R>Thrshold);
count=sum(sum(R11(5:size(R11,1)-5,5:size(R11,2)-5)));
loop=loop+1;
end
R=R*0; R(5:size(R11,1)-5,5:size(R11,2)-5)=R11(5:size(R11,1)-5,5:size(R11,2)-5); [r1,c1] = find®; PIP=[r1+cmin,c1+rmin]%% IP
%%%%%%%%%%%%%%%%%%%%Display
Size_PI=size(PIP,1);
for r=1: Size_PI
I(PIP(r,1)-2:PIP(r,1)+2,PIP(r,2)-2)=255;
I(PIP(r,1)-2:PIP(r,1)+2,PIP(r,2)+2)=255;
I(PIP(r,1)-2,PIP(r,2)-2:PIP(r,2)+2)=255;
I(PIP(r,1)+2,PIP(r,2)-2:PIP(r,2)+2)=255;
end
imshow(uint8(I))
end
I need to detect exactly 8 corners of the two squares. But Harris is not selecting all the 8 corners from the images. Some times it is selecting 8 sometimes is selecting some other number of corners. Like from This (<http://tinypic.com/r/157nl8x/5)>, it is selecting corners like this (<http://tinypic.com/r/n1s8ck/5)>. And from the next image (<http://tinypic.com/r/izccpx/5>) it is detecting corners like this (<http://tinypic.com/r/25rl3pg/5)>.
If you save the 1st and 3rd image and change the file location of the 12th line of the above program and run the program, then you might have the idea about how it is working.
I have to detect exactly 8 corners and I need one program that without finding 8 corners from the images it will run the harris again and again and give the perfect result. I mean I want to limit the corners number at 8 and 8 of them are from the squares.
If you need some other images then please inform me.
I will be very glad to get help from you.

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 24 Jan 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!