GitHub上有个开源的stb库,Star数已过万,地址为https://github.com/nothings/stb,为何叫stb,是用的作者名字的缩写Sean T. Barrett。此库仅包含头文件,除stretchy_buffer.h外,其它所有文件以前缀stb开头,每个头文件的作用及用法在每个头文件的开始部分都作了介绍。此开源库的license为public domain或MIT。下面仅对与图像相关的头文件的作用及使用进行简单的说明,当仅需要将图像数据载入内存、或进行缩放操作、或保存图像时使用stb会非常方便,因为仅需要include一个或三个头文件即可,不需要额外图像处理库的依赖,如libjpeg、libpng、opencv等:

1. stb_image.h:载入图像,支持的图像文件格式包括JPEG、PNG、TGA、BMP、PSD、GIF、HDR、PIC、PNM,使用到的函数主要为:stbi_load,参数依次为:图像文件名(filename),获取图像宽(x),获取图像高(x),获取图像通道数(channels_in_file)、指定期望的通道数(desired_channels,若为0则不做颜色空间变换),此函数正常返回图像数据指针,否则返回NULL;

2. stb_image_resize.h:图像缩放,使用到的函数主要为stbir_resize_uint8,参数依次为:输入图像数据指针(input_pixels)、输入图像宽(input_w)、输入图像高(input_h)、输入图像步长(input_stride_in_bytes,若为0则为宽x通道数)、输出图像数据指针(output_pixels)、输出图像宽(output_w)、输出图像高(output_h)、输出图像步长(output_stride_in_bytes,若为0则为宽*通道数)、图像通道数(num_channels,输入与输出一致),此函数正常返回1,否则返回0;

3. stb_image_write.h:保存图像,支持的图像文件格式包括PNG、BMP、TGA、JPG、HDR,使用到的函数主要为stbi_write_xxx,其中xxx可以为png、bmp、tga、hdr、jpg,参数依次为:保存图像名(filename)、图像宽(w)、图像高(h)、图像通道数(comp)、图像数据指针(data),步长(stride_in_bytes,若为0则为宽*通道数,仅限png)、图像质量(quality,取值范围1~100,仅限jpg),此函数正常返回非0值,否则返回0。

以下为测试代码(test_stb.cpp):

#include "funset.hpp"
#include <iostream>
#include <vector>
#include <string>#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_STATIC
#include "stb_image_resize.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_STATIC
#include "stb_image_write.h"int test_stb_image()
{
#ifdef _MSC_VERconst std::string files_path {"E:/GitCode/OCR_Test/test_data/"};
#elseconst std::string files_path {"test_data/"};
#endifconst std::vector<std::string> images_name{"marge.jpg", "lena.png"};for (auto name : images_name) {const std::string image = files_path + name;// load imageint x, y, channels_in_file, desired_channels = 3;unsigned char* data = stbi_load(image.c_str(), &x, &y, &channels_in_file, desired_channels);if (!data) {fprintf(stderr, "fail to read image: %s\n", image.c_str());return -1;}fprintf(stdout, "image: %s, x: %d, y: %d, channels_in_file: %d, desired_channels: %d\n", name.c_str(), x, y, channels_in_file, desired_channels);// resize imageint width_resize = x * 1.5, height_resize = y * 1.4;unsigned char* output_pixels = (unsigned char*)malloc(width_resize * height_resize * desired_channels);int ret = stbir_resize_uint8(data, x, y, 0, output_pixels, width_resize, height_resize, 0, desired_channels);if (ret == 0) {fprintf(stderr, "fail to resize image: %s\n", image.c_str());return -1;}// write(save) imageconst std::string save_name_png = image + ".png";const std::string save_name_jpg = image + ".jpg";ret = stbi_write_png(save_name_png.c_str(), width_resize, height_resize, desired_channels, output_pixels, 0);if (ret == 0) {fprintf(stderr, "fail to write image png: %s\n", image.c_str());return -1;}ret = stbi_write_jpg(save_name_jpg.c_str(), width_resize, height_resize, desired_channels, output_pixels, 90);if (ret == 0) {fprintf(stderr, "fail to write image jpg: %s\n", image.c_str());return -1;}free(data);free(output_pixels);}return 0;
}

