Creating rgb matrix and finally creating a RGB image

58 次查看(过去 30 天)
I have written a program and that finally outputs a 2d matrix of rows= Size of the picture and column=3(The RGB values) and each row depicts the RGB values stored of each pixel.
Finally at the end of the program evaluation i obtain a 2d matrix. Now I have rgb values and I would like to convert that into an RGB image. "out" is the final 2d matrix with row=Size of the picture and column=3(The RGB values) My code is as follows:- numberOfImages=3;
k=0; out= zeros(4,3); for row=390:391 for col=50:51
for i=1:numberOfImages
filename=[num2str(i), '.tif'];
rgbImage=imread(filename, 'tif');
R = rgbImage(row, col, 1);
G = rgbImage(row, col, 2);
B = rgbImage(row, col, 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(row, col, 1);
G1 = rgbImage1(row, col, 2);
B1 = rgbImage1(row, col, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =[R1,G1,B1];
else
indexOfPictureNeeded=indexForJitter(1,:);
filenameOfImage1=[num2str(indexOfPictureNeeded), '.tif'];
rgbImage1=imread(filename, 'tif');
R1 = rgbImage1(row, col, 1);
G1 = rgbImage1(row, col, 2);
B1 = rgbImage1(row, col, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =[R1,G1,B1];
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(row, col, 1);
G1 = rgbImage2(row, col, 2);
B1 = rgbImage2(row, col, 3);
RGBMatrix2(indexOfPictureNeeded1,:) =[R1,G1,B1];
k=k+1;
out(k,:) =[R1,G1,B1];
end
end
end
  1 个评论
Manan Mishra
Manan Mishra 2018-4-6
You cannot convert a 2D matrix into an RGB image.
An RGB image is stored as an m-by-n-by-3 data array that defines red, green, and blue color components for each individual pixel.
Here m-by-n represents the size of the image and the third dimension gives the values of the pixels for each of R,G and B components.
So, you need a 3 dimensional matrix to represent an RGB image.
You can use the command image to display the RGB image.
Please refer the following link for more information on this:

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by