开源库nothings/stb的介绍及使用(图像方面)
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的介绍及使用(图像方面)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海思3559A上编译Valgrind源码
- 下一篇: Linux下gdb attach的使用(