执行结果如下图所示:

GitHub:https://github.com/fengbingchun/OCR_Test

开源库nothings/stb的介绍及使用(图像方面)相关推荐

  1. 人脸识别开源库face_recognition的简单介绍

    人脸识别开源库face_recognition的简单介绍 原文出处: https://blog.xugaoxiang.com/ai/face-recognition-cnn.html 软硬件环境 ub ...

  2. GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    本文转载于:https://github.com/Freelander/Android_Data/blob/master/Android-Librarys-Top-100.md 本项目主要对目前 Gi ...

  3. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  4. C++ 开源库,很完整介绍【转】

    [转]http://blog.csdn.net/lixingshi/article/details/22714783 向C++初学者推荐的几个开源库 标签:  STL  boost  SDL  wxW ...

  5. 几种常见的java开源库,及其功能介绍

    1.Commons Math 是Apache上一个轻量级自容器的数学和统计计算方法包,包含大多数常用的数值算法. 2.LWJGL(Lightweight Java Game Library)可以帮助J ...

  6. GitHub 上排名前 100 的 Android 开源库介绍

    转自:http://www.codeceo.com/article/github-top-100-android-libs.html 本项目主要对目前 GitHub 上排名前 100 的 Androi ...

  7. GitHub上排名前100的Android开源库介绍

    摘要: 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍,至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果,然 ...

  8. 排名前100的Android开源库

    本项目主要对目前GitHub上排名前100的Android开源库进行简单的介绍,至于排名完全是根据GitHub搜索Java语言选择「BestMatch」得到的结果,然后过滤了跟Android不相关的项 ...

  9. android 离线文字识别开源库 tesseract

    前言 离线文字识别开源库,本文只介绍如何使用 tess-two github地址: https://github.com/rmtheis/tess-two 导入依赖 implementation 'c ...

最新文章

  1. ICML2021|超越SE、CBAM,中山大学开源SAM:无参Attention!
  2. Hyper-v和VMware 兼容问题
  3. 行为模型:客户行为智能分析模型
  4. 使用SecureCRT时屏幕僵死的处理方法——Linux终端设置技巧
  5. Android Webview实现有道电子词典
  6. 查找两个单词链表共同后缀的起始结点(C++,单链表/双向链表解法)
  7. mysql sql优化_Mysql的SQL优化指北
  8. c++11 线程的互斥量
  9. 小程序快速入门:坏境和生命周期
  10. 数学建模比赛需要那些c语言的知识,数学建模需要掌握哪些编程语言和技术
  11. VTD场景搭建指南-搭建一个最简单的场景
  12. 使用 Electron 打印到 PDF
  13. Hexo NexT主题自定义背景图片
  14. 2022-nc-Widespread increasing vegetation sensitivity to soil moisture
  15. android 进退分屏代码策略,Android的分屏模式开发注意事项
  16. 2017车载后市场年终回顾:天下武功,唯快不破
  17. 微信端视频播放时防止被浏览器劫持的问题
  18. 一文读懂keepalive的工作原理
  19. 如何自学插画?零基础要知道的技巧!
  20. 抢滩大数据金融“蓝海”

热门文章

  1. 图像分割:Python的SLIC超像素分割
  2. PyTorch框架:(4)如何去构建数据
  3. 【MediaPipe】(4) AI视觉,远程手势调节电脑音量,附python完整代码
  4. MathType6.9b安装及在Word2013中无法正常使用的解决方法
  5. linux支持hd610显卡吗,HD610相当于什么显卡 HD610和HD630的区别 (全文)
  6. 在macOS 10.13.6下安装Grafana实录
  7. Linux上chown命令的高级用法
  8. 使用C++ stringstream来进行数据类型转换
  9. GameMaker Studio从头开始学习设计和开发3款游戏
  10. Rocksdb 写入数据后 GetApproximateSizes 获取的大小竟然为0?