2
我有一個存儲灰度圖像值的Npp8u * image = new Npp8u[nWidth * nHeight];
數組。我將Npp8u定義爲typedef unsigned char Npp8u;
。當我嘗試從過去的像素中減去最低的圖像值時,會出現問題。寫入值時數組索引似乎滑動
我用這個函數做減法。
void SubtractValue(Npp8u * image, int width, int height, Npp8u value){
int iValue = value;
for (int i = 0; i < height; ++i){
for (int j = 0; j < width; ++j){
int indexVal = (image[i*width + j]);
int newVal = indexVal - iValue;
image[i*width + j] = (Npp8u) newVal;
}
}
}
這個函數的結果很奇怪。我使用Lena Image來測試,這是原始圖像。
如果該值是85,我得到以下圖像
注:原始圖像.PGM
閱讀程序:
for (int i = 0; i < nHeight; ++i)
for (int j = 0; j < nWidth; ++j)
image[i*nWidth + j] = fgetc(fInput);
寫作過程:
for (int i = 0; i < nHeight; ++i)
for (int j = 0; j < nWidth; ++j)
fputc(image[i*nWidth + j], fOutput);
Upd吃了:如果像素值爲10,這是新的換行,則會出現問題。
當您讀寫圖像而不更改任何內容時會發生什麼? – wimh 2014-11-23 19:13:18
@Wimmel我得到相同的圖像,並且如果我減去10(值爲10),像素中沒有滑動。 – Hakes 2014-11-23 19:18:17
只是一個瘋狂的猜測; [格式說明](http://en.wikipedia.org/wiki/Netpbm_format#PGM_example)指定標題中的最大白色值。如果從小於85的數字中減去85,則數字將環繞並且結果將是高白值。如果它大於標題中的最大值,它可能會被丟棄。 – wimh 2014-11-23 19:28:35