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

조회 수: 1 (최근 30일)
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)

채택된 답변

Image Analyst
Image Analyst 2022년 3월 16일
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

추가 답변 (1개)

yanqi liu
yanqi liu 2022년 3월 16일
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);

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by