How to create image matrix via
18 次查看(过去 30 天)
显示 更早的评论
I have the following problem: I have a file.mat, containing a matrix 240X320. If I click on imagesc, I see an image referencing the matrix ... Now, I want to select a portion of the image by clicking on "insert-> rectangle" and from there derive the submatrix. If I click with right mouse button I can show M-code, which gives me only the coordinates of the rectangle, but how can I obtain the submatrix?
0 个评论
采纳的回答
Image Analyst
2011-8-18
No, that's not right. You need to convert your x,y from normalized values to values that correspond to pixel coordinates. Get the size of your image like this
[rows columns numberOfColorChannels] = size(yourImageArray);
Then multiply your x's by the columns, and the y's by the rows to get x and y in columns and rows instead of normalized units. Then get the submatrix like this
submatrix = yourImageArray(row1:row2, column1:column2);
0 个评论
更多回答(7 个)
Andrei Bobrov
2011-8-18
M = randi([-123 421],10) % your matrix
xy = [2,7;4,8] % coordinates of your rectangle in view [x1,x2;y1,y2]
variant
c = arrayfun(@(i1)xy(i1,1):xy(i1,2),1:2,'un',0);
SM = M(c{:}) % your submatrix
or
SM = M(xy(1,1):xy(1,2),xy(2,1):xy(2,2)) % your submatrix
0 个评论
Vincenzo
2011-8-18
1 个评论
Image Analyst
2011-8-20
Your row1, etc. are fractional numbers. You need to use round(), floor(), ceil(), or int32() to get them into integers.
Walter Roberson
2011-8-18
This sounds oddly like yesterday's question http://www.mathworks.com/matlabcentral/answers/13866-move-rectangle-over-te-image
0 个评论
Vincenzo
2011-9-7
2 个评论
Walter Roberson
2011-9-7
x1=0.3867;
row1=int32(x1*320);
That is, do not convert the multiplication factor to integer: convert the result of the multiplication to integer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!