How do I extract the TIFF preview from an EPS file using MATLAB? - 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);

Related

How to convert .mat to any image format(.png,.jpeg,..jpg, .jpeg, .jfif, .pjpeg, .pjp,.webp, .bmp,.tif, .tiff SVG) using 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').

Retrieve data from .rec binary file

The question may be naive, but answers could help me.
A measurement is recorded in binary format, with a header that contains all information about the data and the data itself (i.e. a series of doubles).
The measurement data can be exported in csv format from the application, but it takes ages.
What do you have to pay attention to when trying to read data from a binary file? Is this process even feasible using Matlab to import as an array or labview (export as .txt maybe?)
Binary .rec file format may refer to various things (audio/video encoding format of Topfield based on MPEG4-TS, proprietary audio encoding, and even MRI scanner from Phillips) ...
If it refers to MRI scanner you may find some direct reader on fileexchange: Matlab PAR REC Reader
If it refer to something else, you may parse binary file header and data by yourself using the low level routine: fread
Edit
Not knowing the exact file format for your recorded sensor displacement, here is dummy example with fread for reading large rec file block-by-block supposing header contains just the length of data, and that data is just a serie of double values:
function [] = DummyReadRec()
%[
% Open rec file for reading
[fid, errmsg] = fopen('dummy.rec', 'r');
if (fid < 0), error(errmsg); end
cuo = onCleanup(#()fclose(fid));
% Read header (here supposing it is only an integer giving length of data)
reclenght = fread(fid, 1, 'uint32');
% Read data block-by-block (here supposing it is only double values)
MAX_BLOCK_LENGTH = 512;
blockCount = ceil(reclenght / MAX_BLOCK_LENGTH);
for bi = 1:blockCount,
% Will read a maximum of 'MAX_BLOCK_LENGTH' (or less if we're on the last block)
[recdata, siz] = fread(fid, [1 MAX_BLOCK_LENGTH], 'double');
% Do something with this block (fft or whatever)
offset = (bi-1)*MAX_BLOCK_LENGTH;
position = (offset+1):(offset+siz);
plot(position, 20*log10(abs(fft(recdata))));
drawnow();
end
%]
end
The answer is going to depend on the format of your binary file and how large it is.
I have done many conversion of various binary files all with differing layouts. If the file will fit into memory then you can just use fread as long as you know the layout of the binary file. Below is an example of reading a header & simple data block. It would of course have to be modified depending on the layout of your file. Depending on recording equipment & computer type you may also need to make use of the machinefmt ('ieee-le' or 'ieee-be') options of fread ... that has burned me before.
%Open the File for reading
fid = fopen(yourRECfile,'r');
%Read the Header ... your layout will be different
header.MajorRel = fread(fid,1,'uint16'); %Major File Rev #
header.MinorRel = fread(fid,1,'uint16'); %Minor File Rev #
header.IRIGStart = fread(fid,1,'double'); %Start time in secs
header.Flags = fread(fid,1,'uint32'); %Flags
%Read everything else from there until end of file as a series of doubles.
data = fread(fid,inf,'double');
fclose(fid);
If the file does not fit into memory you will either need to process it in blocks or look into using memmapfile.

How can I convert matlab code to image?

For exmaple,I have the following code.
% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.
n = 50; % 50 data points
r = rand(n,1);
plot(r)
% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
for v = 1.0:-0.2:0.0
disp(v)
end
I want to convert MATLAB code to an image.
For example, if you copy the MATLAB code into a software then it return the image like this:
How to do it?
If I use this code and publish it to PDF file,the code is not completely display.
sumLumi(x,y)=LLmap3(floor(x/4)+1,floor(y/4)+1)+LLmap3(floor(x/4)+2,floor(y/4)+1)+LLmap3(floor(x/4)+1,floor(y/4)+2)+LLmap3(floor(x/4)+2,floor(y/4)+2);
From the MATLAB editor you can Publish your document as a PDF (by changing the Output file format to pdf under publishing options). It will also evaluate the code unless you change the Publishing Options>Code Settings>Evaluate code to false.
The PDF can then can converted to an image (a quick google search gives an online PDF to JPG converter).
You can break a line and continue on the next with ..., for example
sumLumi(x,y)=LLmap3(floor(x/4)+1,floor(y/4)+1)+LLmap3(floor(x/4)+2,floor(y/4)+1)+LLmap3(floor(x/4)+1,floor(y/4)+2)+LLmap3(floor(x/4)+2,floor(y/4)+2);
can be written as
sumLumi(x,y) = LLmap3(floor(x/4)+1, floor(y/4)+1) ...
+ LLmap3(floor(x/4)+2, floor(y/4)+1) ...
+ LLmap3(floor(x/4)+1, floor(y/4)+2) ...
+ LLmap3(floor(x/4)+2, floor(y/4)+2);
If you want to use Matlab only you can use this workflow:
Make your code to be single .m file. Any text file will work.
Read this file in cell column vector Code;
Process the lines adding using TeX or LaTeX formatting commands;
Create white figure with text(x,y,Code);
Export the figure.
This might be good way to produce many figures with same style.

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.

Saving a Matrix in MatLab

I have a MatLab program that generates a large 1000x1000 matrix. How can I save this matrix for use in future programs. Ideally, I want to save it as a particular variable. Here is the code that I am using.
function A = generateSPDmatrix(n)
A = rand(n,n); % generate a random n x n matrix
A = A+A';
A = A*A';
A = A + n*eye(n);
end
If you want to use it in future Matlab programs, you could do it like this:
save('A.mat', 'A');
To load, just do it like this:
load('A.mat');
% the file path is current path.
save('A.txt', 'A','-ascii');
% save to your file path
save('D:\test.txt','m','-ascii')
'D:\test.txt': file name and path
'm': your matrix
'-ascii': 8-digit ASCII format
See the matlab Help. Search the save(Save workspace variables to file) function
save(filename, variables, format) saves in the specified format: '-mat' or '-ascii'. You can specify the format option with additional inputs such as variables, '-struct' , '-append', or version.