Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Image Neighbourhood

Subject: Image Neighbourhood

From: Ravi Shekhar

Date: 23 Mar, 2012 19:12:51

Message: 1 of 11

I have a binary image (say 256 X 256) and for given points (e.g. (5,10), (20,100), (20,200) etc),
I want to find 17 x 17 neighbourhood of all given points in different matrix.
I want to to it without for loop.

Please help.
 

Thanks & Regards,
 Ravi Shekhar

Subject: Image Neighbourhood

From: Matt J

Date: 23 Mar, 2012 19:33:32

Message: 2 of 11

Ravi Shekhar <ravishekhar.82@gmail.com> wrote in message <10038391.985.1332529971996.JavaMail.geo-discussion-forums@pbje9>...
> I have a binary image (say 256 X 256) and for given points (e.g. (5,10), (20,100), (20,200) etc),
> I want to find 17 x 17 neighbourhood of all given points in different matrix.
> I want to to it without for loop.
==============

Elaborate on why don't want to use a for loop. It doesn't sound like it would create any problems.

And how do you define the 17x17 neighborhood of (5,10) which is less than 17 away from the edge of the matrix.

Subject: Image Neighbourhood

From: Ravi Shekhar

Date: 23 Mar, 2012 19:51:28

Message: 3 of 11

I have to densely sample neighbourhood which will create
speed up issues with 'for' loop. I just to avoid it, if possible.

The 17x17 neighborhood of (5,10) will be defined by appending
zeros in the image.

Subject: Image Neighbourhood

From: Roger Stafford

Date: 23 Mar, 2012 20:54:11

Message: 4 of 11

Ravi Shekhar <ravishekhar.82@gmail.com> wrote in message <10038391.985.1332529971996.JavaMail.geo-discussion-forums@pbje9>...
> I have a binary image (say 256 X 256) and for given points (e.g. (5,10), (20,100), (20,200) etc),
> I want to find 17 x 17 neighbourhood of all given points in different matrix.
> I want to to it without for loop.
- - - - - - - - -
  I think Matt was right to question your need to avoid for-loops entirely. To do so is cumbersome and would involve transformations back and forth between subscript indices and linear indices. The resulting program would probably execute more slowly than with a single for-loop.

  Try this single for-loop. Let 'image' be an m-by-n array of pixels and let 'points' be a p-by-2 array of indices into 'image'.

 bigimage = [zeros(8,n+16);zeros(m,8),image,zeros(m,8);zeros(8,n+16)];
 neighimage = zeros(17,17,p);
 for k = 1:p
  meighimage(:,:,k) = ...
    bigimage(points(k,1):points(k,1)+16,points(k,2):points(k,2)+16);
 end

Roger Stafford

Subject: Image Neighbourhood

From: Roger Stafford

Date: 24 Mar, 2012 03:38:29

Message: 5 of 11

"Roger Stafford" wrote in message <jkintj$apr$1@newscl01ah.mathworks.com>...
> meighimage(:,:,k) = ...
- - - - - - - -
  Oops, I see a typo! The fourth line should be:

  neighimage(:,:,k) = ...

Roger Stafford

Subject: Image Neighbourhood

From: Matt J

Date: 24 Mar, 2012 16:37:23

Message: 6 of 11

Ravi Shekhar <ravishekhar.82@gmail.com> wrote in message <15950143.424.1332532288863.JavaMail.geo-discussion-forums@pbgr6>...
> I have to densely sample neighbourhood which will create
> speed up issues with 'for' loop. I just to avoid it, if possible.
>
> The 17x17 neighborhood of (5,10) will be defined by appending
> zeros in the image.
=============

Here's a version of Roger's code without for-loops, but again, it's anybody's guess if this will outperform a loop. I've assumed that the image is already pre-padded with zeros, as you've said you will do,

 d=17;
 [M,N]=size(bigimage);
 E=reshape(1:M*N,[M,N]);
 E=E(1:d,1:d);
 E=E-E((d^2+1)/2);
 
 
