MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Tomas on 23 Nov 2012

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

*No products are associated with this question.*

Answer by Matt Fig on 23 Nov 2012

Edited by Matt Fig on 23 Nov 2012

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....

Show 3 older comments

Tomas on 23 Nov 2012

rgb=imread('image.jpg');

>> whos p C ones p C Name Size Bytes Class Attributes

C 1x1 8 double p 1x1 8 double

p =

400

C =

1800

Matt Fig on 23 Nov 2012

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 on 24 Nov 2012

Tomas, what are the values of p and C ? And is the error still occurring?

Answer by Walter Roberson on 24 Nov 2012

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.

Matt Fig on 24 Nov 2012

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

Answer by Image Analyst on 24 Nov 2012

Edited by Image Analyst on 24 Nov 2012

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.

## 0 Comments