Read .txt file and make comparison

4 views (last 30 days)
Akmal Rahmat
Akmal Rahmat on 21 Nov 2014
Below is my coding. i save the image into .txt file then compare it with another .txt file.
a = imread('img2.jpg');
b = imresize(a,[100,100])
%%c = rgb2ind(b);
dlmwrite('test4.txt', b, 'delimiter', ',');
d = dlmread('test4.txt');
[x,y] = size(d);
e = imread('img1.jpg');
f = imresize(e,[100,100])
%%c = rgb2ind(b);
dlmwrite('test5.txt', f, 'delimiter', ',');
g = dlmread('test5.txt');
[x,y] = size(g);
vissdiff('test4.txt','test5.txt');
but this coding show the result by line only. For example, line1 image1 has 1,2,3,4,5 and line1 image2 has 1,2,3,5,5.. So the different there is number 4 and 5. The coding above check if there one mismatch data from each line then it consider the line is wrong/mismatch. So i want to create a code which check the each one of the data and do not consider the line is wrong if it have only one data that mismatch. so below is the code that i think will check each data and give me the answer that i want because my image greyscale(0-255).
for (int d=0;d<100;d++)
{ for(int g=0;g<100;g++)
{ pixel1 = img1[d][g];
pixel2 = img2[d][g];
fpixel = pixel1-pixel2;
if fpixel<=125
{ fpixel = 1;
return 1; }
else fpixel>125
{ fpixel = 0;
return 0; }
}
}
but i get error for above coding said unexpected MATLAB expression. why is that ? and what is the proper way to read the .txt file so i can apply it on coding above ? i need some guidance. please
  2 Comments
Thorsten
Thorsten on 21 Nov 2014
You use C syntax, not Matlab. I showed you how to compare two matrices in response to your previous question.
Akmal Rahmat
Akmal Rahmat on 21 Nov 2014
thank you again but greyscale image are from 0- 255 right ? how can i categorize it to 0-125 are true then return as 1 and 126-255 are false then return as 0. so later i can calculate it to get the percentage of the similarity.

Sign in to comment.

Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 10 Jan 2025
Hello Akmal,
The error you're encountering is due to a few syntax issues and misunderstandings about MATLAB's syntax and functionality. Let's address these and guide you through the process of comparing two images stored in text files.
% Read images from text files
img1 = dlmread('test4.txt');
img2 = dlmread('test5.txt');
% Initialize a difference counter
difference_count = 0;
% Loop through each pixel and compare
for d = 1:100
for g = 1:100
pixel1 = img1(d, g);
pixel2 = img2(d, g);
fpixel = abs(pixel1 - pixel2); % Use absolute difference
% Check if the difference is significant
if fpixel <= 125
% Consider pixels similar
fprintf('Pixel (%d, %d) is similar.\n', d, g);
else
% Consider pixels different
fprintf('Pixel (%d, %d) is different.\n', d, g);
difference_count = difference_count + 1;
end
end
end
% Output the total number of different pixels
fprintf('Total different pixels: %d\n', difference_count);
I hope it helps!

Categories

Find more on Language Support in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!