双线性内插法进行图像旋转算法的C代码实现

这篇博客介绍了如何使用双线性内插法实现图像旋转,并通过C代码展示了具体实现过程。同时,讨论了利用GPU进行计算加速的原因及其在图像处理中的优势,包括矩阵乘法的并行计算特性。文章最后提到了图像转置操作的算法效果。
摘要由CSDN通过智能技术生成

图像做rotate的数学原理如下,它等效于二维空间中的线性变换(旋转).

原图:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <math.h>

#define DBG(fmt, ...)   do { printf("%s line %d, "fmt"\n", __func__, __LINE__, ##__VA_ARGS__); } while (0)

typedef struct image {
    int w;
    int h;
    int c;
    unsigned char *data;
} image;

static void dump_memory(uint8_t *buf, int32_t len)
{
    int i;
 
    printf("\n\rdump file memory:");
    for (i = 0; i < len; i ++)
    {
        if ((i % 16) == 0)
        {
            printf("\n\r%p: ", buf + i);
        }
        printf("0x%02x ", buf[i]);
    }

    printf("\n\r");

    return;
}

image make_empty_image(int w, int h, int c)
{
    image out;             
    out.data = 0;
    out.h = h;                                                                                                                                                                                              
    out.w = w;
    out.c = c;
    return out;
}

image copy_image(image p)
{
	image copy = p;
    copy.data = (unsigned char*)calloc(p.h * p.w * p.c, sizeof(float));
    memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
    return copy;
}

image make_image(int w, int h, int c)
{
    image out = make_empty_image(w,h,c);
    out.data = (unsigned char*)calloc(h * w * c, sizeof(char));
    return out;
}

static void set_pixel(image m, int x, int y, int c, float val)
{
    if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c) return;

    assert(x < m.w && y < m.h && c < m.c);
    m.data[c*m.h*m.w + y*m.w + x] = val;
}

static float get_pixel(image m, int x, int y, int c)
{ 
    assert(x < m.w && y < m.h && c < m.c);
    return m.data[c*m.h*m.w + y*m.w + x];
} 

void free_image(image m)
{   
    if(m.data){
       free(m.data);
    }
} 

static void add_pixel(image m, int x, int y, int c, float val)
{            
    assert(x < m.w && y < m.h && c < m.c);
    m.data[c*m.h*m.w + y*m.w + x] += val;
}

image resize_image(image im, int w, int h)
{                                         
    if (im.w == w && im.h == h) return copy_image(im);

    image resized = make_image(w, h, im.c);
    image part = make_image(w, im.h, im.c);
    int r, c, k;                          
    float w_scale = (float)(im.w - 1) / (w - 1);
    float h_scale = (float)(im.h - 1) / (h - 1);
    for(k = 0; k < im.c; ++k){            
        for(r = 0; r < im.h; ++r){        
            for(c = 0; c < w; ++c){       
                float val = 0;            
                if(c == w-1 || im.w == 1){
                    val = get_pixel(im, im.w-1, r, k);
                } else {                  
                    float sx = c*w_scale; 
                    int ix = (int) sx;    
                    float dx = sx - ix;   
                    val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k);
                }                         
                set_pixel(part, c, r, k, val);
            }                             
        }                                 
    }                                     
    for(k = 0; k < im.c; ++k){            
        for(r = 0; r < h; ++r){           
           float sy = r*h_scale;         
           int iy = (int) sy;            
           float dy = sy - iy;           
           for(c = 0; c < w; ++c){       
               float val = (1-dy) * get_pixel(part, c, iy, k);
               set_pixel(resized, c, r, k, val);
           }                             
           if(r == h-1 || im.h == 1) continue;
           for(c = 0; c < w; ++c){       
               float val = dy * get_pixel(part, c, iy+1, k);
               add_pixel(resized, c, r, k, val);
           }                             
        }                                 
    }                                     

    free_image(part);                     
    return resized;                       
}

static float get_pixel_extend(image m, int x, int y, int c)
{             
    if (x < 0 || x >= m.w || y < 0 || y >= m.h) return 0xff;
    /*        
    if(x < 0) x = 0;
    if(x >= m.w) x = m.w-1;
    if(y < 0) y = 0;
    if(y >= m.h) y = m.h-1;
    */        
    if (c < 0 || c >= m.c) return 0;
    return get_pixel(m, x, y, c);
}

float bilinear_interpolate(image im, float x, float y, int c)
{      
    int ix = (int) floorf(x);
    int iy = (int) floorf(y);
       
    float dx = x - ix;
    float dy = y - iy;
       
    float val = (1-dy) * (1-dx) * get_pixel_extend(im, ix, iy, c) +
        dy     * (1-dx) * get_pixel_extend(im, ix, iy+1, c) +
        (1-dy) *   dx   * get_pixel_extend(im, ix+1, iy, c) +
        dy     *   dx   * get_pixel_extend(im, ix+1, iy+1, c);
    return val;
}  

image rotate_image(image im, float rad) 
{                
    int x, y, c;
    float cx = im.w/2.;
    float cy = im.h/2.;
    image rot = make_image(im.w, im.h, im.c);
    for(c = 0; c < im.c; ++c){
        for(y = 0; y < im.h; ++y){
            for(x = 0; x < im.w; ++x){
                float rx = cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx;
                float ry = sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy;
                float val = bilinear_interpolate(im, rx, ry, c);
                set_pixel(rot, x, y, c, val);
            }
        }    
    }            
    return rot; 
}

int main(int argc, char **argv)
{
	FILE *file;
	int width, height, i
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

papaofdoudou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值