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:
Combining different indexing methods

Subject: Combining different indexing methods

From: Mirko

Date: 22 Jun, 2010 09:48:04

Message: 1 of 7

Hi, I made some search attempts for this but was not successful.

I work a lot with colour images (M x N x 3) and often would like to access, say, the red channel (M x N) of an image and use 2D logical indexing within that channel.

The only ways I know to do this is either to create a new variable just for the red channel and then index into that or to extend the dimensions of the indexing matrix to M x N x 3.

Is there a way to do this indexing directly. Intuitively I would write with the image IMG and the logical indexing matrix L:

IMG(:,:,1)(L)

which doesn't work but hopefully explains what I'd like to do.

Many thanks!

Subject: Combining different indexing methods

From: Jos (10584)

Date: 22 Jun, 2010 11:08:06

Message: 2 of 7

"Mirko " <arnoldma@tcd.ie> wrote in message <hvq0sk$gkr$1@fred.mathworks.com>...
> Hi, I made some search attempts for this but was not successful.
>
> I work a lot with colour images (M x N x 3) and often would like to access, say, the red channel (M x N) of an image and use 2D logical indexing within that channel.
>
> The only ways I know to do this is either to create a new variable just for the red channel and then index into that or to extend the dimensions of the indexing matrix to M x N x 3.
>
> Is there a way to do this indexing directly. Intuitively I would write with the image IMG and the logical indexing matrix L:
>
> IMG(:,:,1)(L)
>
> which doesn't work but hopefully explains what I'd like to do.
>
> Many thanks!

There are many ways to write this in one line:

M = IMG(cat(3,L,false([size(L) 2])))

however, two lines is easy and fast enough, and imho recommended here since it is easy to read and maintain

R = IMG(:,:,1)
M = R(L)

hth
Jos

Subject: Combining different indexing methods

From: Mirko

Date: 22 Jun, 2010 11:28:21

Message: 3 of 7

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hvq5im$5bm$1@fred.mathworks.com>...
> "Mirko " <arnoldma@tcd.ie> wrote in message <hvq0sk$gkr$1@fred.mathworks.com>...
> > Hi, I made some search attempts for this but was not successful.
> >
> > I work a lot with colour images (M x N x 3) and often would like to access, say, the red channel (M x N) of an image and use 2D logical indexing within that channel.
> >
> > The only ways I know to do this is either to create a new variable just for the red channel and then index into that or to extend the dimensions of the indexing matrix to M x N x 3.
> >
> > Is there a way to do this indexing directly. Intuitively I would write with the image IMG and the logical indexing matrix L:
> >
> > IMG(:,:,1)(L)
> >
> > which doesn't work but hopefully explains what I'd like to do.
> >
> > Many thanks!
>
> There are many ways to write this in one line:
>
> M = IMG(cat(3,L,false([size(L) 2])))
>
> however, two lines is easy and fast enough, and imho recommended here since it is easy to read and maintain
>
> R = IMG(:,:,1)
> M = R(L)
>
> hth
> Jos

Thanks very much, Jos.
I was just hoping there was a way around this using some special syntax as this seems somehow not optimal.

Subject: Combining different indexing methods

From: Jos (10584)

Date: 22 Jun, 2010 13:11:21

Message: 4 of 7

"Mirko " <arnoldma@tcd.ie> wrote in message <hvq6ol$hcf$1@fred.mathworks.com>...
> "Jos (10584) " <#10584@fileexchange.com> wrote in message <hvq5im$5bm$1@fred.mathworks.com>...
> > "Mirko " <arnoldma@tcd.ie> wrote in message <hvq0sk$gkr$1@fred.mathworks.com>...
> > > Hi, I made some search attempts for this but was not successful.
> > >
> > > I work a lot with colour images (M x N x 3) and often would like to access, say, the red channel (M x N) of an image and use 2D logical indexing within that channel.
> > >
> > > The only ways I know to do this is either to create a new variable just for the red channel and then index into that or to extend the dimensions of the indexing matrix to M x N x 3.
> > >
> > > Is there a way to do this indexing directly. Intuitively I would write with the image IMG and the logical indexing matrix L:
> > >
> > > IMG(:,:,1)(L)
> > >
> > > which doesn't work but hopefully explains what I'd like to do.
> > >
> > > Many thanks!
> >
> > There are many ways to write this in one line:
> >
> > M = IMG(cat(3,L,false([size(L) 2])))
> >
> > however, two lines is easy and fast enough, and imho recommended here since it is easy to read and maintain
> >
> > R = IMG(:,:,1)
> > M = R(L)
> >
> > hth
> > Jos
>
> Thanks very much, Jos.
> I was just hoping there was a way around this using some special syntax as this seems somehow not optimal.

Another option is to reshape your image data once(!).

