Reputation: 9
My instructor wrote "Write a function that quantizes the image to q grayscale shades. For example, if q=8, replace each pixel between 0-31 by 0, 32-63 by 32, ..., and 224-255 by 224." This is my code so far.
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;
}
}
When I try to quantize an image it either turns completely white or completely black. I think I'm looping this function wrong but not sure what to do to fix it.
Upvotes: 0
Views: 3025
Reputation: 341
The error is in the header of your function. You are passing a copy of your image, so your copy is modified inside your function, but not out of the function. Moreover, the code could be simplified a lot. I give you my approach:
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);
}
Upvotes: 2