Read .txt file and make comparison
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Thorsten
2014년 11월 21일
You use C syntax, not Matlab. I showed you how to compare two matrices in response to your previous question.
답변 (1개)
prabhat kumar sharma
2025년 1월 10일
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!