Showing posts with label image comparison. Show all posts
Image comparison using MATLAB (edge detection)
Image comparison using MATLAB
(edge detection)
Here is an simple algorithm for image comparison based on edge detection algorithm.
oq=imread('1.jpg'));
odprz=imresize(oq,[300 300]);
imwrite(odprz,'oq1.jpg');
sw=imread('2.jpg'));
stprz=imresize(sw,[300 300]);
imwrite(stprz,'sw1.png');
pic1 = imread('oq1.jpg');
pic2 = imread('sw1.png');
[x,y,z] = size(pic1);
if(z==1)
;
else
pic1 = rgb2gray(pic1);
end
[x,y,z] = size(pic2);
if(z==1)
;
else
pic2 = rgb2gray(pic2);
end
%applying edge detection on first picture
%so that we obtain white and black points and edges of the objects present
%in the picture.
edge_det_pic1 = edge(pic1,'canny');
%%applying edge detection on second picture
%so that we obtain white and black points and edges of the objects present
%in the picture.
edge_det_pic2 = edge(pic2,'canny');
%definition of different variables to be used in the code below
%initialization of different variables used
matched_data = 0;
matched_data1 = 0;
white_points = 0;
black_points = 0;
x=0;
y=0;
l=0;
m=0;
%for loop used for detecting black and white points in the picture.
for a = 1:1:256
for b = 1:1:256
if(edge_det_pic1(a,b)==1)
white_points = white_points+1;
else
black_points = black_points+1;
end
end
end
%for loop comparing the white (edge points) in the two pictures
for i = 1:1:256
for j = 1:1:256
if(edge_det_pic1(i,j)==1)&(edge_det_pic2(i,j)==1)
matched_data = matched_data+1;
else
matched_data1 = matched_data1+1;
end
end
end
%calculating percentage matching.
total_data = white_points;
total_matched_percentage = (matched_data/total_data)*100;
om=min(odprz);
sm=min(stprz);
if om==255
total_matched_percentage=0;
elseif sm==255
total_matched_percentage=0;
end
disp(total_matched_percentage)
disp('process completed')
Automated image comparison using MATLAB
Automated image
comparison using MATLAB
MAIN MOTIVE
This project presents the new machine learning process on images. In competitive intelligence information comparison the text analytic decides the product match type to enhance that automation process image comparison of the products is automated and presented here. Here the corner detection and comparison on images is used to compare the images.
USE
CASES
Mainly developed
for competitive intelligence (i.e.) to compare online marketing client and
competitor images.
Considerations
To compare images in large amount if we download images and compare the
space needed will be more to overcome that scenario the image URLs are used
from both client and competitor side. Images can be of any format in the case
of comparison of images from URLs.
If input is image URLs mean that should be in excel file and output
status of images equal or not equal will be stored in the specified column of
excel sheet.
If images are stored in drives mean other process like renaming of images
will be followed to get the images into the code execution. (Pre-processing is needed)
PROCESS
FLOW
Example images processed
COMPARISON DURING EXECUTION
OUTPUT
output will be stored in excel sheet.
status 1: equal
status 2: not equal
*********************************************************************************
q=2; %% source image name ('2.jpg')
w=2; %% another image ('2.png')
sd=1;
for i=1:1
hIdtc = video.ImageDataTypeConverter();
hCsc = video.ColorSpaceConverter('Conversion','RGB to intensity');
oq=imread(strcat(num2str(q),'.jpg'));
odprz=imresize(oq,[150 150]);
imwrite(odprz,'oq1.jpg');
leftI3chan = step(hIdtc,imread('oq1.jpg'));
leftI = step(hCsc,leftI3chan);
disp(leftI)
sw=imread(strcat(num2str(w),'.png'));
stprz=imresize(sw,[150 150]);
imwrite(stprz,'sw1.png');
rightI3chan = step(hIdtc,imread('sw1.png'));
rightI = step(hCsc,rightI3chan);
harris = video.CornerDetector( 'MaximumCornerCount', 1000, ...
'CornerThreshold', 1e-4, ...
'NeighborhoodSize', [9 9] );
pointsLeft = flipud(step(harris, leftI))+1;
pointsRight = flipud(step(harris, rightI))+1;
% Trim point lists to minimum size.
pointsLeft = pointsLeft(:, 1:find(all(pointsLeft==1,1),1) - 1);
pointsRight = pointsRight(:, 1:find(all(pointsRight==1,1),1) - 1);
halfBlockSize = 4; % Block half-size.
blockSize = 2*halfBlockSize+1; % Full block size.
[r,c] = size(leftI);
matchThreshold = 0.7;
% Extract features for pointsRight
features = zeros(blockSize^2,size(pointsRight,2), 'single');
for i=1:size(pointsRight,2)
T = zeros(blockSize, 'single');
minr = max(1,pointsRight(2,i)-halfBlockSize);
maxr = min(r,pointsRight(2,i)+halfBlockSize);
minc = max(1,pointsRight(1,i)-halfBlockSize);
maxc = min(c,pointsRight(1,i)+halfBlockSize);
T( halfBlockSize+1-(pointsRight(2,i)-minr):halfBlockSize+1+(maxr-pointsRight(2,i)), ...
halfBlockSize+1-(pointsRight(1,i)-minc):halfBlockSize+1+(maxc-pointsRight(1,i)) ) = ...
rightI( minr:maxr, minc:maxc );
features(:,i) = T(:);
end
% Loop over pointsLeft, looking for best matches in pointsRight via features.
ix1 = zeros(1,size(pointsLeft,2), 'single');
d = zeros(size(ix1), 'single');
for i=1:size(pointsLeft,2)
T = zeros(blockSize, 'single');
minr = max(1,pointsLeft(2,i)-halfBlockSize);
maxr = min(r,pointsLeft(2,i)+halfBlockSize);
minc = max(1,pointsLeft(1,i)-halfBlockSize);
maxc = min(c,pointsLeft(1,i)+halfBlockSize);
T( halfBlockSize+1-(pointsLeft(2,i)-minr):halfBlockSize+1+(maxr-pointsLeft(2,i)), ...
halfBlockSize+1-(pointsLeft(1,i)-minc):halfBlockSize+1+(maxc-pointsLeft(1,i)) ) = ...
leftI( minr:maxr, minc:maxc );
% Find matching point in pointsRight with features features.
[v,ix] = min( sum(bsxfun(@minus,features,T(:)).^2,1) );
if v < matchThreshold
ix1(i) = ix;
end
end
% Extract indices of matched points on each side.
ix2 = nonzeros(ix1);
ix1 = find(ix1);
% Create subselected, homogenized point lists.
pointsLeftH = [double(pointsLeft(:,ix1)); ones(1,length(ix1), 'single')];
pointsRightH = [double(pointsRight(:,ix2)); ones(1,length(ix2), 'single')];
figure(1), clf;
imshow(cat(3,rightI,leftI,leftI)), hold on;
plot(pointsLeftH(1,:),pointsLeftH(2,:),'x','Color',[0 0 1],'MarkerSize',8);
plot(pointsRightH(1,:),pointsRightH(2,:),'x','Color',[1 1 0],'MarkerSize',8);
plot([pointsLeftH(1,:);pointsRightH(1,:)],...
[pointsLeftH(2,:);pointsRightH(2,:)],'-','Color',[0 1 0]);
set(gca,'XTick',[],'YTick',[]);
title('Initial corresponded points between the two images');
d1=mean(pointsRightH);
d2=mean(pointsRightH);
t1=mode(d2);
t2=mode(d1);
if t1 == t2 %% modify here put one extra loop if t1<5 means result will be not a match(for better comparison)
result={'exact'};
disp(result)
else
result={'not found'};
disp(result)
end
xlswrite('myfile.xls',result,'sheet1',strcat('A',num2str(sd))) %% excel file to store result
q=q+1;
w=w+1;
sd=sd+1;
end
*********************************************************************************
q=2; %% source image name ('2.jpg')
w=2; %% another image ('2.png')
sd=1;
for i=1:1
hIdtc = video.ImageDataTypeConverter();
hCsc = video.ColorSpaceConverter('Conversion','RGB to intensity');
oq=imread(strcat(num2str(q),'.jpg'));
odprz=imresize(oq,[150 150]);
imwrite(odprz,'oq1.jpg');
leftI3chan = step(hIdtc,imread('oq1.jpg'));
leftI = step(hCsc,leftI3chan);
disp(leftI)
sw=imread(strcat(num2str(w),'.png'));
stprz=imresize(sw,[150 150]);
imwrite(stprz,'sw1.png');
rightI3chan = step(hIdtc,imread('sw1.png'));
rightI = step(hCsc,rightI3chan);
harris = video.CornerDetector( 'MaximumCornerCount', 1000, ...
'CornerThreshold', 1e-4, ...
'NeighborhoodSize', [9 9] );
pointsLeft = flipud(step(harris, leftI))+1;
pointsRight = flipud(step(harris, rightI))+1;
% Trim point lists to minimum size.
pointsLeft = pointsLeft(:, 1:find(all(pointsLeft==1,1),1) - 1);
pointsRight = pointsRight(:, 1:find(all(pointsRight==1,1),1) - 1);
halfBlockSize = 4; % Block half-size.
blockSize = 2*halfBlockSize+1; % Full block size.
[r,c] = size(leftI);
matchThreshold = 0.7;
% Extract features for pointsRight
features = zeros(blockSize^2,size(pointsRight,2), 'single');
for i=1:size(pointsRight,2)
T = zeros(blockSize, 'single');
minr = max(1,pointsRight(2,i)-halfBlockSize);
maxr = min(r,pointsRight(2,i)+halfBlockSize);
minc = max(1,pointsRight(1,i)-halfBlockSize);
maxc = min(c,pointsRight(1,i)+halfBlockSize);
T( halfBlockSize+1-(pointsRight(2,i)-minr):halfBlockSize+1+(maxr-pointsRight(2,i)), ...
halfBlockSize+1-(pointsRight(1,i)-minc):halfBlockSize+1+(maxc-pointsRight(1,i)) ) = ...
rightI( minr:maxr, minc:maxc );
features(:,i) = T(:);
end
% Loop over pointsLeft, looking for best matches in pointsRight via features.
ix1 = zeros(1,size(pointsLeft,2), 'single');
d = zeros(size(ix1), 'single');
for i=1:size(pointsLeft,2)
T = zeros(blockSize, 'single');
minr = max(1,pointsLeft(2,i)-halfBlockSize);
maxr = min(r,pointsLeft(2,i)+halfBlockSize);
minc = max(1,pointsLeft(1,i)-halfBlockSize);
maxc = min(c,pointsLeft(1,i)+halfBlockSize);
T( halfBlockSize+1-(pointsLeft(2,i)-minr):halfBlockSize+1+(maxr-pointsLeft(2,i)), ...
halfBlockSize+1-(pointsLeft(1,i)-minc):halfBlockSize+1+(maxc-pointsLeft(1,i)) ) = ...
leftI( minr:maxr, minc:maxc );
% Find matching point in pointsRight with features features.
[v,ix] = min( sum(bsxfun(@minus,features,T(:)).^2,1) );
if v < matchThreshold
ix1(i) = ix;
end
end
% Extract indices of matched points on each side.
ix2 = nonzeros(ix1);
ix1 = find(ix1);
% Create subselected, homogenized point lists.
pointsLeftH = [double(pointsLeft(:,ix1)); ones(1,length(ix1), 'single')];
pointsRightH = [double(pointsRight(:,ix2)); ones(1,length(ix2), 'single')];
figure(1), clf;
imshow(cat(3,rightI,leftI,leftI)), hold on;
plot(pointsLeftH(1,:),pointsLeftH(2,:),'x','Color',[0 0 1],'MarkerSize',8);
plot(pointsRightH(1,:),pointsRightH(2,:),'x','Color',[1 1 0],'MarkerSize',8);
plot([pointsLeftH(1,:);pointsRightH(1,:)],...
[pointsLeftH(2,:);pointsRightH(2,:)],'-','Color',[0 1 0]);
set(gca,'XTick',[],'YTick',[]);
title('Initial corresponded points between the two images');
d1=mean(pointsRightH);
d2=mean(pointsRightH);
t1=mode(d2);
t2=mode(d1);
if t1 == t2 %% modify here put one extra loop if t1<5 means result will be not a match(for better comparison)
result={'exact'};
disp(result)
else
result={'not found'};
disp(result)
end
xlswrite('myfile.xls',result,'sheet1',strcat('A',num2str(sd))) %% excel file to store result
q=q+1;
w=w+1;
sd=sd+1;
end
********************************** THANK YOU***********************************
Your valid suggestions are anticipated to enhance the program.
LINKED IN : in.linkedin.com/in/srimukunthan
GMAIL : srimukunthan@gmail.com
FACEBOOK: www.facebook.com/mukunthan.gj




