C实现纵横比保持的RGB图像缩放

2024-03-07 07:48

本文主要是介绍C实现纵横比保持的RGB图像缩放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

双线性插值,实现图像scale down, 记个笔记


#include "stdafx.h"
#include <iostream>
#include "stdio.h"using namespace std;int img_down_scale(const unsigned char* src_img, unsigned char* dst_img, unsigned int src_width, unsigned int src_height, unsigned int dst_width, unsigned int dst_height)
{//bilinear interpolationif (!src_img || !dst_img) {std::cout << "input array can not be null!" << std::endl;return -1;}if (!dst_width|| !dst_height) {std::cout << "image size wrong!" << std::endl;return -2;}double x_ratio = (double)src_width / dst_width;double y_ratio = (double)src_height / dst_height;double aspect_ratio = 0.0f;int border_shift = 0;int h = 0, w = 0;int x0 = 0, x1 = 0, y0 = 0, y1 = 0;int dst_r = 0, dst_g = 0, dst_b = 0;if (x_ratio < y_ratio) {aspect_ratio = x_ratio;border_shift = (int)((src_height - (int)(dst_height * aspect_ratio)) / 2);for (h = 0; h < dst_height; h++) {for (w = 0; w < dst_width; w++) {double src_h = border_shift + h * aspect_ratio;double src_w = w * aspect_ratio;x0 = (int)src_w;x1 = x0 + 1;y0 = (int)src_h;y1 = y0 + 1;int rx0y0 = src_img[3 * (y0*src_width + x0) + 0];int gx0y0 = src_img[3 * (y0*src_width + x0) + 1];int bx0y0 = src_img[3 * (y0*src_width + x0) + 2];int rx1y0 = src_img[3 * (y0*src_width + x1) + 0];int gx1y0 = src_img[3 * (y0*src_width + x1) + 1];int bx1y0 = src_img[3 * (y0*src_width + x1) + 2];int rx0y1 = src_img[3 * (y1*src_width + x0) + 0];int gx0y1 = src_img[3 * (y1*src_width + x0) + 1];int bx0y1 = src_img[3 * (y1*src_width + x0) + 2];int rx1y1 = src_img[3 * (y1*src_width + x1) + 0];int gx1y1 = src_img[3 * (y1*src_width + x1) + 1];int bx1y1 = src_img[3 * (y1*src_width + x1) + 2];dst_r = (y1 - src_h) * ((x1 - src_w)* rx0y0 + (src_w - x0)*rx1y0) + (src_h - y0) *((x1 - src_w)*rx0y1 + (src_w - x0)*rx1y1);dst_g = (y1 - src_h) * ((x1 - src_w)* gx0y0 + (src_w - x0)*gx1y0) + (src_h - y0) *((x1 - src_w)*gx0y1 + (src_w - x0)*gx1y1);dst_b = (y1 - src_h) * ((x1 - src_w)* bx0y0 + (src_w - x0)*bx1y0) + (src_h - y0) *((x1 - src_w)*bx0y1 + (src_w - x0)*bx1y1);dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;}}} else {aspect_ratio = y_ratio;border_shift = (int)((src_width - (int)(dst_width * aspect_ratio)) / 2);for (h = 0; h < dst_height; h++) {double src_h = h * aspect_ratio;for (w = 0; w < dst_width; w++) {double src_w = border_shift +  w * aspect_ratio;x0 = (int)src_w;x1 = x0 + 1;y0 = (int)src_h;y1 = y0 + 1;int rx0y0 = src_img[3 * (y0*src_width + x0) + 0];int gx0y0 = src_img[3 * (y0*src_width + x0) + 1];int bx0y0 = src_img[3 * (y0*src_width + x0) + 2];int rx1y0 = src_img[3 * (y0*src_width + x1) + 0];int gx1y0 = src_img[3 * (y0*src_width + x1) + 1];int bx1y0 = src_img[3 * (y0*src_width + x1) + 2];int rx0y1 = src_img[3 * (y1*src_width + x0) + 0];int gx0y1 = src_img[3 * (y1*src_width + x0) + 1];int bx0y1 = src_img[3 * (y1*src_width + x0) + 2];int rx1y1 = src_img[3 * (y1*src_width + x1) + 0];int gx1y1 = src_img[3 * (y1*src_width + x1) + 1];int bx1y1 = src_img[3 * (y1*src_width + x1) + 2];dst_r = (y1 - src_h) * ((x1 - src_w)* rx0y0 + (src_w - x0)*rx1y0) + (src_h - y0) *((x1 - src_w)*rx0y1 + (src_w - x0)*rx1y1);dst_g = (y1 - src_h) * ((x1 - src_w)* gx0y0 + (src_w - x0)*gx1y0) + (src_h - y0) *((x1 - src_w)*gx0y1 + (src_w - x0)*gx1y1);dst_b = (y1 - src_h) * ((x1 - src_w)* bx0y0 + (src_w - x0)*bx1y0) + (src_h - y0) *((x1 - src_w)*bx0y1 + (src_w - x0)*bx1y1);dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;}}}return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{unsigned char *pSrcImg = (unsigned char*)malloc(1000 * 1000 * 3);unsigned char *pDstImg = (unsigned char*)malloc(480 * 320 * 3);FILE *fp = fopen("input_1000x1000.bmp", "rb");fseek(fp, 54, SEEK_SET);fread(pSrcImg, 1000 * 1000 * 3, 1, fp);fclose(fp);img_down_scale(pSrcImg, pDstImg, 1000,1000, 480,320);unsigned char bmpHeader[54] = { 0 };FILE *fp_bmp_header = fopen("semseg_480x320.bmp", "rb");fread(bmpHeader, 54, 1, fp_bmp_header);fclose(fp_bmp_header);FILE *fp_write = fopen("down_scale_480x320.bmp", "wb");if (fp_write){fwrite(bmpHeader, 54, 1, fp_write);fwrite(pDstImg, 480 * 320 * 3, 1, fp_write);fclose(fp_write);fp_write = NULL;}if (pSrcImg) { free(pSrcImg); pSrcImg = NULL; }if (pDstImg) { free(pDstImg); pDstImg = NULL; }return 0;
}

这篇关于C实现纵横比保持的RGB图像缩放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/782827

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的