idx=sub2ind([M,N], points(:,1), points(:,2));
idx=bsxfun(@plus,E(:),idx(:).');

neighimage=reshape(bigimage(idx),d,d,[])

Subject: Image Neighbourhood

From: ImageAnalyst

Date: 26 Mar, 2012 00:59:02

Message: 7 of 11

On Mar 24, 12:37 pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> Here's a version of Roger's code without for-loops, but again, it's anybody's guess if this will outperform a loop. I've assumed that the image is already pre-padded with zeros, as you've said you will do,
>
>  d=17;
>  [M,N]=size(bigimage);
>  E=reshape(1:M*N,[M,N]);
>  E=E(1:d,1:d);
>  E=E-E((d^2+1)/2);
>
> idx=sub2ind([M,N], points(:,1), points(:,2));
> idx=bsxfun(@plus,E(:),idx(:).');
>
> neighimage=reshape(bigimage(idx),d,d,[])

-------------------------------------------------------------------------------------
Why not simply do
subImage17x17 = fullImage(row-8:row+8, col-8:col+8);
That will do the job assuming the neighborhood is a square window,
which is a very common thing to do.

Subject: Image Neighbourhood

From: Matt J

Date: 26 Mar, 2012 12:52:10

Message: 8 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <8e54b0d9-1462-4547-9713-b73f816cf298@z3g2000pbn.googlegroups.com>...
> On Mar 24, 12:37 pm, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> > Here's a version of Roger's code without for-loops, but again, it's anybody's guess if this will outperform a loop. I've assumed that the image is already pre-padded with zeros, as you've said you will do,
> >
> >  d=17;
> >  [M,N]=size(bigimage);
> >  E=reshape(1:M*N,[M,N]);
> >  E=E(1:d,1:d);
> >  E=E-E((d^2+1)/2);
> >
> > idx=sub2ind([M,N], points(:,1), points(:,2));
> > idx=bsxfun(@plus,E(:),idx(:).');
> >
> > neighimage=reshape(bigimage(idx),d,d,[])
>
> -------------------------------------------------------------------------------------
> Why not simply do
> subImage17x17 = fullImage(row-8:row+8, col-8:col+8);
> That will do the job assuming the neighborhood is a square window,
> which is a very common thing to do.
=================


I'm not sure I follow. The OP is trying to extract multiple 17x17 neighborhoods automatically. Your code seems to extract only one.

Subject: Image Neighbourhood

From: ImageAnalyst

Date: 26 Mar, 2012 14:50:25

Message: 9 of 11

On Mar 26, 8:52 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> I'm not sure I follow. The OP is trying to extract multiple 17x17 neighborhoods automatically. Your code seems to extract only one.
---------------------------------------------------------------------
Yes, it extracts only one. As I understood it, he does only want
one. He said "for given points (e.g. (5,10), (20,100), (20,200) etc),
I want to find 17 x 17 neighbourhood"

So for his example he would get one 17x17 neighborhood with the upper
left corner (row1, column1) being (5,10), and he'd get the second
17x17 neighborhood with the upper left corner being (20,100), and
another one with the upper left corner being (20,200). He didn't want
one located at every single pixel of the millions of pixels in the
entire image, just those few "given points" that he specified. My
code extracts multiple neighborhoods only if you call it multiple
times, once for each of the few "given points." At least that's how I
understood it. Ravi please correct me if I'm wrong.

Subject: Image Neighbourhood

From: Roger Stafford

Date: 26 Mar, 2012 15:46:12

Message: 10 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c5e656f8-77cb-4d65-b578-e99a20fcfd56@px4g2000pbc.googlegroups.com>...
> Yes, it extracts only one. As I understood it, he does only want
> one. He said "for given points (e.g. (5,10), (20,100), (20,200) etc),
> I want to find 17 x 17 neighbourhood"
>
> So for his example he would get one 17x17 neighborhood with the upper
> left corner (row1, column1) being (5,10), and he'd get the second
> 17x17 neighborhood with the upper left corner being (20,100), and
> another one with the upper left corner being (20,200). He didn't want
> one located at every single pixel of the millions of pixels in the
> entire image, just those few "given points" that he specified. My
> code extracts multiple neighborhoods only if you call it multiple
> times, once for each of the few "given points." At least that's how I
> understood it. Ravi please correct me if I'm wrong.
- - - - - - -
  ImageAnalyst, I don't see what the fuss is about on this problem. Both Matt and I earlier in this thread expressed doubt that Ravi's wish "to to it without for loop" would yield a faster method. I wrote a for-loop solution which is very similar to the one you suggested more recently (except that yours is for a single point only.) Matt explored the possibility of a vectorized solution to see how it would look and made a good job of it in my opinion. Neither my solution nor Matt's was intended to handle "every single pixel of the millions of pixels in the entire image", but only those specified in an array which we called 'points'. All that is needed now is to try these solutions out under realistic circumstances to see which is the most efficient.

Roger Stafford

Subject: Image Neighbourhood

From: ImageAnalyst

Date: 26 Mar, 2012 18:11:50

Message: 11 of 11

Sorry about that. I didn't notice the points array. Sometimes if
things get too compact they get hard for me to follow (I always have
to think about bsxfun). If you define points first, then you will get
a 3D array out with the number of planes being exactly just the number
of points you assigned and each plane being a 17x17 array around that
point, so Ravi should be able to deal with that with no problem.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us