problem inconsistent dimension arrays

Hello everyone. So here is my problem: i receive a dataset of images with signatures (specifically the CEDAR dataset) and i process it with the help of a function. What I'm actually doing in the function is extracting features (11 features). Ι want to create 4 patches for each image to better train the machine learning models to distinguish the fake signature from the real one. When i run the code i get this error: "Dimensions of arrays being concatenated are not consistent". Below, i will show you the main code for forgeries signatures (i have the same code for original signatures) and a piece of code of Function:
Main code:
clc;
clear;
close all;
format compact;
myFolder = './full_forg';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
jpegFiles = dir(filePattern);
varray=[];
% for each fake image
for k = 1:length(jpegFiles)
k
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% read the image
im1=imread(fullFileName);
%im1=imresize(im1,[650 650]);
im1=imresize(im1,[1024 1024]);
% create the patches
patchit(im1,[512 512]);
% read the patches
load patches.mat;
% get the number of patches
[~,~,~,pn]=size(patch);
v=[];
active_pixesls=[];
% for each patch
for i=1:pn
im_patch=patch(:,:,:,i);
[F,croped_image]=featureExtraction(im_patch);
v=[v F];
active_pixesls=[active_pixesls sum(croped_image(:))];
end
response=0; % because it is a fake image
v=[v active_pixesls response];
varray=[varray; v];
end
Function:
function [Feat_Val,im_processed] = featureExtraction(I)
S=512;
I2=imresize(I,[S S]);
dbstop if error;
dbstop if warning;
%I3=rgb2gray(I2);
I3=im2gray(I2);
I3=im2double(I3);
I3=im2bw(I3); %converting image to black and white
%I3 = bwmorph(~I3, 'thin', inf) %thining the image
I3 = bwmorph(~I3, 'close', inf);
I3=~I3;
Can someone tell me how to deal with this error?
Thanks a lot!!

2 Comments

Which lines gives the error, and what is the full error message?
The error i get it in command "varray=[varray ; v]". The full error message is: "Error using vertcat. Dimensions of arrays being concatenated are not consistent".

Sign in to comment.

 Accepted Answer

the cyclist
the cyclist on 16 May 2023
Edited: the cyclist on 16 May 2023
We can't run your code because we don't have all the inputs to do so. I suggest using the debugger to halt your code at that line, and see that the variables look like. (It might not produce the error the first time through the loop.)

17 Comments

