How to convert Non Image data to images for use in CNN

I have non-image data that I would like to convert to images for use in CNN. Any help would be appreciated.

 Accepted Answer

Perhaps the reshape and imresize functions would work. What form is your non-image data in? Vectors? You can just write them into an image.

7 Comments

@Image Analyst Thanks for your prompt response. Here is the detail:
The file Absorp_MD.txt contains 50 spectra (each rep. by a row) serving as y-axis and lamda_MD.txt is the x-axis data. What I want is to convert the spectra to images suitable for CNN. I hope my explaination is clear.
Thanks while I await your response.
Did you try readmatrix?
% Create image from text file
grayImage = readmatrix('Absorp_MD.txt');
[rows, columns, numberOfColorChannels] = size(grayImage)
rows = 50
columns = 21
numberOfColorChannels = 1
imshow(grayImage, []);
axis('on', 'image')
%impixelinfo
@Image Analyst I thought the output image should be 50 different images. What you generated is like a single image. Could you please clarify it ? Sorry for disturbing you with more questions
If you want each 21 element image to be stored as a 3 by 7 image, you can do this:
% Create image from text file
grayImage = readmatrix('Absorp_MD.txt');
[rows, columns, numberOfColorChannels] = size(grayImage)
imshow(grayImage, []);
axis('on', 'image')
%impixelinfo
% Break up into 50 different images.
for row = 1 : rows
thisImage = reshape(grayImage(row, :), 3, 7);
fullFileName = fullfile(pwd, sprintf('Image%2.2d.png', row));
fprintf('Saving %s\n', fullFileName);
imwrite(thisImage, fullFileName);
end
This is exactly what I want. Thanks @Image Analyst
Can the image be used in CNN for further processing? I dont really understand the reason for choosing (3,7 ) in line 9. I understand 3 is for the gray scale, is it related to my data? Also, is it possible to view the images?
Thanks so much!
Of course you can view the images but they will be very small so you need to magnify them to see them.
imshow(thisImage, [], 'InitialMagnification', 3000);
Remember images must be rectangular. 3 is not for gray scale. So since you have 21 elements, you can either have a 3x7 or a 7x3 image. So you don'[t really have much choice. 3*7 = 21 and 7*3 = 21. No other combinations multiply to give you 21. Or you can have a different size if you want if you paste it in to the upper left corner.
Remember some predefined networks require images of a certain size so you may have to use imresize() with the 'nearest' option if you want to use a prebuilt network such as AlexNet. Or you can build your own, which might train faster since your images will be so much smaller than those used for prebuilt networks, such as 227x227 for Alexnet.

Sign in to comment.

More Answers (1)

You can use an arrayDatastore after reading in the files, or perhaps a tabularTextDatastore . You probably do not need to convert to images.

3 Comments

I have previously used imageDatastore after following a solution posted in another discussion. However, there is no clue on how to compare input data with the target one and how I can view the output. Do you have idea on this? Here is the code for your reference. Thanks
location = 'C:\Users\z5288520\Downloads\NonImageDataMatlab\signal.mat';
imds = imageDatastore(location, 'FileExtensions', '.mat', 'IncludeSubfolders',1, ...
'LabelSource','foldernames');
countEachLabel(imds);
trainingDS = imds;
% Convert labels to categoricals
trainingDS.Labels = categorical(trainingDS.Labels);
trainingDS.ReadFcn = @readFcn1;
layers = [ ...
imageInputLayer([94 21 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(1)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',20,...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(imds,layers,options);
Sorry could you please try it for me on the data I attached? I send me the complete code? Thanks for your time

Sign in to comment.

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!