convert mat file to image
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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?
채택된 답변
Image Analyst
2022년 12월 12일
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
bruno
2022년 12월 12일
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.

bruno
2022년 12월 12일
alexnet accepts size of input image 227*227*RGB(3)
Image Analyst
2022년 12월 12일
편집: Image Analyst
2022년 12월 13일
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);
bruno
2022년 12월 12일
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.
Walter Roberson
2022년 12월 12일
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.
Image Analyst
2022년 12월 13일
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)

bruno
2022년 12월 14일
message =
'WARNING: M cannot be reshaped into a square!'
what is this error? btw can we explain me the code from line 19 onwards?
Image Analyst
2022년 12월 14일
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.
bruno
2022년 12월 14일
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.
bruno
2022년 12월 14일
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
Walter Roberson
2022년 12월 15일
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.
Image Analyst
2022년 12월 15일
(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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
