From: dale on
Hi guys, I am trying to get starting and ending columns for 'number regions,. Below is my code for a matrix with "TWO NUMBER REGIONS" containing values 1 and 2.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;

Rs1 = [ 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 2 2 2 2 0 0;
0 0 0 0 0 0 2 2 2 2 2 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0]

Rs1n = 2
Rs2n = 6
[rs cs]= size(Rs1)
RV = zeros(1,Rs2n)

for i =1:Rs2n
RV= [rs, cs, 1, 1]
end

RV2= [rs, cs, 1, 1]
% Go through all values in Rs1 in parallel, updating extents. NOTE: The extents are RV.

for i = 1:rs
for j = 1:cs
if Rs1(i,j)>0
if RV(Rs1(i,j)) > i
RV(Rs1(i,j))=i
end
if RV2(Rs1(i,j)) > j
RV2(Rs1(i,j))=j
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


My result is as follows:

RV =

3 8 1 1


RV2 =

10 7 1 1



Now what I actuallly want is as follows:

RV =

3 8 6 9


RV2 =

10 7 11 11


So I want RV and RV2 to be in the form:

[ starting_row_for_region_with_1 , starting_row_for_region_with_2,
ending_row_for_region_with_1 , ending_row_for_region_with_2 ]

[ starting_col_for_region_with_1 , starting_col_for_region_with_2,
ending_col_for_region_with_1 , ending_col_for_region_with_2]


If you guys can also get my output into a single variable ,i.e, RV , instead of RV and RV2, that be a good help so I could cater when the matrix with number regions increase to say regions of 3 , 4 and so on.

Any help would be great.

Thanks
From: Ross on
"dale " <persad34(a)gmail.com> wrote in message <i2oqip$9cf$1(a)fred.mathworks.com>...
> Hi guys, I am trying to get starting and ending columns for 'number regions,. Below is my code for a matrix with "TWO NUMBER REGIONS" containing values 1 and 2.
>
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> clc;
> clear all;
>
> Rs1 = [ 0 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 1 0;
> 0 0 0 0 0 0 0 0 0 0 1 0;
> 0 0 0 0 0 0 0 0 0 1 1 0;
> 0 0 0 0 0 0 0 0 0 1 1 0;
> 0 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 2 2 2 2 0 0;
> 0 0 0 0 0 0 2 2 2 2 2 0;
> 0 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0 0]
>
> Rs1n = 2
> Rs2n = 6
> [rs cs]= size(Rs1)
> RV = zeros(1,Rs2n)
>
> for i =1:Rs2n
> RV= [rs, cs, 1, 1]
> end
>
> RV2= [rs, cs, 1, 1]
> % Go through all values in Rs1 in parallel, updating extents. NOTE: The extents are RV.
>
> for i = 1:rs
> for j = 1:cs
> if Rs1(i,j)>0
> if RV(Rs1(i,j)) > i
> RV(Rs1(i,j))=i
> end
> if RV2(Rs1(i,j)) > j
> RV2(Rs1(i,j))=j
> end
> end
> end
> end
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
>
> My result is as follows:
>
> RV =
>
> 3 8 1 1
>
>
> RV2 =
>
> 10 7 1 1
>
>
>
> Now what I actuallly want is as follows:
>
> RV =
>
> 3 8 6 9
>
>
> RV2 =
>
> 10 7 11 11
>
>
> So I want RV and RV2 to be in the form:
>
> [ starting_row_for_region_with_1 , starting_row_for_region_with_2,
> ending_row_for_region_with_1 , ending_row_for_region_with_2 ]
>
> [ starting_col_for_region_with_1 , starting_col_for_region_with_2,
> ending_col_for_region_with_1 , ending_col_for_region_with_2]
>
>
> If you guys can also get my output into a single variable ,i.e, RV , instead of RV and RV2, that be a good help so I could cater when the matrix with number regions increase to say regions of 3 , 4 and so on.
>
> Any help would be great.
>
> Thanks

I can see you have put some effort into your code, it has some good features, and it might be fixable, but I would use find to do this. (By the way, I did not understand why you set Rs2n = 6)

Anyway, here's something to start yourself with:

values=[1 2]; % these are the values you want to locate in Rs1

for k=1:numel(values)

[r,c]=find(Rs1==values(k)); % this returns lists of row & column indices

%write some code of your own here to get the min and max of r and c

%write some more code to put the min and max values in the right places in RV

end

Hope this helps you get going

Ross
From: dale on
Ross,

thanks for the reply.


is there any way I can get the values in the matrix,i.e. the value 1 and 2. without manually having to input it into the array.

ps: if you got any ideas how to get the end rows and cols in my code feel free to hit me with them. Suggestions from anyone else would be grateful.

thanks
dale
From: ImageAnalyst on
dale:
This is trivial if you have the image processing toolbox. Do you?
Just call bwlabel() on your array, and then call regionprops() and
look at the max and min values of the PixelList property of the
structure returned by regionprops().

From: dale on
Hi ImageAnalyst,

yes ,I have the Image toolbox. i applied the following:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc; clear all;


Rs1 = [ 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 2 2 2 2 0 0;
0 0 0 0 0 0 2 2 2 2 2 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0]


[L, num] = bwlabel(Rs1, 4)

STATS = regionprops(L, 'PixelValues' )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

However, I get an error with 'regionprops'.

Can you suggest something .

thanks for the help.
dale