Skip to content

Instantly share code, notes, and snippets.

@berkayakcay
Last active December 3, 2016 19:29
Show Gist options
  • Save berkayakcay/4bad0db8b25a6e02a5dc5f48006a897f to your computer and use it in GitHub Desktop.
Save berkayakcay/4bad0db8b25a6e02a5dc5f48006a897f to your computer and use it in GitHub Desktop.
share test
clc; clear; close all;
%ream images
firstImg = imread('c.png');
firstImg = double(firstImg);
secondImg = imread('c2.png');
secondImg = double(secondImg);
%define distance from pixel
distance2point = 5;
%first image harris
[firstImgRowNumber firstImgColNumber] = size(firstImg);
[firstCornersofImg, firstImgRows, firstImgCols] = harris(im2double(firstImg),1,0.04,15,1);
firstImgCornerCount = numel(firstImgRows);
% %mean filter
% secondImg = meanFilter(secondImg);
% secondImg = double(secondImg);
%second image harris
[secondImgRowNumber secondImgColNumber] = size(secondImg);
[secondCornersofImg, secondImgRows, secondImgCols] = harris(im2double(secondImg),1,0.04,15,1);
secondImgCornerCount = numel(secondImgRows);
%partial initialize
firstPartial = zeros((distance2point*2+1));
firstPartial = double(firstPartial);
secondPartial = zeros((distance2point*2+1));
secondPartial = double(secondPartial);
%Image mean initialize
firstImgMeans = zeros(firstImgCornerCount,1);
firstImgMeans = double(firstImgMeans);
secondImgMeans = zeros(secondImgCornerCount,1);
secondImgMeans = double(secondImgMeans);
%{
calculate means in first image corner list
%}
for i=1:firstImgCornerCount
% get row and col values
row = firstImgRows(i);
col = firstImgCols(i);
% select partial image from main image (first image)
if(((row-distance2point)>0) && ((row + distance2point) < firstImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < firstImgColNumber))
firstPartial = firstImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
% calculate mean of partial image then, add this value to first mean array
partialmean = double(0);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
partialmean = double(partialmean) + double(firstPartial(k,l));
end
end
partialmean = double(partialmean / (distance2point*2 + 1)^2);
firstImgMeans(i,1) = partialmean;
end
%{
calculate means in second image corner list
%}
for i=1:secondImgCornerCount
% get row and col values
row = secondImgRows(i);
col = secondImgCols(i);
% select partial image from main image (second image)
if(((row-distance2point)>0) && ((row + distance2point) < secondImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < secondImgColNumber))
secondPartial = secondImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
partialmean = double(0);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
partialmean = double(partialmean) + double(secondPartial(k,l));
end
end
partialmean = double(partialmean / (distance2point*2 + 1)^2);
secondImgMeans(i,1) = partialmean;
end
%{
normalized process in first image
%}
for i=1:firstImgCornerCount
% get row and col values
row = firstImgRows(i);
col = firstImgCols(i);
% select partial image from main image (first image)
if(((row-distance2point)>0) && ((row + distance2point) < firstImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < firstImgColNumber))
firstPartial = firstImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
% normalize partial image
totalvalueminusmeansquare = double(0);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
totalvalueminusmeansquare = totalvalueminusmeansquare + ((double(firstPartial(k,l)) - firstImgMeans(i))^2);
end
end
totalvalueminusmeansquare = sqrt(totalvalueminusmeansquare);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
firstPartial(k,l) = (double(firstPartial(k,l)) - firstImgMeans(i))/totalvalueminusmeansquare;
end
end
firstImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point) = firstPartial;
end
%{
normalized process in second image
%}
for i=1:secondImgCornerCount
% get row and col values
row = secondImgRows(i);
col = secondImgCols(i);
% select partial image from main image (second image)
if(((row-distance2point)>0) && ((row + distance2point) < secondImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < secondImgColNumber))
secondPartial = secondImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
% normalize partial image
totalvalueminusmeansquare = double(0);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
totalvalueminusmeansquare = totalvalueminusmeansquare + ((double(secondPartial(k,l)) - secondImgMeans(i))^2);
end
end
totalvalueminusmeansquare = sqrt(totalvalueminusmeansquare);
for k=1:(distance2point*2+1)
for l=1:(distance2point*2+1)
secondPartial(k,l) = (double(secondPartial(k,l)) - secondImgMeans(i))/totalvalueminusmeansquare;
end
end
secondImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point) = secondPartial;
end
I = zeros([size(firstImg,1) size(firstImg,2)*2 size(firstImg,3)]);
I(:,1:size(firstImg,2),:)=firstImg; I(:,size(firstImg,2)+1:size(firstImg,2)+size(secondImg,2),:)=secondImg;
figure, imshow(I/255); hold on;
%{
cross correlation process in first image
%}
for i=1:firstImgCornerCount
% get row and col values
row = firstImgRows(i);
col = firstImgCols(i);
minimum = 255;
index = 0;
if(((row-distance2point)>0) && ((row + distance2point) < firstImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < firstImgColNumber))
firstPartial = firstImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
for j=1:secondImgCornerCount
% get row and col values
row = secondImgRows(j);
col = secondImgCols(j);
if(((row-distance2point)>0) && ((row + distance2point) < secondImgRowNumber) && ((col-distance2point)>0) && ((col + distance2point) < secondImgColNumber))
secondPartial = secondImg(row - distance2point: row + distance2point, col - distance2point:col + distance2point);
else
continue;
end
sumofsquareddifferences = 0;
for m=1:(distance2point*2+1)
for n=1:(distance2point*2+1)
sumofsquareddifferences = sumofsquareddifferences + (firstPartial(m,n) - secondPartial(m,n))^2;
end
end
if(sumofsquareddifferences < minimum)
minimum = sumofsquareddifferences;
index = j;
end
end
c=rand(1,3);
plot([firstImgCols(i) secondImgCols(index)+size(firstImg,2)],[firstImgRows(i) secondImgRows(index)],'-','Color',c)
plot([firstImgCols(i) secondImgCols(index)+size(firstImg,2)],[firstImgRows(i) secondImgRows(index)],'o','Color',c)
fprintf('Matched coordinate (%d,%d) to (%d,%d) \n',firstImgRows(i),firstImgCols(i),secondImgRows(index),secondImgCols(index));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment