我在另一个程序中使用此方法时出现分段错误。我已经证实我的指针是正确的,并且这个问题肯定在这个函数中。我假设我试图访问我实际上无法访问的内存。我的问题的解决方案,将不胜感激将像素减半像素
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