图像小波变换理解

文章介绍了小波变换在图像和视频编码中的使用,作为替代JPEG和H.26x中块编码的一种方法,小波变换因其可变长度的变换基能避免块效应,提供更平滑的图像质量。文中还展示了小波变换的过程,包括一次和二次变换的结果,并提供了一个C语言的小波变换示例程序,该程序读取YUV420序列并输出小波变换后的图像文件。
摘要由CSDN通过智能技术生成

图像和视频编码过程中,采用变换将像素值转换到频域进行处理。JPEG和H.26x通常将图像分成小块,在块内进行变换编码。这种方式容易产生块效应。

小波变换的变换基长度是可变的,因此可以对整帧图像进行处理,避免了分块过程。

下图为对一幅图像进行一次小波变换示意图。

图像分辨率为MxN,首先乘以右边的MxM矩阵,得到的结果中,左半边是低频图像,右半边是水平方向高频图像。然后,左边的NxN矩阵再乘以中间图像,得到的结果中图像被分为四个部分,如下图所示。

对图像的左上角低频的A部分,可以再进行小波变换,结果如下图

下面代码为小波变换示例程序。读取yuv420序列,对每一帧进行小波变换后,输出对应的图像文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ABS(x) ( (x)>=0?(x):-(x) )

int main(int argc, char* argv[])
{
    FILE *yuv420_file, *yuv420_dwtfile;
    long f_size = 0;
    int pic_width, pic_height, yuv420_frame_size, yuv420_y_size, yuv420_frame_num;
    int pixel_byte = 1; // 8bit
    unsigned char  *yuv420_buf, *yuv420_dwtbuf;
    unsigned char  *psrc_yuv420, *pdst_yuv420_dwt;
    int i, w, h, depth_count, dwt_width, dwt_height;
    int dwt_depth = 1;
    if(argc < 6)
    {
        printf("This program is used to load yuv420 frame and perform discrete wavelet transform.\n");
        printf("Usage:\n");
        printf("yuv_dwt yuv420file output_dwtfile  width  height dwt_depth\n");
        return 0;
    }

    printf("Input YUV420 File: %s\n", argv[1]);
    yuv420_file  = fopen(argv[1], "rb");
    if(yuv420_file == NULL)
    {
        printf("Open input yuv420(10bit) file %s error\n", argv[1]);
        return -1;
    }
    
    yuv420_dwtfile = fopen(argv[2], "wb");
    if(yuv420_dwtfile == NULL)
    {
        printf("Open output file %s error\n", argv[2]);
        fclose(yuv420_file);
        return -1;
    }
    
    pic_width  = atoi(argv[3]);
    pic_height = atoi(argv[4]);
    dwt_depth  = atoi(argv[5]);
    
    yuv420_frame_size = pic_width * pic_height * 3 / 2 * pixel_byte;  // yuv420frame is 10bit
    yuv420_y_size     = pic_width * pic_height * pixel_byte;          // yuv420frame is 10bit
    yuv420_buf        = (unsigned char*)malloc(yuv420_frame_size);
    yuv420_dwtbuf     = (unsigned char*)malloc(yuv420_y_size);

    fseek(yuv420_file, 0L, SEEK_END); // seek to file end
    f_size = ftell(yuv420_file);
    fseek(yuv420_file, 0L, SEEK_SET); // seek to file begin 
    yuv420_frame_num = f_size / yuv420_frame_size;
    printf("YUV420 Frame Size %dx%d, Frame numbers = %d\n", pic_width, pic_height, yuv420_frame_num);
    
    for(i = 0; i < yuv420_frame_num; i++)
    {
        printf("Process Frame %d\n", i + 1);
        fread(yuv420_buf, 1, yuv420_frame_size, yuv420_file);
        psrc_yuv420     = (unsigned char *)yuv420_buf;
        pdst_yuv420_dwt = (unsigned char *)yuv420_dwtbuf;

        dwt_width   = pic_width;
        dwt_height  = pic_height;
        depth_count = 0;
        while(depth_count < dwt_depth)
        {
            // Horizontal transform
            for(h = 0; h < dwt_height; h++)
            {
                for(w = 0; w < dwt_width / 2; w++)
                {
                    pdst_yuv420_dwt[h * pic_width + w]                 =    (psrc_yuv420[h * pic_width + 2 * w] + psrc_yuv420[h * pic_width + 2 * w + 1]) / 2; 
                    pdst_yuv420_dwt[h * pic_width + w + dwt_width / 2] = ABS(psrc_yuv420[h * pic_width + 2 * w] - psrc_yuv420[h * pic_width + 2 * w + 1]) / 2; 
                }
            }
            // Vertical transform
            for(w = 0; w < dwt_width; w++)
            {
                for(h = 0; h < dwt_height / 2; h++)
                {
                    psrc_yuv420[h * pic_width + w]                    =    (pdst_yuv420_dwt[2 * h * pic_width + w] + pdst_yuv420_dwt[(2 * h + 1) * pic_width + w]) / 2;
                    psrc_yuv420[(dwt_height / 2 + h) * pic_width + w] = ABS(pdst_yuv420_dwt[2 * h * pic_width + w] - pdst_yuv420_dwt[(2 * h + 1) * pic_width + w]) / 2;
                }
            }

            depth_count++;
            dwt_width  /= 2;
            dwt_height /= 2;
        }
        
        //*** Output dwt frame
        fwrite(psrc_yuv420, 1, yuv420_y_size,  yuv420_dwtfile);
    }

    fclose(yuv420_file);
    fclose(yuv420_dwtfile);
    free(yuv420_buf);
    free(yuv420_dwtbuf);
}

原始图像如下:

一阶小波变换结果

二阶小波变换结果

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ITRonnie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值