How to perform normalization of images, splitting of datasets and data processing correctly?

4 views (last 30 days)
I’m fairly new to MATLAB. I have created a file with 500 images of apples, all of them have a size of 32*32*3. I want to normalize the images to obtain a dimension of 1024.
I've split the first 450 images into a training dataset and the remaining 50 images into a testing dataset. The next step would be to apply global mean and variance to the images, but I don't know how to write a function in MATLAB to find out the mean and variance. Can anyone help me?
images ='D:\group_2\apples';
for i=1:500;
jpgfiles=dir(fullfile(images,'\*.jpg*'));
n=numel(jpgfiles(i));
im=jpgfiles(i).name
im1=imread(fullfile(images,im));
gray_images= rgb2gray(im1);
new_images = reshape(gray_images, [1024,1])
imshow(gray_images);
end
% MATLAB returns an error indicating that the index exceeds the number of array elements(0)
[setTrain, setTest] = partition(images, [0.9, 0.1], 'randomized');
mean1 = mean2(new_images)
sd1 = std2(new_images)
grayImage = (double(new_images) - mean1) * (0.005/sd1);
subplot(2, 1, 2);
imshow(grayImage, []);
mean2 = mean2(grayImage)
sd2 = std2(grayImage)

Accepted Answer

Image Analyst
Image Analyst on 16 Mar 2022
Pull dir() out of the loop so you only look as many time as there are images. There might not be 500 images like you thought.
imageFolder ='D:\group_2\apples';
jpgFiles = dir(fullfile(imageFolder, '*.jpg*'));
numImages = numel(jpgFiles)
for k = 1 : numImages;
% Create full file name.
fullFileName = fullfile(jpgfiles(k).folder, jpgfiles(k).name);
% Read in image.
thisImage = imread(fullFileName);
% Cast to gray scale if necessary
if ndims(thisImage) == 3
% It's RGB. Convert to gray scale.
gray_image = rgb2gray(thisImage);
else
% It's already gray scale.
gray_image = thisImage;
end
% 32*32 is 1024. Reshape gray_image into a column vector with 1024 rows but only one column.
% Not sure why this is wanted.
new_image = reshape(gray_image, [], 1]); % or (simpler) new_image = gray_image(:)
% Display the gray scale column vector.
imshow(gray_images); % Will appear as a vertical line.
end

More Answers (1)

yanqi liu
yanqi liu on 16 Mar 2022
yes,sir,may be use
mat2gray 、rescale、mapminmax to process data,such as
img = double(imread('rice.png'));
[~,PS] = mapminmax(img,0,1);
Y = mapminmax('apply',img,PS);
Y2 = mat2gray(img);
Y3 = rescale(img, 0, 1);

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!