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()