From: Mirko on
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!
From: Jos (10584) on
"Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq0sk$gkr$1(a)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
From: Mirko on
"Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hvq5im$5bm$1(a)fred.mathworks.com>...
> "Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq0sk$gkr$1(a)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.
From: Jos (10584) on
"Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq6ol$hcf$1(a)fred.mathworks.com>...
> "Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hvq5im$5bm$1(a)fred.mathworks.com>...
> > "Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq0sk$gkr$1(a)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
From: Mirko on
"Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hvqcpp$86s$1(a)fred.mathworks.com>...
> "Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq6ol$hcf$1(a)fred.mathworks.com>...
> > "Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hvq5im$5bm$1(a)fred.mathworks.com>...
> > > "Mirko " <arnoldma(a)tcd.ie> wrote in message <hvq0sk$gkr$1(a)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!