How to create image matrix via

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?

 Réponse acceptée

Image Analyst
Image Analyst le 18 Août 2011

0 votes

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);

Plus de réponses (7)

Andrei Bobrov
Andrei Bobrov le 18 Août 2011

0 votes

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
Vincenzo
Vincenzo le 18 Août 2011

0 votes

Sorry can you help me step by step because i'm a neophyte...
This is the M-code:
function createrectangle3(figure1)
%CREATERECTANGLE3(FIGURE1)
% FIGURE1: annotation figure
% Auto-generated by MATLAB on 18-Aug-2011 11:55:47
% Create rectangle
annotation(figure1,'rectangle','FaceColor','flat',...
'Position',[0.4206 0.5929 0.1365 0.1429]);
%end of M-code
The name of original matrix is Seq009...Now i have to put what you wrote in m-file or in command window? like this?:
function createrectangle3(figure1)
%CREATERECTANGLE3(FIGURE1)
% FIGURE1: annotation figure
% Auto-generated by MATLAB on 18-Aug-2011 11:55:47
% Create rectangle
annotation(figure1,'rectangle','FaceColor','flat',...
'Position',[0.4206 0.5929 0.1365 0.1429]);
Seq009 = randi([-123 421],10) % your matrix
xy = [0.4206 0.5929 0.1365 0.1429] % coordinates of your rectangle in view [x1,x2;y1,y2]
is it right or not?
Vincenzo
Vincenzo le 18 Août 2011

0 votes

@Image Analyst
A submatrix appears but just a 56x2..it's strange isn't it?
Summarize what I did:
In command window->
[rows columns numberOfColorChannels] = size(Seq009);
%Seq009 is the matrix 240x320
% the position of rectangle is [0.4206 0.5929 0.1365 0.1429]
x1=0.4206; x2=0.5929; y1=0.1365; y2=0.1429;
row1= x1*columns; row2= x2*columns; column1=y1*rows; column2=y2*rows;
submatrix=Seq009(row1:row2 , column1:column2);
And after that appears :
"Warning: Integer operands are required for colon operator when used as index"
and if i go imagesc(submatrix) appears a lot of multicolor rows.
What did I do wrong??

1 commentaire

Image Analyst
Image Analyst le 20 Août 2011
Your row1, etc. are fractional numbers. You need to use round(), floor(), ceil(), or int32() to get them into integers.

Connectez-vous pour commenter.

Vincenzo
Vincenzo le 7 Sep 2011

0 votes

Hi, first of all thanks for your help...I read your last answer.."http://www.mathworks.com/matlabcentral/answers/13951-how-to-create-image-matrix-via" now if i use rand(), floor(), ceil(), or int32() they approximate the value to 0 or 1 (cause i have value x1=0.386 x2=0.5571 y1=0.1436 y2=0.1143) and when i do a submatrix, an error message appears:"Subscript indices must either be real positive integers or logicals."(if i use int32) or "Index exceeds matrix dimensions"(with ceil) ...what i did:
x1=0.3867;
x2=0.5571;
y1=0.1436;
y2=0.1143;
x3=int32(x1); %appears x3 with value 0
x4=int32(x2); % " x4 " " 1
y3=int32(y1); % " y4 " " 0
y4=int32(y2); % " x4 " " 1
[rows columns numberOfColorChannels] size(Seq009); %appears rows with value 240 and columns with value 320
row1=x3*320;
row2=x4*320;
column1=y3*240;
column2=y4*240;
submatrix=Seq009(row1:row2,column1:column2);
And the error message appears
Now what's the problem :( ??

2 commentaires

Walter Roberson
Walter Roberson le 7 Sep 2011
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.
Vincenzo
Vincenzo le 8 Sep 2011
Thank you Walter...a submatix appears with no error but it's a 61X2 i think it's a little small or not?
I was thinking of getting a 60X70 or something like that...

Connectez-vous pour commenter.

Vincenzo
Vincenzo le 9 Sep 2011

0 votes

In your view it's right a submatrix 61x2?

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by