raw downsample using c language and python visualization

void Downsampled_calculation(unsigned char *src, unsigned char *dst) {
    const uint32_t RawOut_Width = 3840;
    const uint32_t RawOut_Height = 2160;
    const uint32_t RawOut_Size = RawOut_Width * RawOut_Height * 2;
    const uint32_t Downfactor = 15;
    uint32_t new_height = RawOut_Height / Downfactor;
    uint32_t new_width = RawOut_Width / Downfactor;
    uint32_t new_Size = new_height * new_width * 2;
   // unsigned char *src_temp = src;
   // unsigned char *dst_temp = dst;

     for (int y = 0; y < new_height; y++) {
        for (int x = 0; x < new_width; x++) {

            memcpy(dst,src,2);
            dst += 2;
            src +=2 * Downfactor;
            //printf("%ld\n",*dst);
           
        }
        src += 2 * (Downfactor - 1) * RawOut_Width;
    }

}

downsample need input *src and *dst which represent full resolution and resize resolution.

out this function,the code are as follow:

    int width = 3840;
    int height = 2160;
    hi_s32 ret;
    hi_char path[PATH_MAX] = { 0 };
    size_t file_size;

    //if (realpath("./atc/aaa_rgb.bin", path) == HI_NULL) {    // rgb 
    if (realpath("./data/image/1.raw", path) == HI_NULL) {   //yuv
        sample_svp_trace_err("Invalid file!.\n");
        return HI_FAILURE;
    }

    

    u_int16_t *raw_in = (u_int16_t *)malloc(3840*2160*2);
    if(raw_in == NULL){

        printf("内存分配失败");
        return 1;
    }

    FILE *fp = fopen(path, "rb");
    sample_svp_check_exps_return(fp == HI_NULL, HI_FAILURE, SAMPLE_SVP_ERR_LEVEL_ERROR,
        "open image file failed!\n");

    if (fp == NULL) {
        fprintf(stderr, "无法打开文件\n");
        free(raw_in);
        return 1;
    }



    size_t element_size = fread(raw_in,2,3840*2160,fp);
    if (element_size != 3840*2160) {
        fprintf(stderr, "读取数据失败\n");
        free(raw_in);
        fclose(fp);
        return 1;
    }


   
   //ret = fread(dev_buf, 144*256*2, 1, low_raw_1d);

    u_int16_t *low_raw_1d = (u_int16_t*)malloc(144*256*2);
    
    Downsampled_calculation(raw_in,low_raw_1d);

    free(raw_in);
   

    
    // file_size = (file_size > buf_size) ? buf_size : file_size;
    


    
   // dev_buf = low_raw_1d;

    
    memcpy(dev_buf,low_raw_1d,144*256*2);

for(int i = 0 ;i<100;i++){


        printf("%d\n",*(low_raw_1d+i));
    }
    FILE *file_save = fopen("./raw_resize_.bin","wb");
    size_t num_write = fwrite(dev_buf,2,144*256,file_save);
    fclose(file_save);

after this operation , the low resolution image will be cp to dev_buf, free the low_raw_1d.

the code how to visualize the bin or raw use python are as follow:

import numpy as np
import matplotlib.pyplot as plt


filename = 'raw_resize_.bin'
width = 256
height = 144


data = np.fromfile(filename, dtype=np.uint16)
print(data.max())

data = data.reshape((height, width))
data = data**(1/2.2)

plt.imshow(data, cmap='gray')
plt.colorbar()
plt.title('16-bit RAW Image')
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SetMaker

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

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

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

打赏作者

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

抵扣说明:

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

余额充值