"Subscripted assignment dimension mismatch" Error
Mostrar comentarios más antiguos
Hello,
I am working on a script that was made a few years ago. I had to make some changes to it, and now I can get it to run on certain images, but not on all. When I change the images in my folder to a new set, I get the error:
Subscripted assignment dimension mismatch.
Error in luminance (line 29)
alc(i,1).rgb(:,:,:) = imread(alcJPG(i).name); %now in format [rows columns RGB=1,2,3]
I have included the relevant portion of the code below. I'm not sure why I am only getting this error on some images and not others. Thank you for the help.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%read in alc and nonalc images and convert to RBG with imread
display ('reading alc and nonalc images');
cd(alc_dir);
alcJPG = dir('*.JPG'); %alcohol images
for i = 1:length(alcJPG)
alc(i,1).rgb(:,:,:) = imread(alcJPG(i).name); %now in format [rows columns RGB=1,2,3]
alc(i).hsv = rgb2hsv(alc(i).rgb); %convert to HSV %%%(Hue, Saturation, Value)
alc(i).ycbcr = rgb2ycbcr(alc(i).rgb); %convert to YCbCr %%%(brightness, two color dif signals- Cb and Cr)
alc(i).lab = RGB2Lab(alc(i).rgb); %convert to Lab %%%(Lightness and color-opponent dimenstions-- a and b)
end
cd(nonalc_dir);
nonalcJPG = dir('*.JPG'); %nonalcohol images
for i = 1:length(nonalcJPG)
nonalc(i,1).rgb(:,:,:) = imread(nonalcJPG(i).name); %now in format [rows columns RGB=1,2,3]
nonalc(i).hsv = rgb2hsv(nonalc(i).rgb); %convert to HSV, V = 3 (value)
nonalc(i).ycbcr = rgb2ycbcr(nonalc(i).rgb); %convert to YCbCr, Y = 1 (luminance)
nonalc(i).lab = RGB2Lab(nonalc(i).rgb); %convert to Lab
end
cd(p);
display ('calculating RGB means');
for i = 1:length(nonalc)
%RGB means
alc(i).meanRintensity = mean(mean(alc(i).rgb(:,:,1),2),1);
alc_meanR(i) = alc(i).meanRintensity;
alc(i).meanGintensity = mean(mean(alc(i).rgb(:,:,2),2),1);
alc_meanG(i) = alc(i).meanGintensity;
alc(i).meanBintensity = mean(mean(alc(i).rgb(:,:,3),2),1);
alc_meanB(i) = alc(i).meanBintensity;
nonalc(i).meanRintensity = mean(mean(nonalc(i).rgb(:,:,1),2),1);
nonalc_meanR(i) = nonalc(i).meanRintensity;
nonalc(i).meanGintensity = mean(mean(nonalc(i).rgb(:,:,2),2),1);
nonalc_meanG(i) = nonalc(i).meanGintensity;
nonalc(i).meanBintensity = mean(mean(nonalc(i).rgb(:,:,3),2),1);
nonalc_meanB(i) = nonalc(i).meanBintensity;
%HSV mean (only store V)
alc(i).meanVintensity = mean(mean(alc(i).hsv(:,:,3),2),1);
alc_meanV(i) = alc(i).meanVintensity;
nonalc(i).meanVintensity = mean(mean(nonalc(i).hsv(:,:,3),2),1);
nonalc_meanV(i) = nonalc(i).meanVintensity;
%YCbCr mean (only store Y)
alc(i).meanYintensity = mean(mean(alc(i).ycbcr(:,:,1),2),1);
alc_meanY(i) = alc(i).meanYintensity;
nonalc(i).meanYintensity = mean(mean(nonalc(i).ycbcr(:,:,1),2),1);
nonalc_meanY(i) = nonalc(i).meanYintensity;
%Lab mean (only store L)
alc(i).meanLintensity = mean(mean(alc(i).lab(:,:,1),2),1);
alc_meanL(i) = alc(i).meanLintensity;
nonalc(i).meanLintensity = mean(mean(nonalc(i).lab(:,:,1),2),1);
nonalc_meanL(i) = nonalc(i).meanLintensity;
end
%to apply SHINE to the image need to replace V with Y to change to HSL -
%but SHINE is only useful if we want to match the luminance in the images,
%not just report it
%therefore, let's create a text output with all of these values in order to
%input to spss
out = fopen([export_dir 'luminance_test_4.25a.txt'], 'wt');
fprintf(out,'img, group, meanR, meanG, meanB, meanV, meanY, meanL\n');
for i = 1:length(alcJPG) %%this should be the # of jpgs in folder
display ('alc out');
if length(alcJPG(i).name(1:4)) == 4 %%AT added: if first 4 letters = 4 letters (always will) then....
%if length(alcJPG(i).name) == 6; %%original was basing if on length of
%filename... changed it to work around this
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(:),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i)); %%.name(ratio) here is printing to output the first 2,3,4 letters...WHY?
%%.name(:) prints whole filename
elseif length(alcJPG(i).name) == 7
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(1:3),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i));
elseif length(alcJPG(i).name) == 8
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(1:4),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i));
end
end
for i = 1:length(nonalcJPG)
display ('nonalc out');
if length(nonalcJPG(i).name) == 6
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(1:2),nonalc_mefclose(out),anR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
elseif length(nonalcJPG(i).name(1:7)) == 7 %%changed ==7 rather than 6 like alc because 6 here has a weird line (mefclose(out))
%elseif length(nonalcJPG(i).name) == 7
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(:),nonalc_meanR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
elseif length(nonalcJPG(i).name) == 8
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(1:4),nonalc_meanR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
end
end
3 comentarios
Walter Roberson
el 25 de Abr. de 2018
Code to check consistency of image sizes: https://www.mathworks.com/matlabcentral/answers/385472-error-in-image-size#answer_307796
Holly Pothier
el 25 de Abr. de 2018
Walter Roberson
el 25 de Abr. de 2018
I posted a follow-up there describing what would need to be done for earlier versions.
Respuestas (1)
Holly Pothier
el 25 de Abr. de 2018
0 votos
15 comentarios
Walter Roberson
el 25 de Abr. de 2018
(The files did not get attached)
Holly Pothier
el 25 de Abr. de 2018
Walter Roberson
el 25 de Abr. de 2018
I guess I need some files that are relevant to alc_dir and nonalc_dir, and I need to know about the p that you cd() to.
By the way your code assumes that the file system is not case sensitive. On case sensitive file systems, JPG and jpg are different file extensions.
You use RGB2Lab . Is that intended to be the same as rgb2lab ?
I do not seem to encounter a problem with the two images you provide, after making the changes to your program to define alc_dir nonalc_dir p and export_dir .
Holly Pothier
el 25 de Abr. de 2018
Walter Roberson
el 25 de Abr. de 2018
It looks like RGB2Lab is https://www.mathworks.com/matlabcentral/fileexchange/24009-rgb2lab . The rgb2lab() function was added to MATLAB in the release after yours, R2014b.
Walter Roberson
el 25 de Abr. de 2018
I am not seeing any difficulty even testing in R2014a.
Before using this, RGB2Lab must be on your MATLAB path (not just in your current directory, because you cd around to other directories.)
The below is the script I am using:
alc_dir = '/Users/roberson/MATLAB/3/397/397354/alc_dir';
nonalc_dir = '/Users/roberson/MATLAB/3/397/397354/nonalc_dir';
p = '/Users/roberson/MATLAB/3/397/397354';
export_dir = '/Users/roberson/MATLAB/3/397/397354/out';
ext = '.jpg';
%read in alc and nonalc images and convert to RBG with imread
display ('reading alc and nonalc images');
cd(alc_dir);
alcJPG = dir(['*' ext]); %alcohol images
for i = 1:length(alcJPG)
alc(i,1).rgb(:,:,:) = imread(alcJPG(i).name); %now in format [rows columns RGB=1,2,3]
alc(i).hsv = rgb2hsv(alc(i).rgb); %convert to HSV %%%(Hue, Saturation, Value)
alc(i).ycbcr = rgb2ycbcr(alc(i).rgb); %convert to YCbCr %%%(brightness, two color dif signals- Cb and Cr)
alc(i).lab = RGB2Lab(alc(i).rgb); %convert to Lab %%%(Lightness and color-opponent dimenstions-- a and b)
end
cd(nonalc_dir);
nonalcJPG = dir(['*' ext]); %nonalcohol images
for i = 1:length(nonalcJPG)
nonalc(i,1).rgb(:,:,:) = imread(nonalcJPG(i).name); %now in format [rows columns RGB=1,2,3]
nonalc(i).hsv = rgb2hsv(nonalc(i).rgb); %convert to HSV, V = 3 (value)
nonalc(i).ycbcr = rgb2ycbcr(nonalc(i).rgb); %convert to YCbCr, Y = 1 (luminance)
nonalc(i).lab = RGB2Lab(nonalc(i).rgb); %convert to Lab
end
cd(p);
display ('calculating RGB means');
for i = 1:length(nonalc)
%RGB means
alc(i).meanRintensity = mean(mean(alc(i).rgb(:,:,1),2),1);
alc_meanR(i) = alc(i).meanRintensity;
alc(i).meanGintensity = mean(mean(alc(i).rgb(:,:,2),2),1);
alc_meanG(i) = alc(i).meanGintensity;
alc(i).meanBintensity = mean(mean(alc(i).rgb(:,:,3),2),1);
alc_meanB(i) = alc(i).meanBintensity;
nonalc(i).meanRintensity = mean(mean(nonalc(i).rgb(:,:,1),2),1);
nonalc_meanR(i) = nonalc(i).meanRintensity;
nonalc(i).meanGintensity = mean(mean(nonalc(i).rgb(:,:,2),2),1);
nonalc_meanG(i) = nonalc(i).meanGintensity;
nonalc(i).meanBintensity = mean(mean(nonalc(i).rgb(:,:,3),2),1);
nonalc_meanB(i) = nonalc(i).meanBintensity;
%HSV mean (only store V)
alc(i).meanVintensity = mean(mean(alc(i).hsv(:,:,3),2),1);
alc_meanV(i) = alc(i).meanVintensity;
nonalc(i).meanVintensity = mean(mean(nonalc(i).hsv(:,:,3),2),1);
nonalc_meanV(i) = nonalc(i).meanVintensity;
%YCbCr mean (only store Y)
alc(i).meanYintensity = mean(mean(alc(i).ycbcr(:,:,1),2),1);
alc_meanY(i) = alc(i).meanYintensity;
nonalc(i).meanYintensity = mean(mean(nonalc(i).ycbcr(:,:,1),2),1);
nonalc_meanY(i) = nonalc(i).meanYintensity;
%Lab mean (only store L)
alc(i).meanLintensity = mean(mean(alc(i).lab(:,:,1),2),1);
alc_meanL(i) = alc(i).meanLintensity;
nonalc(i).meanLintensity = mean(mean(nonalc(i).lab(:,:,1),2),1);
nonalc_meanL(i) = nonalc(i).meanLintensity;
end
%to apply SHINE to the image need to replace V with Y to change to HSL -
%but SHINE is only useful if we want to match the luminance in the images,
%not just report it
%therefore, let's create a text output with all of these values in order to
%input to spss
out = fopen(fullfile(export_dir, 'luminance_test_4.25a.txt'), 'wt');
fprintf(out,'img, group, meanR, meanG, meanB, meanV, meanY, meanL\n');
for i = 1:length(alcJPG) %%this should be the # of jpgs in folder
display ('alc out');
if length(alcJPG(i).name(1:4)) == 4 %%AT added: if first 4 letters = 4 letters (always will) then....
%if length(alcJPG(i).name) == 6; %%original was basing if on length of
%filename... changed it to work around this
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(:),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i)); %%.name(ratio) here is printing to output the first 2,3,4 letters...WHY?
%%.name(:) prints whole filename
elseif length(alcJPG(i).name) == 7
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(1:3),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i));
elseif length(alcJPG(i).name) == 8
fprintf(out,'%s, alc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', alcJPG(i).name(1:4),alc_meanR(i),alc_meanG(i), ...
alc_meanB(i),alc_meanV(i),alc_meanY(i),alc_meanL(i));
end
end
for i = 1:length(nonalcJPG)
display ('nonalc out');
if length(nonalcJPG(i).name) == 6
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(1:2),nonalc_mefclose(out),anR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
elseif length(nonalcJPG(i).name(1:7)) == 7 %%changed ==7 rather than 6 like alc because 6 here has a weird line (mefclose(out))
%elseif length(nonalcJPG(i).name) == 7
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(:),nonalc_meanR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
elseif length(nonalcJPG(i).name) == 8
fprintf(out,'%s, nonalc, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f, %4.4f\n', nonalcJPG(i).name(1:4),nonalc_meanR(i),nonalc_meanG(i), ...
nonalc_meanB(i),nonalc_meanV(i),nonalc_meanY(i),nonalc_meanL(i));
end
end
Holly Pothier
el 25 de Abr. de 2018
Holly Pothier
el 25 de Abr. de 2018
Walter Roberson
el 25 de Abr. de 2018
I am not seeing any problem:
>> nonalc
nonalc =
3×1 struct array with fields:
rgb
hsv
ycbcr
lab
meanRintensity
meanGintensity
meanBintensity
meanVintensity
meanYintensity
meanLintensity
>> alc
alc =
3×1 struct array with fields:
rgb
hsv
ycbcr
lab
meanRintensity
meanGintensity
meanBintensity
meanVintensity
meanYintensity
meanLintensity
Walter Roberson
el 25 de Abr. de 2018
You mention having the SHINE toolbox on your path; there is a chance that is interfering. I will pull it down from http://www.mapageweb.umontreal.ca/gosselif/SHINE/ and see what happens.
Walter Roberson
el 25 de Abr. de 2018
No, that made no difference at all, it still works for me.
Holly Pothier
el 25 de Abr. de 2018
Walter Roberson
el 25 de Abr. de 2018
I suggest you
clearvars
and try again.
Holly Pothier
el 25 de Abr. de 2018
Holly Pothier
el 25 de Abr. de 2018
Categorías
Más información sobre Convert Image Type en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!