我的導師寫道:「寫一個量化圖像爲q灰度陰影的函數,例如,如果q = 8,則將0-31之間的每個像素替換爲0,32-63,32,...,和224到224-255。「這是我的代碼到目前爲止。量化圖像
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;
}
}
當我嘗試量化圖像時,它會變成完全白色或完全黑色。我想我正在循環這個功能錯誤,但不知道如何解決它。
你爲什麼不做'image [col] [row] = floor((double)image [col] [row]/temp)* temp',並且只使用嵌套循環? – Carafini