How to convert .mat to any image format(.png,.jpeg,..jpg, .jpeg, .jfif, .pjpeg, .pjp,.webp, .bmp,.tif, .tiff SVG) using matlab - matlab

I have a dataset whose images extension are in .mat. I found a solution in Matlab to solve this issue

Here is an example MATLAB code to convert a .mat file to an image format:
% Load the .mat file
load('example.mat');
% Convert the data to uint8
I = reshape(uint16(linspace(0,65535,25)),[5 5])
example_matrix = im2uint8(I);
% Try to save the image
try
imwrite(example_matrix, 'example.png');
disp('Image saved successfully');
catch
disp('Error saving image');
end
Note that you should replace "example.mat" and "example_matrix" with the actual names of your .mat file and matrix data, respectively. You can also change the format of the output image by changing the file extension in the imwrite function (e.g., 'example.jpg' or 'example.bmp').

Related

Open a text file, scan it and plot it in MATLAB

I am trying to open a text file in MATLAB and plot it in a graph. The following is my code:
%% Get the data
[filename, pathname] = uigetfile('*txt', 'Pick text file');
x=filename(:,1);
y=filename(:,2);
plot(x,y);
But each time I run it, I get following error:
Error using plot
Invalid first data argument.
Error in readtxtfile (line 5)
plot(x,y);
The text file that I imported has 2 rows. I am planning to plot the first row with the second say plot (row 1, row 2) in MATLAB.
You have the name of the file stored in filename combined with the path to the directory where the file is stored in pathname but you actually haven't read any of the contents. To do that, the easiest thing would be to use dlmread. I'm assuming your text file is properly formatted to have two rows of data as you stated. If this is the case, you need to change the way you're indexing into your data. You have it indexing entire columns instead of rows so you need to flip the indexing in your code. Also, you need a call to dlmread, then access the columns of the resulting matrix:
%% Get the data
[filename, pathname] = uigetfile('*txt', 'Pick text file');
data = dlmread(fullfile(pathname, filename));
x=data(1,:);
y=data(2,:);
plot(x,y);
Notice that I made the full path to your file to use fullfile because using uigetfile allows you to read in a file from anywhere on your computer so we make sure that we capture the full path to your file. Again to reiterate, pathname is the directory of where the file is contained and filename is the name of the file contained in the directory.

how to convert .mat file into bitmap in Matlab

I am trying to convert a .mat file into a bitmap image in Matlab but I cannot seem to find a way to do it. This is my current code:
human_seg = load(human_img);
where the human_img is a .mat file. I need to then convert the human_seg into a bmp but when I try it I recieve the error
Conversion to logical from struct is not possible.
Try this instead
imwrite(human_seg,'Imagefile1.bmp')

How to export images in *.dat format in MATLAB

I want to use the MATLAB findcluster tool (Fuzzy Logic Toolbox), but it requires the data to be loaded to be in *.dat format.
Is it possible to export my grayscale images(2D matrices actually) in *.dat through MATLAB? And if yes, could I also export 3D images the same way?
Thanks in advance for any suggestions,
Ziggy.
The key is to think of the image as just like any other array. Then saving/loading to a .dat file is easy. Here's some sample code:
% Open a sample image
testImage = imread('tire.tif');
figure; imshow(testImage)
% Convert to double so the ASCII conversion will work properly
testImage = double(testImage);
% Save the .dat file
save test.dat testImage -ascii
% Load the .dat file we just saved
testImageReopened = load('test.dat');
% Convert back to the original format
testImageReopened = uint8(testImageReopened);
figure; imshow(testImageReopened);
I believe it should work similarly for a 3D image, but you might want to try it out to be sure.

How do I extract the TIFF preview from an EPS file using MATLAB?

EPS files can include embedded TIFF (and rarely WMF) previews for easy rendering in environments which do not have PostScript available. (See Wikipedia for more info.)
Given such an EPS, how can I extract the TIFF into a separate file using MATLAB?
% Define the source EPS file and the desired target TIFF to create.
source = 'ode_nonneg1.eps';
target = 'ode_nonneg1.tif';
% Read in the EPS file.
f = fopen(source,'rb');
d = fread(f,'uint8');
fclose(f);
% Check the header to verify it is a TIFF.
if ~isequal(d(1:4),[197;208;211;198])
error 'This does not appear to be a vaild TIFF file.'
end
% Extract the TIFF data location from the headers.
tiffStart = sum(d(21:24).*256.^(0:3)')+1;
tiffLength = sum(d(25:28).*256.^(0:3)');
% Write the TIFF file.
f = fopen(target,'w');
fwrite(f,d(tiffStart:tiffStart-1+tiffLength),'uint8','b');
fclose(f);

Saving image using MATLAB

I made a program in MATLAB to generate images with different specifications, but each time I change one of theses specifications I had to re-save the image under a different name and path. So, I made a for loop to change these specifications, but I don't know how I can make MATLAB save the generated image with different names and different paths...
How can I write a program to make MATLAB save multiple generated images with different names and different paths as a part of for–loop?
Put something like this at the end of your loop:
for i = 1:n
<your loop code>
file_name=sprintf('%d.jpg',i); % assuming you are saving image as a .jpg
imwrite(your_image, file_name); % or something like this, however you choose to save your image
end
If you want to save JPEG, PNG etc., then see #AGS's post. If you want to save FIG files, use
hgsave(gcf, file_name)
instead of the imwrite line. There's also
print('-djpeg', file_name) %# for JPEG file (lossy)
print('-dpng', file_name) %# for PNG file (lossless)
as an alternative for imwrite.
Since I wanted to save the plots in my loop in a specific folder in my present working directory (pwd), I modified my naming routine as follows:
for s = 1:10
for i = 1:10
<loop commands>
end
end
% prints stimuli to folder created for them
file_name=sprintf('%s/eb_imgs/%0.3f.tif',pwd,s(i)); % pwd = path of present working
% directory and s(i) = the value
% of the changing variable
% that I wanted to document
file_name = /Users/Miriam/Documents/PSYC/eb_imgs/0.700.tif % how my filename appears
print('-dtiff', '-r300', file_name); % this saves my file to my desired location
matlab matlab-path