Can you help me to correct this error?

4 次查看(过去 30 天)
Tomas
Tomas 2012-11-23
Hi, can you help me to correct this error
[p C]=size(lab)
lchan=lab(:,:,1);
L=60;
Lr = L .* ones(p, C);
d
??? Error using ==> times
Matrix dimensions must agree.
Error in ==> lab at 60
Lr = L .* ones(p, C);
thanks

回答(3 个)

Matt Fig
Matt Fig 2012-11-23
编辑:Matt Fig 2012-11-23
What are p and C?
L = 60;
Lr = L .* ones(3, 5); % No error...(the .* is not necessary)
You have a variable named lab and a function named lab?? This is a very bad idea and may (somehow) be at the root of your problem....
  6 个评论
Matt Fig
Matt Fig 2012-11-23
There is more to the story then. See, this does not error on my machine:
p = 400;
C = 1800;
L = 60.0221;
Lr = L .* ones(p, C);
So I am afraid I am out of ideas here. There is something else going on in the code somewhere, but without seeing the code I cannot say. Someone else may be able to guess the problem.
Walter Roberson
Walter Roberson 2012-11-24
Tomas, what are the values of p and C ? And is the error still occurring?

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2012-11-24
Your RGB image is 3 dimensional, not 2 dimensional. Row, Column, Color-plane. Your lab equivalent of the RB is 3 dimensional as well. When you take the size() of a 3 dimensional array, you need to have either only 1 output or else 3 or more outputs. If you only have two outputs as in
[p C]=size(lab)
then p will be assigned the number of rows (as expected) but C will be assigned the product of all the remaining dimensions, and so will be columns times number of color planes.
  1 个评论
Matt Fig
Matt Fig 2012-11-24
编辑:Matt Fig 2012-11-24
That doesn't explain the error, however...
>> clear all
>> rgb=imread('office_2.jpg'); % Image in the IPT
>> cform = makecform('srgb2lab'); % On the FEX
>> lab = applycform(rgb,cform);
>> [p C]=size(lab);
>> % lchan=lab(:,:,1);
>> L=60.0221;
>> Lr = L .* ones(p, C); % No error....
>> whos
Name Size Bytes Class Attributes
C 1x1 8 double
L 1x1 8 double
Lr 600x2709 13003200 double
cform 1x1 13376 struct
lab 600x903x3 1625400 uint8
p 1x1 8 double
rgb 600x903x3 1625400 uint8

请先登录,再进行评论。


Image Analyst
Image Analyst 2012-11-24
编辑:Image Analyst 2012-11-24
Like Walter said, your problem is (partially) that your C is the number of columns TIMES the number of color channels (which is 3). You should be doing it this way, as you'll notice that I ALWAYS do it in code I post, even when I suspect the image is gray level:
[rows columns numberOfColorChannels] = size(lab);
If you do it this way:
[rows columns] = size(lab);
as you did, then columns will not be the number of columns in the image but will be columns * numberOfColorChannels. Please see this blog entry by Steve Eddins for further discussion: http://blogs.mathworks.com/steve/2011/03/29/even-more-information-about-the-size-function/
But like Matt said, that wouldn't cause an error if L was just some number like 60. So, I suspect that you actually did something like
L = lab(:, :, 1); % Extract the L color channel.
and then L would have the size rows*columns, but your ones() function would return an array rows*columns*numberOfColorChannels. Hence the sizes don't match up. So I don't know why you said L was 60 when it was not, but that's your problem. I speculate that you'd like to construct a constant luminance image. In that case, you set
Lr = 60 * ones(size(L));
and then you invert the transform to get back to RGB.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by