2013-01-15 199 views
0

我正在經歷「R編程的藝術」和部分跨這塊傳來:模糊的圖像

# adds random noise to img, at the range rows,cols of img; img and the 
# return value are both objects of class pixmap; the parameter q 
# controls the weight of the noise, with the result being 1-q times the 
# original image plus q times the random noise 
blurpart <- function(img,rows,cols,q) { 
    lrows <- length(rows) 
    lcols <- length(cols) 
    newimg <- img 
    randomnoise <- matrix(nrow=lrows, ncol=ncols,runif(lrows*lcols)) 
    [email protected] <- (1-q) * [email protected] + q * randomnoise 
    return(newimg) 
} 

我的問題是關於行:

[email protected] <- (1-q) * [email protected] + q * randomnoise 

如何[email protected]結束其尺寸與[email protected]相同。由於randomnoise是較小的矩陣,因此[email protected]部分如何識別要模糊的圖像部分。

我認爲應該是這樣的:

newimg <- img 
[email protected][rows,cols] <- (1-q) * [email protected][rows,cols] + q * randomnoise 
+2

什麼是'ncols'在那個函數中?它應該是'lcols'嗎?你能給我們一個完整的工作可重複的例子嗎?我得到的只有'ncols not found',直到我將其更改爲'lcols',然後得到'不符合的數組'。你有這個工作嗎?幫助美國幫助你。 – Spacedman

+1

正在看[Google Books](http://books.google.co.uk/books?id=4jL3sfmP1l8C&pg=PA65&lpg=PA65&dq=%22adds+random+noise+to+img,+at+the+range+rows, COLS + +的img%22&源= BL&OTS = 0SkjF1qCIx&SIG = ILB-UaRzXoh0mIASnUCpTyz3dSQ&HL = EN&SA = X&EI = HUj1UOenIYmGhQedyoCACg&VED = 0CEYQ6AEwAw#v = onepage&q =%22adds%20random%20noise%20to%20img%2C%20AT%第二十條%20range%20rows%2Ccols的%20of%20img%22&f = false)它似乎是在原始 – Henry

+0

中的拼寫錯誤按照原樣嘗試運行示例並使用建議的更正不是很容易嗎?爲什麼在做明顯之前發佈問題? –

回答

1

似乎有在書中印刷錯誤,我已經驗證書中的代碼不能正常工作,甚至後得到錯字權利。我已將反饋意見發送給作者。正確的一塊應該是:

newimg <- img 
[email protected][rows,cols] <- (1-q) * [email protected][rows,cols] + q * randomnoise