0

I've a strange problem after applying the kernel2d R code on my images. The resulting density plot is shifted several pixels (up & right side). I load my image, threshold it (until now the images are perfectly overlapping). Then I generate a list of pixels > 0.

ListPosPixels=which(ThIv>0, arr.ind = TRUE);

which serves as input for the kernel2d function. w and height are set to the dimensions of the input image. For h I've chosen different values but they seem not to be correlated with the shifts.

res <- kernel2d(as.points(ListPosPixels), poly=cbind(c(0,    d[1], d[1],     0), 
                                                     c(0,       0, d[2], d[2])),
                h0=h, nx=w, ny=height);

The image is then stored as a png

png(paste('KernelDensity.png', sep=''), width=w, height=height)
par(mar=c(0,0,0,0))
contour(res, add=F, drawlabels=F, col='green',xaxt='n', yaxt='n')

Any hints? Thanks! Durin

Full code example:

library(EBImage)
require(splancs)


options(max.contour.segments=50000);
ThIv<-readImage('shiftedInput.jpg');
ThIv<-flip(ThIv); #due to plotting
ListPosPixels=which(ThIv>0, arr.ind = TRUE);
d=dim(ThIv);
w <-d[1];
height <- d[2];
res <- kernel2d(as.points(ListPosPixels), poly=cbind(c(0,    d[1], d[1],     0), 
                                                     c(0,       0, d[2], d[2])),
                h0=20, nx=w, ny=height);

png(paste('Kerneldensity.png', sep=''), width=w, height=height)
par(mar=c(0,0,0,0))
contour(res, add=F, drawlabels=F, col='green',xaxt='n', yaxt='n')
dev.off()

Input image for code above

Example Image

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
Durin
  • 687
  • 1
  • 8
  • 18
  • Can you attach the image to this post? I suspect it won't be available at the link you provided for very long, hence making this question obsolete quite fast. – Roman Luštrik Aug 07 '13 at 10:52
  • Can you: make a reproducible example using data we can all get or generated randomly that illustrates the problem; tell us what package kernel2d comes from; – Spacedman Aug 07 '13 at 11:19
  • Ok thanks! I added a full code example. The generated kernel density plot will be shifted to bottom/right side. – Durin Aug 07 '13 at 12:28

0 Answers0