- 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
- Do not use saveas(): use imwrite()
How to convert vectors to their image?
4 views (last 30 days)
Show older comments
Yamini Nibhanupudi
on 30 Jun 2018
Commented: Yamini Nibhanupudi
on 30 Jun 2018
I have a vector of 1x10000 (contains only 0s and 1s) which I need to convert into a 100x100 and then 1200x900 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 1200x900 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.
0 Comments
Accepted Answer
Walter Roberson
on 30 Jun 2018
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
imwrite(RenamedImageVariable, fullfile(dir, 'Image.jpg'));
More Answers (0)
See Also
Categories
Find more on Convert Image Type 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!