我在另一個程序中使用此方法時出現分段錯誤。我已經證實我的指針是正確的,並且這個問題肯定在這個函數中。我假設我試圖訪問我實際上無法訪問的內存。我的問題的解決方案,將不勝感激將像素減半像素
uint8_t* half(const uint8_t array[], unsigned int cols, unsigned int rows) {
int length = ((floor(cols/2)) * (floor(rows/2))); // change when we must ignore odd rows/cols
uint8_t* newimg = malloc(length * sizeof(uint8_t));
memset(newimg, 0, length);
int count = 0;
for(unsigned int c = 0; c < cols; c+=2){
for(unsigned int r = 0; r < rows; r+=2){
uint8_t first = array[(c - 1) + ((r - 1) * cols)];
uint8_t second = array[c + ((r - 1) * cols)];
uint8_t third = array[(c - 1) + (r * cols)];
uint8_t fourth = array[c + (r * cols)];
uint8_t mean = (first + second + third + fourth)/4;
newimg[count] = mean;
count++;
}
}
return newimg;
}
你的調試器說什麼? – 2014-10-07 18:18:38
它給予em沒有任何跡象表明有什麼錯誤,但是當編譯出現seg故障時,明顯是出現 – 2014-10-07 18:24:20
您不需要使用「floor」。對於非負整數,「n/2」與「floor(0.5 * n)」相同。 – 2014-10-07 18:26:36