How to perform normalization of images, splitting of datasets and data processing correctly?
4 次查看(过去 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)
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!