Obtain Submatrix
7 views (last 30 days)
Show older comments
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
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?
Answers (7)
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
on 14 Jan 2012
It was
http://www.mathworks.com/matlabcentral/answers/13951-how-to-create-image-matrix-via#anwer_19104
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.
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
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.
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.
0 Comments
Vincenzo
on 17 Jan 2012
2 Comments
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()
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!