void scale_brightness( uint8_t array[],
unsigned int cols,
unsigned int rows,
double scale_factor )
{
for (int x = 0; x < (cols*rows); x++)
{
array[x] = round(scale_factor * (double)(array[x]));
if (array[x] > 255 )
{
array[x] = 255;
}
}
}
此函数应该将像素乘以比例因子,并根据因子乘以的方式使图像变亮或变暗。如果相乘的图像大于255,则像素被阈值化为255.我尝试了代码并放入scale_brightness(数组,256,256,2)。这应该将每个像素乘以2,因此它应该使图像变亮。但当我跑它时,它变成了黑色,完全与它应该做的相反。我也试过做0,它变黑了。而任何其他颜色只是给我一个深灰色。有人能帮我找出原因吗?
答案 0 :(得分:2)
您的逻辑不起作用,因为array[x]
不能超过255,因此永远不会执行if
正文,而array[x]
只会被截断。使用额外的double
值进行计算:
double d = round(scale_factor * (double)(array[x]));
if (d >= 255.0 )
{
array[x] = 255;
}
else
{
array[x] = (uint8_t)d;
}
请注意,通过排除浮点运算可以有效地优化此循环。乘以double
因子的乘法可以乘以int
并向右移动来替换。