我的导师写道“编写一个将图像量化为q灰度阴影的函数。例如,如果q = 8,则将0-31之间的每个像素替换为0,32-63乘32,...,和224- 255乘224.“到目前为止,这是我的代码。
void quantize (int image[][MAXHEIGHT], int width, int height, int q)
{
const int temp = 256 / q;
int startPixel, endPixel;
int indicator = temp; // where the pixels are divided, 0-31, 32-63, etc if q = 8
while (indicator <= 256)
{
startPixel = indicator - temp;
endPixel = indicator - 1;
cout << "start value is " << startPixel << " and end value is " << endPixel << endl;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if ((image[col][row] > startPixel) && (image[col][row] <= endPixel));
{
image[col][row] = startPixel;
}
}
}
indicator += temp;
}
}
当我尝试量化图像时,它会变成完全白色或完全变黑。我想我正在循环这个函数错误,但不知道如何解决它。
答案 0 :(得分:2)
错误位于函数的标题中。您正在传递图像的副本,因此您的副本会在您的功能中被修改,但不会超出该功能。而且,代码可以简化很多。我告诉你我的方法:
void quantize (int **image, int width, int height, int q)
{
const int r = 256 / q; // Ensure your image is on [0 255] range
for (int row = 0; row < height; row++)
for (int col = 0; col < width; col++)
image[col][row] = (int) (image[col][row] / r);
}