Problem working with 3-D image matrix

5 次查看(过去 30 天)
I want to create an image is divided into 3 parts automatically with different levels of light, but this script does not work for an image that has a decimal value after a divided three-dimensional, why ?
this is my script.
g=input('input picture name(ex : xxxx.jpg or xxxx.gif"): ','s');
start=imread(g);
s=size(start);
z=s(1,1);
a=z/3;
y=ceil(a);
x=y+1;
b=y*2;
c=y*3;
start([1:y],:,:)=uint8(double(start([1:y],:,:))*4);
start([y:b],:,:)=uint8(double(start([y:b],:,:))*1);
start([b:c],:,:)=uint8(double(start([b:c],:,:))*0);
image(start);
error says
??? Index exceeds matrix dimensions.
Error in ==> kaka at 12
start([b:c],:,:)=uint8(double(start([b:c],:,:))*0);

采纳的回答

Brett Shoelson
Brett Shoelson 2011-3-4
I'm not sure what you mean by "does not work for an image that has a decimal value after a divided three-dimensional."
But I think that you're trying to do something like this:
start = imread(g);
base = ones(ceil(size(start,1)/3),size(start,2),size(start,3));
base = uint8(cat(1,base*4,base,base*0));
start = start.*base;
Cheers, Brett
  1 个评论
Mardhika Jenned
Mardhika Jenned 2011-3-4
please use the formula for a beginner like I use, because I do not understand the formula you provide. .
your formula is doesn't work for picture it has vertical size decimal,
example: vertical size is 101
100 /3 = 33.33333333 <<< the program is error
please help me !

请先登录,再进行评论。

更多回答(2 个)

Brett Shoelson
Brett Shoelson 2011-3-4
Well, if you want to chop your image into thirds and it doesn't divide equally into three bins, then YOU have to decide how to chop it. CEIL, ROUND, FLOOR, FIX...all convert fractions to whole numbers.
If you want to ensure that your matrix dimensions match, thry this:
start = imread(g);
base = ones(ceil(size(start,1)/3),size(start,2),size(start,3));
base = uint8(cat(1,base*4,base,base*0));
base = base(1:size(start,1),:,:);
start = start.*base;
So, I first built a matrix of ones 1/3 the size of start. Then I concatenated (CAT) three copies of it, once multiplied by 4, once multiplied by 1, and once multiplied by zero. That will either be exactly the same size as start, or 1 or 2 rows longer than start. So next, I "cropped" (base = base(1:size(start,1),:,:);) the result to be the same size as start, and then multiplied the two matrices element-by-element.
Cheers, Brett
  2 个评论
Mardhika Jenned
Mardhika Jenned 2011-3-4
can you use the matriks logic to solve this problem ? thx
Walter Roberson
Walter Roberson 2011-3-4
What do you include in "matrix logic" ?
If you mean something like matrix multiplication, then NO. Brett's code will expand the matrix by up to two rows, but matrix multiplication cannot change the number of rows in a matrix.

请先登录,再进行评论。


Brett Shoelson
Brett Shoelson 2011-3-5
Here's another approach, just for fun:
start = imread(g);
multCol = reshape(repmat([4 1 0],ceil(size(start,1)/3),1),[],1);
multCol = uint8(multCol(1:size(start,1),:));
start = bsxfun(@times,start,multCol);
Have a good weekend!
Brett
  1 个评论
Brett Shoelson
Brett Shoelson 2011-3-5
Or, more generally, change that second "multCol=" line to:
multCol = cast(multCol(1:size(start,1),:),class(start));
Walter's right, of course, that your matrix multiplications have to make mathematical sense, dimensionally speaking. For element-wise multiplications (.*), for instance, the matrices have to be the same size. But if the matrices you are attempting to multiply differ dimensionally in such a way that expanding singleton dimensions can make the matrices dimensionally consistent, the underused BSXFUN allows you to play fast and loose with your matrices! :)
Cheers,
Brett

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by