views:

231

answers:

1

I have the following error in my code

Assignment has more non-singleton rhs dimensions than non-singleton

This is the code:

 string =['d:\face\face\ffw' int2str(r) '_' int2str(Sel(nSelRow,t)) '.bmp'];
 A = imread(string);
 B = im2double(A);
 Train_Dat(:,:,s)=B;

Update 1:

When I updated the code we got new error in the next row

my code

for r=1:Class_Num
    for t=1:Class_Train_Num
        %string=['e:\face_lib\feret_80\ff' int2str(r) '_' int2str(t) '.tif'];
        string =['d:\face\face\ffw' int2str(r) '_' int2str(Sel(nSelRow,t)) '.bmp'];
        A=imread(string);
        B=im2double(A);
        Train_Dat(:,:,:,s)=B;
        Train_Dat_Vector(:,s)=B(:); %here new error Subscripted assignment dimension mismatch.
        s=s+1;
    end

Update 2:

my define for 
nImgW = 40;
nImgH = 40;
nImgSize = nImgW*nImgH;
Train_Dat_Vector = zeros( nImgSize, Train_Num );
            A=imread(string);
            B=im2double(A);
            Train_Dat(:,:,:,s)=B;
            Train_Dat_Vector(:,s)=B(:);%here i want convert matrix to 40x40,Train_Num
            s=s+1;
+2  A: 

I think the problem is that B is most likely a 3-D RGB image, and you're trying to assign it to a single 2-D plane of your 3-D matrix Train_Dat. If you're trying to collect a set of 3-D images to use as training data, you will have to make Train_Dat either a 4-D matrix (if all of your images have the exact same dimensions) or a cell array (with one image per cell):

Example #1: a 4-D matrix...

nRows = 100;  %# The number of rows in the images
nCols = 100;  %# The number of columns in the images
nDepth = 3;   %# The depth of the images (3 color planes for RGB images)
nImages = 5;  %# The number of images you will use
Train_Dat = zeros(nRows,nCols,nDepth,nImages);  %# Initialize to zeros
Train_Dat(:,:,:,1) = B;                         %# Assign B as the first image

If you want to use this option, but all of your images are not the same size, you will have to resize them all to a given size. One way to do this, if you have the Image Processing Toolbox, is to use the function IMRESIZE:

newSize = [40 40];        %# The new size the image will be
C = imresize(B,newSize);  %# Resize image B

If you don't have access to the Image Processing Toolbox, one alternative is to use the function INTERP2 to resize your image. Here's one example of resizing a 3-D RGB image of type UINT8:

B = double(B);                   %# Convert B to double (needed to use INTERP2)
[nRows,nCols,nDepth] = size(B);  %# Get the old image size
C = zeros(40,40,3,'uint8');      %# Initialize the new 3-D 40-by-40 uint8 image
xi = linspace(1,nCols,40);       %# Create a down-sampled set of x points
yi = linspace(1,nRows,40);       %# Create a down-sampled set of y points
[X,Y] = meshgrid(xi,yi);         %# Create 40-by-40 grids of x and y points
C(:,:,1) = interp2(B(:,:,1),X,Y,'spline');  %# Interpolate the red color plane
C(:,:,2) = interp2(B(:,:,2),X,Y,'spline');  %# Interpolate the green color plane
C(:,:,3) = interp2(B(:,:,3),X,Y,'spline');  %# Interpolate the blue color plane

The image C will now be a down-sampled 40-by-40 version of B.

Example #2: a cell array...

nImages = 5;  %# The number of images you will use
Train_Dat = cell(1,nImages);  %# Initialize the cell array
Train_Dat{1} = B;             %# Assign B as the first image

In this case, the images you add to each cell can be different sizes and types, so no resizing is needed.

gnovice

related questions