MATLAB: How to convert vectors to their image

imagesvectors

I have a vector of 1×10000 (contains only 0s and 1s) which I need to convert into a 100×100 and then 1200×900 image. The following is the code I use.
% the vector is present in an excel file.
filename = 'D:\Winter 2018\project\Dataset\CSV files\csv_files\Object1_set9\Prediction.xlsx';
sheet = 1;
xlRange = 'A2:A10001';
data = xlsread(filename,sheet,xlRange);
image = double(reshape(data,100,100)); %devectorizing the 1x10000 vector
Im = imresize(image,[1200,900]); % resizing it to a 1200x900 image
dir = strcat('D:\Winter 2018\project\Dataset\Results_70\GTP_GBT\');
saveas(gcf,strcat(dir,('Image')),'jpg'); %saving the current image in jpg format
figure,imshow(image); % preview of the current image
I face two problems with this code. First, I am unable to get the image in 1200×900 dimension. It is saved with some random dimensions. Second, my image has a lot of white space around it when it is saved on the computer. But, that white space is not seen in the preview. This means that the image is not saved as it is shown in the preview. Please let me know what is my error in this code.

Best Answer

You do not preview anything. You do not draw anything. gcf will refer to whatever happens to be on the figure, or will create an empty figure if nothing is open.
You should
  1. use a different variable name rather than "image", because image() is a key MATLAB routine to display images; at the very least you are confusing readers
  2. Do not use saveas(): use imwrite()
imwrite(RenamedImageVariable, fullfile(dir, 'Image.jpg'));