我的导师写道:“编写一个函数,将图像量化为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;
}
}当我试图量化一幅图像时,它要么变成完全白色,要么变成完全黑色。我想我对这个函数进行了错误的循环,但是不确定该怎么做才能修复它。
发布于 2016-04-18 02:29:43
错误在您的函数的头中。您正在传递图像的副本,因此您的副本会在函数内修改,但不会在函数外修改。此外,代码可以大大简化。我给你介绍一下我的方法:
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);
}https://stackoverflow.com/questions/36680330
复制相似问题