convert mat file to image

Hello! I have a mat file attached. I have to perform classification problem later on using FFT+ Alexnet+transfer learning. So for the input in alexnet I want my data to be 2D from 1D as an image so I can implement further. What shall I do?

 Accepted Answer

s = load('100.mat')
s = struct with fields:
trial: 100 disp: 0 SAMPLES2READ: 648000 TIME: [0 0.0028 0.0056 0.0083 0.0111 0.0139 0.0167 0.0194 0.0222 0.0250 0.0278 0.0306 0.0333 0.0361 0.0389 0.0417 0.0444 0.0472 0.0500 0.0528 0.0556 0.0583 0.0611 0.0639 0.0667 0.0694 0.0722 0.0750 0.0778 0.0806 0.0833 0.0861 0.0889 … ] ATRTIMED: [2266×1 double] ANNOTD: [2266×1 double] i: 1 TR: [100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 121 122 123 124 200 201 202 203 205 207 208 209 210 212 213 214 215 217 219 220 221 222 223 228 230 231 232 233 234] Path: 'MIT-BIH_MAT\' LTR: 48 M: [648000×2 double]
There are a lot of variables in there. Which should turn into an image, and how?

13 Comments

M is my data that should turn into an image. I have plotted (attached)my M signals basically It shows two signals red and blue one both represent some info in ecg so later on i will extract features from here. But right now I want to convert this M into image.
alexnet accepts size of input image 227*227*RGB(3)
So do you want the first channel to be the reshaped first column of M and the second channel to be the reshaped second column of M and the third channel to be all black?
M = s.M;
numRows = ceil(sqrt(size(M(:, 1)))
MRed = reshape(M(:, 1), [numRows, numRows])
MRed = uint8(imresize(MRed, [227, 227]));
MGreen = reshape(M(:, 2), [numRows, numRows])
MGreen = uint8(imresize(MRed, [227, 227]));
MBlue = zeros(227, 227, class(M));
rgbImage = cat(3, MRed, MGreen, MBlue);
Well im totally new to this, i dont think im able to understand what do yu mean by all black chanel. can you elaborate the code also.
I think they want to draw their data in red and blue, capture the image, resize it to 227 x 227 x 3, and submit the result to alexnet classification.
Alright, here is a full demo for you:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 22;
markerSize = 20;
s = load('100.mat')
M = s.M;
[rows, columns] = size(M)
subplot(2, 2, 1);
M1 = M(:, 1);
M2 = M(:, 2);
plot(M1, 'b-');
hold on;
plot(M2, 'r-');
grid on;
r = sqrt(rows)
numRows = ceil(sqrt(rows))
title('M, both columns', 'FontSize', fontSize)
legend('M, Column1', 'M, Column 2', 'Location', 'southwest')
if r ~= numRows
message = 'WARNING: M cannot be reshaped into a square!'
uiwait(warndlg(message))
% Pad it so they can be a square.
M1(numRows^2) = 0;
M2(numRows^2) = 0;
end
MRed = reshape(M1, numRows, numRows);
MRed = imresize(MRed, [227, 227]);
% Scale to 0-255
MRed = uint8(rescale(MRed, 0, 255));
subplot(2, 2, 2);
imshow(MRed, [])
title('M1 going to the red channel', 'FontSize', fontSize)
MGreen = reshape(M2, numRows, numRows);
MGreen = imresize(MRed, [227, 227]);
MGreen = uint8(rescale(MGreen, 0, 255));
subplot(2, 2, 3);
imshow(MGreen, [])
title('M2 going to the green channel', 'FontSize', fontSize)
MBlue = zeros(227, 227, class(MRed));
subplot(2, 2, 4);
rgbImage = cat(3, MRed, MGreen, MBlue);
imshow(rgbImage)
title('RGB Image for AlexNet', 'FontSize', fontSize)
message =
'WARNING: M cannot be reshaped into a square!'
what is this error? btw can we explain me the code from line 19 onwards?
227 by 227 is a square. So before we resize the image into those dimensions, it must be a square. The dimensions of your M are such that it cannot be reshaped into a square. The last row would be only partially filled. So to make the "missing" elements of the last row zero I padded it with enough zeros to make sure that when reshaped it would be a full square. Then it can be reshaped into 227x227.
For example, what if your M was this
M = [1,2,3,4,5,6,7,8,9,10]
It can't fit into a 3x3 matrix but it can fit into a 4x4 matrix
M = [
1 2 3 4
5 6 7 8
9 10 missing, missing
missing, missing, missing, missing];
But it doesn't know what to put in the "missing" spots. So I just told it to put zeros there and now we can use reshape just fine.
The Deep Learning should learn to ignore the region of zeros at the end of the image.
r = sqrt(rows)
numRows = ceil(sqrt(rows))
if r ~= numRows
what these lines do specifically here?
Let's pick an M of length 10. So rows = 10. So look at
rows = 10;
r = sqrt(10)
r = 3.1623
We see that r = 3.1623. However matrices can't have 3.1623 rows. It must be either 3 or 4. But we can't fit all of M into a 3x3 = 9 element matrix because M has 10 elements and one would be left out. So that means we need to get 4 by taking the number rounded up to the next higher integer using ceil():
numRows = ceil(sqrt(rows))
numRows = 4
But that means that there will be some missing elements because M, which has only 10 elements, does not have enough elements to fill up the 16 elements needed for a 4 x 4 matrix.
So, to alert the user to that fact, that we were going to have to pad the matrix, I check if r equals numRows
if r ~= numRows
If theyre the same, like if M had 16 elements instead of 10 they'd both be 4, then there will be no warning. But in this example 3.1623 is not equal to 4 so I decide to alert the user. If you don't want to be warned of that fact, then just comment out the if statement. It's not absolutely required for the code to run.
Ok so now i have a question, We transforming 1D signal into RGB image, 1channel M1 is Red M2 is green whats blue? I dont have 3rd channel. What Im suppoed to do for such cases? im still confused
What are the possibilities? Which possibilities have advantages or disadvantages?
  • constant 0; constant 255; constant 127 or 128
  • copy red channel; copy green channel
  • some linear combination of red and green channel
None of the above add new information.
If you were training, then some of the possibilities would run the risk that the training would waste time chasing apparent information that was not really there.
But you are not training: you are using a pre-trained AlexNet for classification purposes. So then question then becomes which of those possibilities lead to the best predictions from that pre-trained network.
I think you will have a very difficult time getting anything useful out of the pre-trained alexnet for your purposes. I imagine, though, that your project assignment is to try it anyhow and see what you can do anyhow, and then analyze the failures, to demonstrate that you understand how the network is intended to work and talk about why it did not work for you.
(Expand to see Walter's comment above.)
If you're retraining Alexnet (transfer learning by replacing some layers), then all you have is info for two color channels. You can put them into whatever color channel you want but that still leaves one color channel that you have to create. I created it as just all zeros. I could be wrong but I think that with all zeros it might learn more quickly to ignore that channel completely. And that's what we need. We need all the weights for pixels in the 3rd channel to be zero since there is no additional information in that channel. The network should learn, during the retraining, to ignore that channel.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!