EBImage: filtering in the presence of NAs
1
1
Entering edit mode
@arnaud-duranel-5720
Last seen 10.2 years ago
Hello None of the EBImage filtering functions seems to work when there are NAs in the image: library(EBImage) lenac = readImage(system.file("images", "lena-color.png", package="EBImage")) lenag<-channel(lenac, 'gray') lenag[1:50, ]<-NA flo = makeBrush(21, shape='disc', step=FALSE)^2 flo = flo/sum(flo) lenaflo = filter2(lenag, flo) display(lenaflo) any(!is.na(lenaflo)) Is there a way to process an image that includes NAs, with some kind of na.rm=TRUE? I am working with pictures of irregularly-shaped objects, and the pixels between the picture margins and the object were set to NA using a mask. I want to apply filters to the object itself, not to the whole image. Is there another way to do this? Many thanks for your help. Best regards A. Duranel, UCL Department of Geography
PROcess EBImage PROcess EBImage • 1.5k views
ADD COMMENT
1
Entering edit mode
@wolfgang-huber-3550
Last seen 12 weeks ago
EMBL European Molecular Biology Laborat…
Dear Arnaud can you provide an example of what else you would expect to happen? As far as I can see, a linear filter operation on an image is defined as the convolution of the image with a kernel, and for discrete 2D images both kernel and image are assumed to be functions on Z^2 (Z being the whole numbers); usually they have finite support. Alternative, more general definitions are welcome, and we will be happy to consider them. Are you sure you cannot achieve what you want by setting your non-object pixels to a constant value such as zero? Also, please consider the erode/dilate functions, these in fact do work gracefully with images that contain NA: library("EBImage") x = readImage(system.file("images", "shapes.png", package="EBImage")) x[200:360, 140:200] = NA tableis.na((x))) # FALSE TRUE # 186787 9821 tableis.na(dilate(x, kern))) # # FALSE TRUE # 187071 9537 tableis.na(erode(x, kern))) # FALSE TRUE # 187384 9224 Best wishes Wolfgang Il giorno Mar 8, 2013, alle ore 3:43 PM, Arnaud Duranel <arnaud.duranel.09 at="" ucl.ac.uk=""> ha scritto: > Hello > > None of the EBImage filtering functions seems to work when there are NAs in the image: > > library(EBImage) > lenac = readImage(system.file("images", "lena-color.png", package="EBImage")) > lenag<-channel(lenac, 'gray') > lenag[1:50, ]<-NA > flo = makeBrush(21, shape='disc', step=FALSE)^2 > flo = flo/sum(flo) > lenaflo = filter2(lenag, flo) > display(lenaflo) > any(!is.na(lenaflo)) > > Is there a way to process an image that includes NAs, with some kind of na.rm=TRUE? > > I am working with pictures of irregularly-shaped objects, and the pixels between the picture margins and the object were set to NA using a mask. I want to apply filters to the object itself, not to the whole image. Is there another way to do this? > > Many thanks for your help. > > Best regards > > A. Duranel, UCL Department of Geography > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD COMMENT
0
Entering edit mode
Btw, one possible extended definition that I could think of is here: if we assumed that 0*NA=0, then in those places of the filtered image where all NA values in the original image coincide with zero values of the kernel, a finite value would be obtained. However, in R-2.15.2, > 0*NA [1] NA so the assumption would be inconsistent with R behaviour in other places. (Interestingly, FALSE && NA does in fact evaluate to FALSE). Also, I think it would be difficult to reconcile the above with the current implementation of linear filters, which uses fft (from the stats package). As said before, it is possible that what you need is provided by morphological operators, and in a much cooler way (http://en.wikipedia.org/wiki/Mathematical_morphology). Best wishes Wolfgang Il giorno Mar 9, 2013, alle ore 7:51 PM, Wolfgang Huber <whuber at="" embl.de=""> ha scritto: > Dear Arnaud > > can you provide an example of what else you would expect to happen? > > As far as I can see, a linear filter operation on an image is defined as the convolution of the image with a kernel, and for discrete 2D images both kernel and image are assumed to be functions on Z^2 (Z being the whole numbers); usually they have finite support. Alternative, more general definitions are welcome, and we will be happy to consider them. Are you sure you cannot achieve what you want by setting your non-object pixels to a constant value such as zero? > > Also, please consider the erode/dilate functions, these in fact do work gracefully with images that contain NA: > > library("EBImage") > x = readImage(system.file("images", "shapes.png", package="EBImage")) > x[200:360, 140:200] = NA > > tableis.na((x))) > > # FALSE TRUE > # 186787 9821 > > tableis.na(dilate(x, kern))) > # > # FALSE TRUE > # 187071 9537 > > tableis.na(erode(x, kern))) > > # FALSE TRUE > # 187384 9224 > > Best wishes > Wolfgang > > > > Il giorno Mar 8, 2013, alle ore 3:43 PM, Arnaud Duranel <arnaud.duranel.09 at="" ucl.ac.uk=""> ha scritto: > >> Hello >> >> None of the EBImage filtering functions seems to work when there are NAs in the image: >> >> library(EBImage) >> lenac = readImage(system.file("images", "lena-color.png", package="EBImage")) >> lenag<-channel(lenac, 'gray') >> lenag[1:50, ]<-NA >> flo = makeBrush(21, shape='disc', step=FALSE)^2 >> flo = flo/sum(flo) >> lenaflo = filter2(lenag, flo) >> display(lenaflo) >> any(!is.na(lenaflo)) >> >> Is there a way to process an image that includes NAs, with some kind of na.rm=TRUE? >> >> I am working with pictures of irregularly-shaped objects, and the pixels between the picture margins and the object were set to NA using a mask. I want to apply filters to the object itself, not to the whole image. Is there another way to do this? >> >> Many thanks for your help. >> >> Best regards >> >> A. Duranel, UCL Department of Geography >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD REPLY
1
Entering edit mode
Dear Wolfgang Many thanks for your reply. I am sorry I lack the theoretical knowledge in maths and image processing that would probably be necessary to clearly explain what I need and fully benefit from your advice, so my apologies for this. I found a work-around using the focal() function of the raster package that gives the result I am expecting: library(EBImage) library(raster) lenac = readImage(system.file("images", "lena-color.png", package="EBImage")) lenag<-channel(lenac, 'gray') lenag[1:50, ]<-NA lenag<-raster(imageData(lenag)) smoothed <- focal(lenag, w=15, fun = mean, na.rm=TRUE, pad=TRUE, padValue=NA) smoothed[is.na(lenag)]<-NA smoothed<-as.Image(as.matrix(smoothed)) display(smoothed) This might be quite crude but it does the job. Thanks again for your help Best wishes Arnaud On 09/03/2013 19:17, Wolfgang Huber wrote: > > Btw, one possible extended definition that I could think of is here: if we assumed that 0*NA=0, then in those places of the filtered image where all NA values in the original image coincide with zero values of the kernel, a finite value would be obtained. > > However, in R-2.15.2, >> 0*NA > [1] NA > > so the assumption would be inconsistent with R behaviour in other places. (Interestingly, FALSE && NA does in fact evaluate to FALSE). Also, I think it would be difficult to reconcile the above with the current implementation of linear filters, which uses fft (from the stats package). > > As said before, it is possible that what you need is provided by morphological operators, and in a much cooler way (http://en.wikipedia.org/wiki/Mathematical_morphology). > > Best wishes > Wolfgang > > > Il giorno Mar 9, 2013, alle ore 7:51 PM, Wolfgang Huber <whuber at="" embl.de=""> ha scritto: > >> Dear Arnaud >> >> can you provide an example of what else you would expect to happen? >> >> As far as I can see, a linear filter operation on an image is defined as the convolution of the image with a kernel, and for discrete 2D images both kernel and image are assumed to be functions on Z^2 (Z being the whole numbers); usually they have finite support. Alternative, more general definitions are welcome, and we will be happy to consider them. Are you sure you cannot achieve what you want by setting your non-object pixels to a constant value such as zero? >> >> Also, please consider the erode/dilate functions, these in fact do work gracefully with images that contain NA: >> >> library("EBImage") >> x = readImage(system.file("images", "shapes.png", package="EBImage")) >> x[200:360, 140:200] = NA >> >> tableis.na((x))) >> >> # FALSE TRUE >> # 186787 9821 >> >> tableis.na(dilate(x, kern))) >> # >> # FALSE TRUE >> # 187071 9537 >> >> tableis.na(erode(x, kern))) >> >> # FALSE TRUE >> # 187384 9224 >> >> Best wishes >> Wolfgang >> >> >> >> Il giorno Mar 8, 2013, alle ore 3:43 PM, Arnaud Duranel <arnaud.duranel.09 at="" ucl.ac.uk=""> ha scritto: >> >>> Hello >>> >>> None of the EBImage filtering functions seems to work when there are NAs in the image: >>> >>> library(EBImage) >>> lenac = readImage(system.file("images", "lena-color.png", package="EBImage")) >>> lenag<-channel(lenac, 'gray') >>> lenag[1:50, ]<-NA >>> flo = makeBrush(21, shape='disc', step=FALSE)^2 >>> flo = flo/sum(flo) >>> lenaflo = filter2(lenag, flo) >>> display(lenaflo) >>> any(!is.na(lenaflo)) >>> >>> Is there a way to process an image that includes NAs, with some kind of na.rm=TRUE? >>> >>> I am working with pictures of irregularly-shaped objects, and the pixels between the picture margins and the object were set to NA using a mask. I want to apply filters to the object itself, not to the whole image. Is there another way to do this? >>> >>> Many thanks for your help. >>> >>> Best regards >>> >>> A. Duranel, UCL Department of Geography >>> >>> _______________________________________________ >>> Bioconductor mailing list >>> Bioconductor at r-project.org >>> https://stat.ethz.ch/mailman/listinfo/bioconductor >>> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > > . >
ADD REPLY

Login before adding your answer.

Traffic: 597 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6