Actually i would like to send the entire error message as a screenshot but according toy MATLAB Answers not allowed to upload photos. In terms of inputs, i accept all images of the CEDAR dataset, which is the following:
If there is any more information you need let me know.
One can certainly upload images, so I'm not sure what your issue was.
You should not expect everyone here to download that entire dataset to help you debug a simple MATLAB error. Instead, you could upload one or two of those images here, and make a small piece of code that people can run to replicate your error. Make it easy for people to help you.
Maybe I'm wrong but before I made the post it said somewhere "don't upload photos", but it's ok. Here are some usefull screenshots:
I'm sorry, i deleted the previous screenshots because i wanted to upload a alternative screenshot and now i cant upload any screenshot for 24 hours.
Hmm ok i will check this and thank you. I would send you the entire dataset in a zip file but it's over 5MB so unfortunately i can't send it. This dataset contains 1321 forgeries and 1321 genuines signatures.
To be clear, no one needs all the signatures to debug your code. I expect your code breaks even if you have just a few signatures.
Ok perfect, i can send you tommorow a signature that the code breaks (i can't today because i I exceeded the number of uploads).
Hello again. So, in continuation of our previous discussion, i send you one photo with entire screen of Matlab and one photo of a signature that code breaks (from CEDAR dataset).
I still can't run your code.
First, there was a problem with patch. I guessed you are using patchit, so I downloaded it.
But the featureExtraction function
function [Feat_Val,im_processed] = featureExtraction(I)
that you uploaded here is not assigning values for Feat_Val or im_processed, so the code errors out there.
Have you spent any time using my suggestion to try the debugger to solve your own problem?
Yes i use patches. Actually why the function is not assigning values for Feat_val or im_processed? I don't undertand this.
Yes i have spent time but i didn't find a solution. I still search it with the help of the dubugger.
Here is the featureExtraction function you uploaded yourself:
function [Feat_Val,im_processed] = featureExtraction(I)
S=512;
I2=imresize(I,[S S]);
dbstop if error;
dbstop if warning;
%I3=rgb2gray(I2);
I3=im2gray(I2);
I3=im2double(I3);
I3=im2bw(I3); %converting image to black and white
%I3 = bwmorph(~I3, 'thin', inf) %thining the image
I3 = bwmorph(~I3, 'close', inf);
I3=~I3;
It never calculates Feat_Val or im_processed.
I haven't send all the code because it is very big. Is it ok if i send it here but without screenshot?
I suggest you attach it in the same way you attached the PNG files.
There is no need for screenshot of code.
Ok i will send the code of function here, without photos because I will need to upload many photos and I will exceed the limit again.
function [Feat_Val,im_processed] = featureExtraction(I)
S=512;
I2=imresize(I,[S S]);
dbstop if error;
dbstop if warning;
%I3=rgb2gray(I2);
I3=im2gray(I2);
%I3=medfilt2(I3]);
I3=im2double(I3);
I3=im2bw(I3); %converting image to black and white
%I3or=I3;
%I3 = bwmorph(~I3, 'thin', inf); %thining the image %thining the image
I3 = bwmorph(~I3, 'close', inf);
I3=~I3;
im1=I3;
k=1;
for i=1:S
for j=1:S
if(I3(i,j)==0)
u(k)=i;
v(k)=j;
k=k+1;
I3(i,j)=1;
end
end
end
if k==1
Feat_Val =zeros(1,33);
im_processed=true(size(im1,[1 2]));
else
C=[u;v];
N=k-1;%the number of pixels in the signature
oub=sum(C(1,:))/N; %the original x co-ordinate center of mass of the image
ovb=sum(C(2,:))/N; %the original y co-ordinate center of mass of the image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%********ROTATE******%%%%%%%%%%%%%%%%%%%%%%%%
%moving the signature to the origin
for i=1:N
u(i)=u(i)-oub+1;
v(i)=v(i)-ovb+1;
end
% the new curve of the signature
C=[u;v];
ub=sum(C(1,:))/N;
vb=sum(C(2,:))/N;
ubSq=sum((C(1,:)-ub).^2)/N;
vbSq=sum((C(2,:)-vb).^2)/N;
for i=1:N
uv(i)=u(i)*v(i);
end
uvb=sum(uv)/N;
M=[ubSq uvb;uvb vbSq];
%calculating minimum igen value of the matrix
minIgen=min(abs(eig(M)));
%the eigen vector
MI=[ubSq-minIgen uvb;uvb vbSq-minIgen];
theta=(atan((-MI(1))/MI(2))*180)/pi;
thetaRad=(theta*pi)/180;
rotMat=[cos(thetaRad) -sin(thetaRad);sin(thetaRad) cos(thetaRad)];
%% rotating the signature and passing the new co-ordinates
for i=1:N
v(i)=(C(2,i)*cos(thetaRad))-(C(1,i)*sin(thetaRad));
u(i)=(C(2,i)*sin(thetaRad))+(C(1,i)*cos(thetaRad));
end
C=[u;v];
%moving the signature to its original position
for i=1:N
u(i)=round(u(i)+oub-1);
v(i)=round(v(i)+ovb-1);
end
%after rotating the image the signature might go out of the boundry (128x128) therefore
%we have to move the signature curve
mx=0;%the moving x co-ordinate
my=0;%the moving y co-ordinate
if (min(u)<=0)
mx=-min(u);
for i=1:N
u(i)=u(i)+mx+1;
end
end
if (min(v)<=0)
my=-min(v);
for i=1:N
v(i)=v(i)+my+1;
end
end
C=[u;v];
for i=1:N
I3((u(i)),(v(i)))=0;
%I3((u(i)),(v(i)))=1;
end
% removing extra white space in sides
xstart=S;
xend=1;
ystart=S;
yend=1;
for r=1:S
for c=1:S
if((I3(r,c)==0))
if (r<ystart)
ystart=r;
end
if((r>yend))
yend=r;
end
if (c<xstart)
xstart=c;
end
if (c>xend)
xend=c;
end
end
end
end
%cutting the image and copying it to another matrix
for i=ystart:yend
for j=xstart:xend
im((i-ystart+1),(j-xstart+1))=I3(i,j);
end
end
%cropped image
im_processed = im;
% Feature Extraction - NSA --------------------------
PixelB = 0;
PixelA = 0;
for i=ystart:yend
for j=xstart:xend
if (im(i-ystart+1,j-xstart+1)== 0)
PixelB = PixelB + 1;
end
PixelA = PixelA + 1;
end
end
%disp([PixelB,PixelA]);
NSA = PixelB/PixelA;
NSA2= 1-NSA;
PixelB = 0;
PixelA = 0;
for i=ystart:yend
for j=xstart:xend
if (im(i-ystart+1,j-xstart+1)== 1)
PixelB = PixelB + 1;
end
PixelA = PixelA + 1;
end
end
%NSA2 = PixelB/PixelA;
% disp(NSA);
% Feature Extration - Aspect Ratio -----------------
height_sign = yend-ystart;
length_sign = xend-xstart;
aspect_ratio = length_sign/height_sign;
% Feature Extraction - Maximum Horizontal and Vertical Projection
max7=0;
for i=ystart:yend
summ=0;
for j=xstart:xend
if(im((i-ystart+1),(j-xstart+1))==0)
summ=summ+1;
end
end
if (summ>max7)
max7=summ;
end
end
max7;
max1=0;
for i=xstart:xend
summ=0;
for j=ystart:yend
if(im((j-ystart+1),(i-xstart+1))==0)
summ=summ+1;
end
end
if (summ>max1)
max1=summ;
end
end
max1;
xdiff=xend-xstart;
ydiff=yend-ystart;
Hor_Proj = max7/xdiff;
Ver_Proj = max1/ydiff;
max2=0;
for i=ystart:yend
summ=0;
for j=xstart:xend
if(im((i-ystart+1),(j-xstart+1))==1)
summ=summ+1;
end
end
if (summ>max2)
max2=summ;
end
end
max2;
max3=0;
for i=xstart:xend
summ=0;
for j=ystart:yend
if(im((j-ystart+1),(i-xstart+1))==1)
summ=summ+1;
end
end
if (summ>max3)
max3=summ;
end
end
max3;
xdiff1=xend-xstart;
ydiff1=yend-ystart;
Hor_Proj1 = max2/xdiff1;
Ver_Proj1 = max3/ydiff1;
% Feature Extraction - Minimun Horizontal and Vertical Projection
min1=0;
for i=ystart:yend
summ1=0;
for j=xstart:xend
if(im((i-ystart+1),(j-xstart+1))==0)
summ1=summ1+1;
end
end
if (summ1<min1)
min1=summ1;
end
end
min1;
min2=0;
for i=xstart:xend
summ1=0;
for j=ystart:yend
if(im((j-ystart+1),(i-xstart+1))==0)
summ1=summ1+1;
end
end
if (summ1<min2)
min2=summ1;
end
end
min2;
xdiff2=xend-xstart;
ydiff2=yend-ystart;
Hor_Proj2 = min1/xdiff2;
Ver_Proj2 = min2/ydiff2;
min3=0;
for i=ystart:yend
summ1=0;
for j=xstart:xend
if(im((i-ystart+1),(j-xstart+1))==1)
summ1=summ1+1;
end
end
if (summ1<min3)
min3=summ1;
end
end
min3;
min4=0;
for i=xstart:xend
summ1=0;
for j=ystart:yend
if(im((j-ystart+1),(i-xstart+1))==1)
summ1=summ1+1;
end
end
if (summ1<min4)
min4=summ1;
end
end
min4;
xdiff3=xend-xstart;
ydiff3=yend-ystart;
Hor_Proj3 = min3/xdiff3;
Ver_Proj3 = min4/ydiff3;
% Feature Extraction End Points ---------------------------
i1 = im1;
[row, col, depth] = size(i1);
%add row%
addrow = ones(1, col);
i1 = [addrow; addrow; i1; addrow];
[row, col, depth] = size(i1);
%add column%
addcol = ones(row, 1);
i1 = horzcat(addcol, i1, addcol, addcol);
[row, col, depth] = size(i1);
i1=~i1;
crosspoints=0;
for r = 3:row-1
for c = 2:col-2
if(i1(r,c)==1)
if (i1(r-1,c-1)+i1(r-1,c)+i1(r-1,c+1)+i1(r,c-1)+i1(r,c+1)+i1(r+1,c-1)+i1(r+1,c)+i1(r+1,c+1)==1)
crosspoints=crosspoints+1;
end
end
end
end
if mod(xdiff,2)==1
xdiff=xdiff-1;
end
if mod(ydiff,2)==1
ydiff=ydiff-1;
end
% Feature Extraction Center of Gravities of the vertically divided images--
n1 = im(:, 1: xdiff/2);%splitting images
n2 = im(:, xdiff/2+1:xdiff);
sum1=0;
pix_total=0;
%for the first half
for i=1:ydiff
pix_sum=0;
for j=1:xdiff/2
if(n1(i,j)==0)
pix_sum=pix_sum+1;
pix_total=pix_total+1;
end
end
sum1=sum1+(pix_sum*i);
end
Y1=sum1/pix_total;
RY1=Y1/ydiff;
sum1=0;
for i=1:xdiff/2
pix_sum=0;
for j=1:ydiff
if(n1(j,i)==0)
pix_sum=pix_sum+1;
end
end
sum1=sum1+(pix_sum*i);
end
X1=sum1/pix_total;
RX1=2*X1/xdiff;
%for the secong half
sum1=0;
pix_total=0;
for i=1:ydiff
pix_sum=0;
for j=1:xdiff/2
if(n2(i,j)==0)
pix_sum=pix_sum+1;
pix_total=pix_total+1;
end
end
sum1=sum1+(pix_sum*i);
end
Y2=sum1/pix_total;
RY2=Y2/ydiff;
sum1=0;
for i=1:xdiff/2
pix_sum=0;
for j=1:ydiff
if(n2(j,i)==0)
pix_sum=pix_sum+1;
end
end
sum1=sum1+(pix_sum*i);
end
X2=sum1/pix_total;
RX2=2*X2/xdiff;
centroid = [ [RX1 RY1] [RX2 RY2] ];
% Feature Extraction - Slope ------------------
m=xdiff;
m=m/2;
k=m+X2;
slope=(Y2-Y1)/(k-X1);
% Feature Extraction - regionprops ------------------
% Feature Extraction - GLCM properties-entropy ------------------
BW=entropy(im);
glcm=graycomatrix(im);
stats=graycoprops(glcm,'Contrast Correlation Energy Homogeneity');
Contrast=stats.Contrast;
Correlation=stats.Correlation;
Energy=stats.Energy;
Homogeneity=stats.Homogeneity;
% Feature Extraction - regionprops ------------------
stats = regionprops('table',im,'Centroid','Area','BoundingBox','ConvexArea','Eccentricity','EquivDiameter','EulerNumber','Extent','FilledArea','Orientation','Perimeter','Solidity',...
'MajorAxisLength','MinorAxisLength');
centers = stats.Centroid;
a=(centers)/100;
ax=median(a,1);
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
b=(diameters)/100;
bx=median(b,1);
radii = diameters/2;
cc=(radii)/100;
ccx=median(cc);
d=stats.Area;
dx=median(d);
xmin=0.9;
xmax=1;
n=1;
x=xmin+rand(1,n)*(xmax-xmin);
e=stats.BoundingBox;
ex=median(e,1);
f=stats.ConvexArea;
fx=median(f);
g=stats.Eccentricity;
gx=median(g);
h=stats.EquivDiameter;
hx=median(h);
ii=stats.EulerNumber;
iix=median(ii);
j=stats.Extent;
jx=median(j);
kk=stats.FilledArea;
kkx=median(k);
l=stats.Orientation;
lx=median(l);
mm=stats.Perimeter;
mmx=median(mm);
n=stats.Solidity;
nx=median(n);
%Feat_Val = [NSA Hor_Proj Ver_Proj Hor_Proj2 Ver_Proj2 crosspoints centroid BW Contrast Correlation Energy Homogeneity ax bx ccx dx ex fx gx hx iix jx kkx lx mmx nx];
Feat_Val= [NSA aspect_ratio Hor_Proj crosspoints centroid slope];
if length(Feat_Val)~=33
1==1;
end
end
end
I was not able to recreate your error.
Here is what I tried:
  • Copied your "Main code" from the original question
  • Copied your featureExtraction function just above
  • Made 5 copies of the signature image you uploaded, and put them in the full_forg directory
Your code ran to completion, without error.
Hello again. So i fixed it but also i have an issue. I send you two pictures one of the code which i filled and one with the error:
The reason that i use the function reshape is because i want the "v" vector to has equal dimension with "varray" vector.
First, instead of pasting screenshots, it is better to cut & paste the code itself, or upload the code as an M-file. If you do a screenshot, we need to re-type your code if we want to try it.
Second, and I am repeating myself now, you should share a complete example that I can just paste into MATLAB to replicate your error. For example, here, I don't know what input caused the error, I don't know the size of v, etc. I have to make a lot of guesses. Again, make it as easy as possible for people to help you.
In this particular case, here is my guess:
If v is shorter than 137 elements, look what happens:
% Define a vector that is too short
v = rand(1,125);
% Use your code
v = reshape(v(1:137),[1 137])
Index exceeds the number of array elements. Index must not exceed 125.
The reason is that your code tries to access elements that do not exist. (In my example, elements 126:137 do not exist.)
I don't know if it makes sense in your application, but if the vector is too short, perhaps you can add NaN or zero values to it, to make it length 137.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!