Obtain Submatrix

7 views (last 30 days)
Vincenzo
Vincenzo on 13 Jan 2012
Hi, need absolutely your help as soon as possible.
I selected a rectangle from an image(that contains values), than trough show m code i obtained the position of rect: [x1 y1 width height]...than i got:
x2=x1+width;
y2=y1+height; %(x are columns and y are rows)
[rows columns numberOfColorChannels]=size(NameMatrix);
column1=int32(x1*columns);
column2=int32(x2*columns);
row1=int32(y1*rows);
row2=int32(y2*rows);
submatrix=NameMatrix(row1:row2,column1:column2);
I thought that was the solution but if i do imagesc(submatrix) it appears another part of image and not the area i wanted...why?Is it right or miss something?
  1 Comment
Walter Roberson
Walter Roberson on 14 Jan 2012
I really have no idea why you are multiplying x1 by columns ?? What is the mechanism you used to obtain the rectangle position, which call?

Sign in to comment.

Answers (7)

Vincenzo
Vincenzo on 14 Jan 2012
I multiply for the columns and rows because they give me the number of original matrix just to cover the area i chose...and someone told me this..
In any case here there's the explanation of [left right width height]
what should i do?

Vincenzo
Vincenzo on 14 Jan 2012
Any idea??

Image Analyst
Image Analyst on 14 Jan 2012
Like Walter said, DON'T multiply x and y by rows or columns. "Someone" told you incorrectly to do that. That's nonsense unless x and y are in the range 0-1 instead of 1-rows and 1-columns, which I doubt. If you got x and y from imrect(), rbbox() or similar, they are already in units of rows and columns so just use them:
x2=x1+width;
y2=y1+height; %(x are columns and y are rows)
[rows columns numberOfColorChannels]=size(NameMatrix);
column1=int32(x1);
column2=int32(x2);
row1=int32(y1);
row2=int32(y2);
submatrix=NameMatrix(row1:row2,column1:column2);
You'll probably also want to write a function to clip x to between 1 and columns, and y to between 1 and rows since, depending on how you got those x and y it might be possible for them to be outside the range of your image.
  5 Comments
Walter Roberson
Walter Roberson on 14 Jan 2012
It was
http://www.mathworks.com/matlabcentral/answers/13951-how-to-create-image-matrix-via#anwer_19104
Image Analyst
Image Analyst on 14 Jan 2012
Ah, ok, so they *were* normalized values. So then I'm not sure why his code doesn't work. Probably has something to do with trying to do things from code, instead of interactively drawing a rectangle and having the figure generate m-code for him. Probably something about the units property of the figure. He needs to manually check what the values of row1, row2, column1, column2 are and see if they make sense. Turning "axis" on may help with that so he can see what the actual row and column numbers are.

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Jan 2012
I would be concerned about indexing going up versus indexing going down. Matrix indexing goes down (that is, the top left corner is said to be (1,1)), but it is common to flip matrices over as they are displayed so that the bottom left corner is (1,1) and y increases upwards.
You have to know which coordinate system the [x1 y1 width height] is using in order to be sure you get the right indices.
  1 Comment
Image Analyst
Image Analyst on 14 Jan 2012
Good point. imshow() has a "YData" property where I think you can reverse this, though I haven't experimented around with it. Usually if you call rbbox() or imrect() it seems to know the y direction and return the y coords correctly. I think there's a "ydir reverse" command that could also be worth looking into. But I'd just figure out how to get x and y in pixel coordinates correctly in the first place rather than normalized units.

Sign in to comment.


Vincenzo
Vincenzo on 15 Jan 2012
I did the follow thing:
insert rectangle->Show Property Editor->More Properties->Units->Pixels it change me automatically the position from 0.34.... to 100... but i can't put this numbers because are too high (Indiced Exceed Matrix dimension), so i right click in the area and chose "show m code"...it appears this...
so i put the values in x1,x2,width and height
PS. x2=x1+height and y2=y1+width;
and that's what appear after doing column1=int32(x1*columns) and so on...till sub=NAME(row1:row2,column1:column2):
As you can see there is only a part of rectangle ,not all..why?

Image Analyst
Image Analyst on 15 Jan 2012
Vincenzo: If you look at your first figure, you'll see that the units are already in pixels. They're not normalized in the range of 0 to 1. So you don't want to multiply by the number of rows or columns. I'm not sure why, after all the discussion from Walter and me, that you didn't just try what we said, which was to not multiply by the number of rows and columns. It would take just a few seconds to try it. You changed the units to pixels interactively but then you said you still did this: "column1=int32(x1*columns) " which is what we said NOT to do if x1 is already in pixels. Now, drawing rectangles gives you fractional units so if you see things like x1=0.34 then you need to clip x1,x2,y1,y2 to the appropriate range (1 to rows) or (1 to columns) because you can't have a fractional index into your image matrix.
P.S. x corresponds to the width, not height, and y corresponds to the height, not the other way around like you had in your PS.

Vincenzo
Vincenzo on 17 Jan 2012
Sorry but i don't understand what i have to do ...I tried a lot of ways but nothing...reassuming: after i reached the position [x1,y1,width,height] in pixels unit (always in 0.34,0.23,..), i did x2=x1+width; y2=y1+height;...now the column1,column2,row1 and row2? I don't know..please can you write this?
  2 Comments
Walter Roberson
Walter Roberson on 17 Jan 2012
My approach would be to stop trying to use the insert rectangle tool and instead switch to something like ginput()
Image Analyst
Image Analyst on 18 Jan 2012
Or rbbox().

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!