Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine matrix boundary without loops

Tags:

matrix

matlab

I have got a 2D matrix. There is some region in the matrix where the elements are non-zero, in particular everywhere around the edge they are zero.

I plot the matrix using image as a colorplot and would like to add the curve that shows the boundary between non-zero values to zero values in the matrix. Is there any neat way to do this without loops?

like image 985
Wolpertinger Avatar asked Sep 11 '25 21:09

Wolpertinger


1 Answers

This looks like a job for convhull :

To illustrate this code i'll take a dummy example :

A=zeros(10);
B=binornd(1,0.5,8,8);
A(2:end-1,2:end-1)=B

A =

 0     0     0     0     0     0     0     0     0     0
 0     0     0     1     0     0     0     0     0     0
 0     0     1     1     1     1     1     1     0     0
 0     0     1     1     0     0     0     0     1     0
 0     0     0     1     0     0     0     1     0     0
 0     1     0     0     0     0     0     1     0     0
 0     0     0     1     1     1     1     1     1     0
 0     0     1     0     1     1     1     1     0     0
 0     1     0     1     1     1     1     0     1     0
 0     0     0     0     0     0     0     0     0     0

1/ Find the locations of all non zero entries :

[row,col]=find(A);

2/ Take the convex hull of these locations

k=convhull(row,col);

3/ Plot the convex hull (I plot the non zero points aswell but in your problem it will be your image points)

plot(row(k),col(k),'r-',row,col,'b*')

Result :

enter image description here

like image 96
BillBokeey Avatar answered Sep 14 '25 01:09

BillBokeey