日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時(shí)間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 开源库nothings/stb的介绍及使用(图像方面) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

GitHub上有個(gè)開(kāi)源的stb庫(kù),Star數(shù)已過(guò)萬(wàn),地址為https://github.com/nothings/stb,為何叫stb,是用的作者名字的縮寫(xiě)Sean T. Barrett。此庫(kù)僅包含頭文件,除stretchy_buffer.h外,其它所有文件以前綴stb開(kāi)頭,每個(gè)頭文件的作用及用法在每個(gè)頭文件的開(kāi)始部分都作了介紹。此開(kāi)源庫(kù)的license為public domain或MIT。下面僅對(duì)與圖像相關(guān)的頭文件的作用及使用進(jìn)行簡(jiǎn)單的說(shuō)明,當(dāng)僅需要將圖像數(shù)據(jù)載入內(nèi)存、或進(jìn)行縮放操作、或保存圖像時(shí)使用stb會(huì)非常方便,因?yàn)閮H需要include一個(gè)或三個(gè)頭文件即可,不需要額外圖像處理庫(kù)的依賴,如libjpeg、libpng、opencv等:

1. stb_image.h:載入圖像,支持的圖像文件格式包括JPEG、PNG、TGA、BMP、PSD、GIF、HDR、PIC、PNM,使用到的函數(shù)主要為:stbi_load,參數(shù)依次為:圖像文件名(filename),獲取圖像寬(x),獲取圖像高(x),獲取圖像通道數(shù)(channels_in_file)、指定期望的通道數(shù)(desired_channels,若為0則不做顏色空間變換),此函數(shù)正常返回圖像數(shù)據(jù)指針,否則返回NULL;

2. stb_image_resize.h:圖像縮放,使用到的函數(shù)主要為stbir_resize_uint8,參數(shù)依次為:輸入圖像數(shù)據(jù)指針(input_pixels)、輸入圖像寬(input_w)、輸入圖像高(input_h)、輸入圖像步長(zhǎng)(input_stride_in_bytes,若為0則為寬x通道數(shù))、輸出圖像數(shù)據(jù)指針(output_pixels)、輸出圖像寬(output_w)、輸出圖像高(output_h)、輸出圖像步長(zhǎng)(output_stride_in_bytes,若為0則為寬*通道數(shù))、圖像通道數(shù)(num_channels,輸入與輸出一致),此函數(shù)正常返回1,否則返回0;

3. stb_image_write.h:保存圖像,支持的圖像文件格式包括PNG、BMP、TGA、JPG、HDR,使用到的函數(shù)主要為stbi_write_xxx,其中xxx可以為png、bmp、tga、hdr、jpg,參數(shù)依次為:保存圖像名(filename)、圖像寬(w)、圖像高(h)、圖像通道數(shù)(comp)、圖像數(shù)據(jù)指針(data),步長(zhǎng)(stride_in_bytes,若為0則為寬*通道數(shù),僅限png)、圖像質(zhì)量(quality,取值范圍1~100,僅限jpg),此函數(shù)正常返回非0值,否則返回0。

以下為測(cè)試代碼(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;
}

執(zhí)行結(jié)果如下圖所示:

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

總結(jié)

以上是生活随笔為你收集整理的开源库nothings/stb的介绍及使用(图像方面)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。