I need help on cell array and Imaging processing

Hi
I am using the following code
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
% img2= imshow(img, 'InitialMagnification', 800);
img3= imresize(img, [100 100]);
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %#ok<SAGROW>
end
hiddenSize = 25;
autoenc = trainAutoencoder(Train,hiddenSize,'MaxEpochs',1000,...
'DecoderTransferFunction','purelin','EncoderTransferFunction','satlin','L2WeightRegularization',0.0004,'SparsityRegularization',4,'SparsityProportion',0.15);
numImages = numel(TestData.Files);
for i = 1:numImages
img5 = readimage(TestData, i);
img6= imresize(img, [100 100]);
img7= imshow(img3, 'InitialMagnification', 800);
drawnow;
Test{i} = (img6); %#ok<SAGROW>
end
xReconstructed = predict(autoenc,Test);
%% Test Images
figure();
for i = 1:10
subplot(4,5,i);
imshow(TestData.Files{i});
end
%% Reconstructed images from TestData
figure();
for i = 1:10
subplot(4,5,i);
imshow(xReconstructed(i))
end
But I got the following error
Error using imageDisplayValidateParams
Expected input number 1, I, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Instead its type was cell.
Error in images.internal.imageDisplayValidateParams (line 11)
validateattributes(common_args.CData, {'numeric','logical'},...
Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in data_process1 (line 51)
imshow(xReconstructed(i))
When I compare to the example,
https://www.mathworks.com/help/deeplearning/ref/trainautoencoder.html
the issue is with Train{i}and Test {i}, The model is not trained probably with given data. When I compared with example in MATLAB for digitTrainCellArrayData and digitTTestCellArrayData I am having problems and my cell array which are not the same as in example
My cell arrays for Train{i}and Test {i} are like this like for example Train{i}
But in the example in MATLAB for digits, digitTrainCellArrayData and digitTestCellArrayData are like this
digitTrainCellArrayData
In addition to that. Range in example is from 0 – 1 while my data is from 0 – 255
Kindly looking for your support

 採用された回答

drummer
drummer 2020 年 10 月 26 日

0 投票

figure();
for i = 1:10
subplot(4,5,i);
imshow(TestData.Files{i});
end
Hi.
It's probably your argument for imshow that is cell type, while it only accepts the type mentioned in your error message.
Try this workaround
figure();
for i = 1:10
subplot(4,5,i);
image = cell2mat(TestData.Files{i});
imshow(image);
end
I haven't tested, so before performing within the loop, try outside, with a single image.
Cheers

15 件のコメント

Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
Hi drummer
Thanks for yur reply
I have problems with commands
Train{i} = (img3);
Test{i} = (img6);
I am not converting the images into correct matrix required model
I should have 1-by-102 cell array, where each cell containing a 28-by-28 matrix representing a synthetic image. like this with values 0 to 1
but i having this with wrong range 0 255
Walter Roberson
Walter Roberson 2020 年 10 月 26 日
im2double() to convert [0 255] range to [0 1] range
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
Hi Walter
I am using the following code
close all;
clc;
%% Initalize the data
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
% img2= imshow(img, 'InitialMagnification', 800);
img3= imresize(img, [100 100]);
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %#ok<SAGROW>
end
hiddenSize = 25;
autoenc = trainAutoencoder(Train,hiddenSize,'MaxEpochs',1000,...
'DecoderTransferFunction','purelin','EncoderTransferFunction','satlin','L2WeightRegularization',0.0004,'SparsityRegularization',4,'SparsityProportion',0.15);
numImages = numel(TestData.Files);
for i = 1:numImages
img5 = readimage(TestData, i);
img6= imresize(img, [100 100]);
img7= imshow(img3, 'InitialMagnification', 800);
drawnow;
Test{i} = (img6); %#ok<SAGROW>
end
xReconstructed = predict(autoenc,Test);
%% Test Images
figure();
for i = 1:10
subplot(4,5,i);
imshow(TestData.Files{i});
end
%% Reconstructed images from TestData
figure();
for i = 1:10
subplot(4,5,i);
imshow(xReconstructed(i))
end
But I got the following error
Error using imageDisplayValidateParams
Expected input number 1, I, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Instead its type was cell.
Error in images.internal.imageDisplayValidateParams (line 11)
validateattributes(common_args.CData, {'numeric','logical'},...
Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in data_process1 (line 51)
imshow(xReconstructed(i))
When I compare to the example,
https://www.mathworks.com/help/deeplearning/ref/trainautoencoder.html
the issue is with Train{i}and Test {i}, The model is not trained probably with given data. When I compared with example in MATLAB for digitTrainCellArrayData and digitTTestCellArrayData I am having problems and my cell array which are not the same as in example
My cell arrays for Train{i}and Test {i} are like this like for example Train{i}
But in the example in MATLAB for digits, digitTrainCellArrayData and digitTestCellArrayData are like this
digitTrainCellArrayData
In addition to that. Range in example is from 0 – 1 while my data is from 0 – 255
Kindly looking for your support
drummer
drummer 2020 年 10 月 26 日
uHm, I see, so you should insert Walter's suggestion while you're transforming your imds.
for i = 1:numImages
img = readimage(TrainData, i);
% img2= imshow(img, 'InitialMagnification', 800);
img3= im2double(imresize(img, [100 100])); % inserting Walter's suggestion.
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %#ok<SAGROW>
end
Check if it works to get your image range.
Walter Roberson
Walter Roberson 2020 年 10 月 26 日
  • If Xnew is a cell array of image data, then Y is also a cell array of image data, where each cell contains the data for a single image.
Test{i} = (img6); %#ok<SAGROW>
So Test is a cell array.
xReconstructed = predict(autoenc,Test);
You are passing that cell array to predict() and so you are getting a cell array out of it.
imshow(xReconstructed(i))
Using () indexing on a cell array gives you a cell array output. However, imshow() cannot display cell arrays. You need
imshow(xReconstructed{i})
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
編集済み: Abdussalam Elhanashi 2020 年 10 月 26 日
Hi Walter
Thanks for your support
For Train and Test that i have is like this
But I want like this
How to make that to be as columns and raws
Walter Roberson
Walter Roberson 2020 年 10 月 26 日
We already said to use im2double() to change the scale.
You might possibly also want to take im2double( uint8(255) - XTrain{1,1} ) if you want to change the white (255) into black (0)
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
Hi walter
Thanks for your help
What i want is to have minimize the color for back ground of reconstructed images and here in the the currect results i have
Test images as input to detector
Reconstructed images as output from the detector
as you can notice that background is still with black color and it has to be white color the same as Test images or almost similar
Walter Roberson
Walter Roberson 2020 年 10 月 26 日
as you can notice that background is still with black color
No, I do not notice that. The background looks white to me in the reconstructed images.
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
Hi Walter
Yes, What I mean is it possible to increase brightness for background to be as original Test images or that is normal for Reconstructed images to be like this
Walter Roberson
Walter Roberson 2020 年 10 月 26 日
reconstructed(imbinarize(reconstructed)) = 1;
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 26 日
Hi Walter
I tried ths line but i got error
reconstructed(imbinarize(xReconstructed)) = 1;
Error using imbinarize
Expected I to be one of these types:
uint8, uint16, uint32, int8, int16, int32, single, double
Instead its type was cell.
Error in imbinarize>validateImage (line 262)
validateattributes(I,supportedClasses,supportedAttribs,mfilename,'I');
Error in imbinarize>parseInputs (line 198)
validateImage(I);
Error in imbinarize (line 134)
[I,isNumericThreshold,options] = parseInputs(I,varargin{:});
Walter Roberson
Walter Roberson 2020 年 10 月 27 日
reconstructed = xReconstructed{i};
reconstructed(imbinarize(xReconstructed)) = 1;
imshow(reconstructed)
Abdussalam Elhanashi
Abdussalam Elhanashi 2020 年 10 月 27 日
Hi walter I tried this but i got error
reconstructed(imbinarize(xReconstructed)) = 1;
Error using imbinarize
Expected I to be one of these types:
uint8, uint16, uint32, int8, int16, int32, single, double
Instead its type was cell.
Error in imbinarize>validateImage (line 262)
validateattributes(I,supportedClasses,supportedAttribs,mfilename,'I');
Error in imbinarize>parseInputs (line 198)
validateImage(I);
Error in imbinarize (line 134)
[I,isNumericThreshold,options] = parseInputs(I,varargin{:});
Walter Roberson
Walter Roberson 2020 年 10 月 27 日
reconstructed = xReconstructed{i};
reconstructed(imbinarize(reconstructed)) = 1;
imshow(reconstructed)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by