[英]How I can resize a bitmap properly?
I want to downsample the bitmap of a BMP file by a factor M
. 我想将BMP文件的位图下采样
M
倍。 I want to obatain the image without aliasing. 我想获得不带锯齿的图像。 So in order to achieve it I compute the mean of the MxM pixels in this way:
因此,为了实现它,我以这种方式计算了MxM像素的均值:
The problem apears when I try to resize non-squared images because it only compute the mean proprely in a square. 当我尝试调整非正方形图像的大小时,问题就出现了,因为它仅按平方正确地计算均值。 For example, if the final image is 300x150, the mean is right until 150x150 pixel.
例如,如果最终图像为300x150,则均值直到150x150像素为止。 If I had the
previous_mean
-> new_mean = (previous_mean+value)/2
如果我有
previous_mean
> new_mean = (previous_mean+value)/2
This is how I actually compute it: 这是我实际的计算方式:
for (i = 0; i < new_height; i++) {
for (j = 0; j < new_width; j++) {
mean.r = bitmap[i*factor][j*factor].r;
mean.g = bitmap[i*factor][j*factor].g;
mean.b = bitmap[i*factor][j*factor].b;
for(k = i*factor; (k < i*factor+factor)&&(k<old_height); k++){
for(l = j*factor; (l < j*factor+factor)&&(l<old_width); l++){
mean.r = (mean.r + bitmap[k][l].r)/2;
mean.g = (mean.g + bitmap[k][l].g)/2;
mean.b = (mean.b + bitmap[k][l].b)/2;
}
}
new_bitmap[i][j] = mean;
mean.r = 0;
mean.g = 0;
mean.b = 0;
}
} }
new_bitmap
and bitmap
are 2-D array of PIXELS
, being PIXELS
: new_bitmap
和bitmap
是2-d阵列PIXELS
,作为PIXELS
:
typedef struct __attribute__((__packed__)){
unsigned char b;
unsigned char g;
unsigned char r;
} PIXELS;
这是绝对正确的,我用old_heigth替换了old_width。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.