Problems generating final image

5 views (last 30 days)
Pavel Chatterjee
Pavel Chatterjee on 6 Apr 2018
Edited: Pavel Chatterjee on 6 Apr 2018
I have a code that returns a single RGB value for every pixel. I run it over a loop through all pixels and I keep storing them in a matrix after every loop. In the end i have a matrix lets say out(412324,3) as the dimensions of the matrix. After reshape() and converting it to RGB matrix I try to get the final picture out of it.
My problems:- 1. The final picture is very different than expected. It is not a clear picture at all. 2. It is skewed. I have uploaded 2 pictures. One is expected and the other is obtained
Here is my code: numberOfImages=3; pic=imread('1.tif'); sizeOfY=size(pic,1); sizeofX=size(pic,2); totalSizeOfImage=sizeofX*299;
k=0; out= zeros(totalSizeOfImage,3);
for y=370:sizeOfY for x=1:sizeofX
for i=1:numberOfImages
filename=[num2str(i), '.tif'];
rgbImage=imread(filename, 'tif');
R = rgbImage(y, x, 1);
G = rgbImage(y, x, 2);
B = rgbImage(y, x, 3);
RGBMatrix(i,:) =[R,G,B];
end
for imageNumber=1:numberOfImages
match=0;
jitter=0;
for j=numberOfImages:-1:1
RedChannelDifference=abs(double(RGBMatrix(j,1))-double(RGBMatrix(imageNumber,1)));
GreenChannelDifference=abs(double(RGBMatrix(j,2))-double(RGBMatrix(imageNumber,2)));
BlueChannelDifference=abs(double(RGBMatrix(j,3))-double(RGBMatrix(imageNumber,3)));
if (RedChannelDifference+GreenChannelDifference+BlueChannelDifference)<=10
jitter=jitter+RedChannelDifference+GreenChannelDifference+BlueChannelDifference;
match=match+1;
end
end
match=match-1;
matchMatrixOfAllImages(imageNumber,:)=[match];
end
%fprintf('The maximum value of matches is : %d \n', max(matchMatrixOfAllImages(:)));
index=find(matchMatrixOfAllImages == max(matchMatrixOfAllImages(:)));
if size(index,1)>1
jitterMatrixOfAllImages(imageNumber,:)=[jitter];
% fprintf('Jitter values of ONLY MATCHES');
jitterMatrixOfMatchedImages=jitterMatrixOfAllImages(index,:);
indexForJitter=find(jitterMatrixOfMatchedImages == min(jitterMatrixOfMatchedImages(:)));
if size(indexForJitter,1)>1
indexOfPictureNeeded=indexForJitter(1,:);
filenameOfImage1=[num2str(indexOfPictureNeeded), '.tif'];
rgbImage1=imread(filename, 'tif');
R1 = rgbImage1(y, x, 1);
G1 = rgbImage1(y, x, 2);
B1 = rgbImage1(y, x, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix1 ;
else
indexOfPictureNeeded=indexForJitter(1,:);
filenameOfImage1=[num2str(indexOfPictureNeeded), '.tif'];
rgbImage1=imread(filename, 'tif');
R1 = rgbImage1(y, x, 1);
G1 = rgbImage1(y, x, 2);
B1 = rgbImage1(y, x, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix1 ;
end
else
jitterMatrixOfAllImages(imageNumber,:)=[jitter];
jitterMatrixOfMatchedImages=jitterMatrixOfAllImages(index,:);
indexForJitter=find(jitterMatrixOfMatchedImages == min(jitterMatrixOfMatchedImages(:)));
indexOfPictureNeeded1=indexForJitter(1,:);
filename1=[num2str(indexOfPictureNeeded1), '.tif'];
rgbImage2=imread(filename, 'tif');
R1 = rgbImage2(y, x, 1);
G1 = rgbImage2(y, x, 2);
B1 = rgbImage2(y, x, 3);
RGBMatrix2(indexOfPictureNeeded1,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix2 ;
end
end
end
for i=1:totalSizeOfImage R(i,1) = out (i,1); G(i,1) = out (i,2); B(i,1) = out (i,3); end
R = reshape(R, 299, sizeofX); G = reshape(G, 299, sizeofX); B = reshape(B, 299, sizeofX);
RGB(:,:,1)=R; RGB(:,:,2)=G; RGB(:,:,3)=B;

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!