% data
  img_original = rand(3,4,3) ;
  L = rand(3,4) > 0.5
  channel = 1

% reshape - each plane becomes a column
  img_work = reshape(img_original,[],3) ; % only once!
  M = img_work(L, channel)

% check
  checkM = img_original(:,:,channel) ;
  checkM = checkM(L)
  isequal(M, checkM)

hth
Jos

Subject: Combining different indexing methods

From: Mirko

Date: 22 Jun, 2010 13:43:22

Message: 5 of 7

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hvqcpp$86s$1@fred.mathworks.com>...
> "Mirko " <arnoldma@tcd.ie> wrote in message <hvq6ol$hcf$1@fred.mathworks.com>...
> > "Jos (10584) " <#10584@fileexchange.com> wrote in message <hvq5im$5bm$1@fred.mathworks.com>...
> > > "Mirko " <arnoldma@tcd.ie> wrote in message <hvq0sk$gkr$1@fred.mathworks.com>...
> > > > Hi, I made some search attempts for this but was not successful.
> > > >
> > > > I work a lot with colour images (M x N x 3) and often would like to access, say, the red channel (M x N) of an image and use 2D logical indexing within that channel.
> > > >
> > > > The only ways I know to do this is either to create a new variable just for the red channel and then index into that or to extend the dimensions of the indexing matrix to M x N x 3.
> > > >
> > > > Is there a way to do this indexing directly. Intuitively I would write with the image IMG and the logical indexing matrix L:
> > > >
> > > > IMG(:,:,1)(L)
> > > >
> > > > which doesn't work but hopefully explains what I'd like to do.
> > > >
> > > > Many thanks!
> > >
> > > There are many ways to write this in one line:
> > >
> > > M = IMG(cat(3,L,false([size(L) 2])))
> > >
> > > however, two lines is easy and fast enough, and imho recommended here since it is easy to read and maintain
> > >
> > > R = IMG(:,:,1)
> > > M = R(L)
> > >
> > > hth
> > > Jos
> >
> > Thanks very much, Jos.
> > I was just hoping there was a way around this using some special syntax as this seems somehow not optimal.
>
> Another option is to reshape your image data once(!).
>
> % data
> img_original = rand(3,4,3) ;
> L = rand(3,4) > 0.5
> channel = 1
>
> % reshape - each plane becomes a column
> img_work = reshape(img_original,[],3) ; % only once!
> M = img_work(L, channel)
>
> % check
> checkM = img_original(:,:,channel) ;
> checkM = checkM(L)
> isequal(M, checkM)
>
> hth
> Jos

This is really nice! I didn't know this was possible.

Many Thanks!

Subject: Combining different indexing methods

From: Loren_Shure

Date: 22 Jun, 2010 14:02:32

Message: 6 of 7


"Mirko " <arnoldma@tcd.ie> wrote in message
news:hvq0sk$gkr$1@fred.mathworks.com...
> Hi, I made some search attempts for this but was not successful.
>
> I work a lot with colour images (M x N x 3) and often would like to
> access, say, the red channel (M x N) of an image and use 2D logical
> indexing within that channel.
>
> The only ways I know to do this is either to create a new variable just
> for the red channel and then index into that or to extend the dimensions
> of the indexing matrix to M x N x 3.
>
> Is there a way to do this indexing directly. Intuitively I would write
> with the image IMG and the logical indexing matrix L:
>
> IMG(:,:,1)(L)
>
> which doesn't work but hopefully explains what I'd like to do.
>
> Many thanks!

How about

IMG(L(1),L(2),1)

--Loren

Subject: Combining different indexing methods

From: Mirko

Date: 22 Jun, 2010 14:38:09

Message: 7 of 7

"Loren_Shure" <loren.shure@mathworks.com> wrote in message <hvqfps$lfv$1@fred.mathworks.com>...
>
> "Mirko " <arnoldma@tcd.ie> wrote in message
> news:hvq0sk$gkr$1@fred.mathworks.com...
> > Hi, I made some search attempts for this but was not successful.
> >
> > I work a lot with colour images (M x N x 3) and often would like to
> > access, say, the red channel (M x N) of an image and use 2D logical
> > indexing within that channel.
> >
> > The only ways I know to do this is either to create a new variable just
> > for the red channel and then index into that or to extend the dimensions
> > of the indexing matrix to M x N x 3.
> >
> > Is there a way to do this indexing directly. Intuitively I would write
> > with the image IMG and the logical indexing matrix L:
> >
> > IMG(:,:,1)(L)
> >
> > which doesn't work but hopefully explains what I'd like to do.
> >
> > Many thanks!
>
> How about
>
> IMG(L(1),L(2),1)
>
> --Loren

This will just use the first and second element of L. So if I am lucky and none of them is zero, I'll get IMG(1,1,1) :-)

Thanks very much anyway!

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