使用哪种算法将32位RGB图像缩小到自定义分辨率? 算法应该是平均像素。
例如,如果我有100x100图像,我想要大小为20x50的新图像。 第一个源行的前五个像素的平均值将给出dest的第一个像素,而第一个源列的前两个像素的平均值将给出第一个dest列像素。
目前我所做的是先在X分辨率下缩小,然后在Y分辨率下缩小。 我在这个方法中需要一个临时缓冲区。
你知道有没有优化的方法?
which algorithm to use to scale down 32Bit RGB IMAGE to custom resolution? Algorithm should average pixels.
for example If I have 100x100 image and I want new Image of size 20x50. Avg of first five pixels of first source row will give first pixel of dest, And avg of first two pixels of first source column will give first dest column pixel.
Currently what I do is first scale down in X resolution, and after that I scale down in Y resolution. I need one temp buffer in this method.
Is there any optimized method that you know?
最满意答案
这平均了适当的像素。
w_ratio = src.w / dest.w h_ratio = src.h / dest.h dest[x,y] = AVG( src[x * w_ratio + xi, y * h_ratio + yi] ) where xi in range (0, w_ratio - 1), inc by 1 yi in range (0, h_ratio - 1), inc by 1对于边界条件,执行单独的循环(如果在循环中则不进行)。
这是一个更像C的代码:
src和dest是位图: *属性src [x,y]为像素 *属性src.w的宽度 *属性src.h为高度
已经定义了像素
加入
p1 = p1 + p2 is same as p1.r = p1.r + p2.r p1.g = p1.g + p2.g ...师
p1 = p1 / c p1.r = p1.r / c p1.g = p1.g / c用常数0进行评估
p1 = 0 p1.r = 0 p1.g = 0 ...为简单起见,我不会在像素分量整数溢出时考虑问题...
float w_ratio = src.w / dest.w; float h_ratio = src.h / dest.h; int w_ratio_i = floor(w_ratio); int h_ratio_i = floor(h_ratio); wxh = w_ratio*h_ratio; for (y = 0; y < dest.w; y++) for (x = 0; x < dest.h; x++){ pixel temp = 0; int srcx, srcy; // we have to use here the floating point value w_ratio, h_ratio // otherwise towards the end it can get a little wrong // this multiplication can be optimized similarly to Bresenham's line srcx = floor(x * w_ratio); srcy = floor(y * h_ratio); // here we use floored value otherwise it might overflow src bitmap for(yi = 0; yi < h_ratio_i; yi++) for(xi = 0; xi < w_ratio_i; xi++) temp += src[srcx + xi, srcy + yi]; dest[x,y] = temp / wxh; }Bresenham的线优化
This averages the appropriate pixels.
w_ratio = src.w / dest.w h_ratio = src.h / dest.h dest[x,y] = AVG( src[x * w_ratio + xi, y * h_ratio + yi] ) where xi in range (0, w_ratio - 1), inc by 1 yi in range (0, h_ratio - 1), inc by 1For boundary conditions do a separate loop (no if's in loop).
Here's a more C like code:
src and dest are bitmaps that: * property src[x,y] for pixel * property src.w for width * property src.h for height
pixel has been defined so that
adding
p1 = p1 + p2 is same as p1.r = p1.r + p2.r p1.g = p1.g + p2.g ...division
p1 = p1 / c p1.r = p1.r / c p1.g = p1.g / cevaluation with a constant 0
p1 = 0 p1.r = 0 p1.g = 0 ...for simplicity sake I won't consider the problem when pixel component integer overflows...
float w_ratio = src.w / dest.w; float h_ratio = src.h / dest.h; int w_ratio_i = floor(w_ratio); int h_ratio_i = floor(h_ratio); wxh = w_ratio*h_ratio; for (y = 0; y < dest.w; y++) for (x = 0; x < dest.h; x++){ pixel temp = 0; int srcx, srcy; // we have to use here the floating point value w_ratio, h_ratio // otherwise towards the end it can get a little wrong // this multiplication can be optimized similarly to Bresenham's line srcx = floor(x * w_ratio); srcy = floor(y * h_ratio); // here we use floored value otherwise it might overflow src bitmap for(yi = 0; yi < h_ratio_i; yi++) for(xi = 0; xi < w_ratio_i; xi++) temp += src[srcx + xi, srcy + yi]; dest[x,y] = temp / wxh; }Bresenham's line optimization
更多推荐
发布评论