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

相关文章

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Java中实现订单超时自动取消功能(最新推荐)

《Java中实现订单超时自动取消功能(最新推荐)》本文介绍了Java中实现订单超时自动取消功能的几种方法,包括定时任务、JDK延迟队列、Redis过期监听、Redisson分布式延迟队列、Rocket... 目录1、定时任务2、JDK延迟队列 DelayQueue(1)定义实现Delayed接口的实体类 (

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1