Problem working with 3-D image matrix
조회 수: 3 (최근 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);
댓글 수: 0
채택된 답변
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
추가 답변 (2개)
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
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
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
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!