Subscripted assignment dimension mismatch.

1 次查看(过去 30 天)
hi everybody...i doing this code but i have this problem in code can any one help me plz
input_dir = 'E:\matlab\pcaimage';
image_dims = [48, 64];
filenames = dir(fullfile(input_dir, '*.jpg'));
num_images = length(filenames);
images = [];
for n = 1:num_images
filename = fullfile(input_dir, filenames(n).name);
img = imread(filename);
if n == 1
% img=reshape(img,48,64);
images = zeros(prod(image_dims), num_images);
end
images(:, n) = img(:);
end
the size of my images is 1200X900 i try to resize the images into 48X64 the problem is
Subscripted assignment dimension mismatch.
Error in eigenvector (line 14) images(:, n) = img(:);
can any one help me plz

采纳的回答

Geoff Hayes
Geoff Hayes 2014-6-1
编辑:Geoff Hayes 2014-6-1
The above code seems to be assuming that the images are two dimensional only. Is this a valid assumption, or are the images (or matrices) 1200x900x3?
The error message is telling you that there is dimension mismatch in your assignment at line
images(:, n) = img(:);
In this case, the code is trying to insert a vector (once the colon is used in img(:)) that is larger than the destination row of images. As you have stated, the size of any image is 1200x900 (so 1080000 elements) BUT images has only been sized a matrix with n rows and 3072=48*64 columns…which is considerably smaller than 1080000. Hence the error.
In the line prior to this assignment, there is an attempt to reshape the data which has been commented out since this line probably generated the error To RESHAPE the number of elements must not change. Since you want to resize, the code should use the imresize command instead:
img = imresize(img,[48 64]);
Using imresize will reduce the image to the 48x64 size which can then be assigned to images.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by