"Subscripted assignment dimension mismatch" Error

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

Holly Pothier
Holly Pothier el 25 de Abr. de 2018
Unfortunately I only have access to 2014a. I will try to find a more recent version and run this, thank you.
I posted a follow-up there describing what would need to be done for earlier versions.

Iniciar sesión para comentar.

Respuestas (1)

Holly Pothier
Holly Pothier el 25 de Abr. de 2018

0 votos

Here are some examples of the filenames that work, and do not work:
This type of filename works: 1P_1.jpg, but when I use their resized versions with the filename: 1P_1_RS.jpg I get the error above. This occurs with our two image types/folders.

15 comentarios

(The files did not get attached)
Holly Pothier
Holly Pothier el 25 de Abr. de 2018
Here are the .jpg files. The difference is that the _RS file has been resized.
I tried putting the resized files in the folder and changing the naming structure so that it is the same as the originals hoping that it was a naming issue. However, this still gives the same error. Which makes me think it is not a naming issue, and leaves me more confused.
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 .
Here are some files relevant to alc_dir and nonalc_dir. The files that start with 1P are from alc_dir, and the files that start with 2P are from nonalc_dir. The attached files are the resized versions that are causing the error. They are all the same size, and have been resized in paint (maybe the issue?).
What changes did you make to the dir and p to get rid of the error?
And as far as case sensitivity, I do not know why RGB2Lab is used, this code was handed to me recently and was made years ago.
Here is the code for p and cd:
p = pwd;
alc_dir = 'R:\Pennington\Projects\Active Projects\VA CDA Pennington\MRI\Image Sets\MATLAB Image Matching\Alc';
nonalc_dir = 'R:\Pennington\Projects\Active Projects\VA CDA Pennington\MRI\Image Sets\MATLAB Image Matching\Non_Alc';
export_dir = 'R:\Pennington\Projects\Active Projects\VA CDA Pennington\MRI\Image Sets\MATLAB Image Matching\Alc';
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.
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
Even when I copy/paste the code you are using (but different dir) I get the same error. The SHINE toolbox, and RGB2lab are in my matlab path (as seen with pathtool). Since the code works with some images (the originals) I am confused why it won't work with the new resized images.
When you run it, are you able to get rgb lab and information for the _RS pictures I attached in my previous post? When I run this code successfully (on the non-resized images) I am given these values for alcJPG and nonalcJPG:
18x1 struct array with fields:
rgb
hsv
ycbcr
lab
meanRintensity
meanGintensity
meanBintensity
meanVintensity
meanYintensity
meanLintensity
Holly Pothier
Holly Pothier el 25 de Abr. de 2018
Sorry, I mean for alc and nonalc, not alcJPG and nonalcJPG
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
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.
No, that made no difference at all, it still works for me.
Is there a reason why when I type : whos alc, I always get 18x1 struct array, compared to: whos alcJPG, I get 8x1.
I also ran iminfo for the resized and original images and they are basically the same besides height and width. Is there something hidden in the resized images that are having a hard time with the line
alc(i,1).rgb(:,:,:) = imread(alcJPG(i).name);
I am not sure why/how it is working for you and not for me...unless I am missing something big. I don't think it is my path because the originals work and output fine.
I suggest you
clearvars
and try again.
Holly Pothier
Holly Pothier el 25 de Abr. de 2018
...that fixed the problem / error. Thank you for your help and all the suggestions.
I will try with all my stimuli and report back.
Thanks again.
Holly Pothier
Holly Pothier el 25 de Abr. de 2018
It works with all my stimuli now. Thank you.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Abr. de 2018

Comentada:

el 25 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by