2012-03-26 282 views
1

當使用R中的包裝點陣繪製輪廓圖時,如何控制輪廓線的平滑度?對於輪廓邊緣清晰的低分辨率圖像,默認值特別令人不快。輪廓線中輪廓線的平滑度

編輯︰ 這是我的問題 - 段在銳角加入,而我想要一個光滑的關節。

require(lattice) 
contourplot(volcano[1:10,1:10]) 
+2

你可以提供證明您所描述的行爲重複的例子? – joran 2012-03-26 15:54:52

回答

1

沒有一個可重複的例子,要準確理解你想要的東西有點棘手。

無論如何,請嘗試KernSmooth包。首先,我們的包,並得到了一些數據:

library("KernSmooth") 
data(geyser, package="MASS") 

接下來我們使用bkde2D函數來計算一個2-d核密度估計。更改帶寬以獲得更多的平滑:

x <- cbind(geyser$duration, geyser$waiting) 
est <- bkde2D(x, bandwidth=c(0.7, 7)) 

然後做一個等高線圖上的平滑輸出:

contour(est$x1, est$x2, est$fhat) 
+0

因爲我們都在猜測JohnRos只是想要美觀的輪廓,所以類似的方法是將他的圖像變成一個'rasterimage',因爲IIRC默認的轉換會進行一些邊緣平滑處理。 – 2012-03-26 19:42:22

+0

那麼唯一的辦法就是平滑圖像?我寧願平滑輪廓,但保持圖像不變。 – JohnRos 2012-03-26 20:30:58

+0

你會如何建議平滑?等高線跟隨數據。這就是說,這是一個可以工作的黑客。計算(但不要繪製)輪廓以獲得平滑的圖像副本,即smoothlines <-contourplot(volcano)'。繪製原始圖像,然後提取「smoothlines」的輪廓線分量並繪製它們。我沒有嘗試過,也沒有調查'trellis'對象'countourplot'返回的結構,但它可能會解決。 – 2012-03-27 12:12:10

0

下面是從更新的問題,平滑的數據。例如,使用loess迴歸答案。

# original data - jagged contours 
require(lattice) 
contourplot(volcano[1:10, 1:10]) 

# convert to long form 
require(reshape2) 
a = melt(volcano) 

# use loess to smooth 
# higher values of span will deliver more smoothing 
b = loess(value ~ Var1+Var2, data=a, span=0.02) 

# estimate the smoothed values on our grid 
smooth = predict(b, newdata=data.frame(Var1=a$Var1, Var2=a$Var2)) 
smooth = array(smooth, dim=dim(volcano)) 

# the result 
contourplot(smooth[1:10, 1:10]) 


# worth also comparing the bigger picture 
contourplot(volcano) 
contourplot(smooth)