使用STB库在C++中操作JPG图片
创作时间:
作者:
@小白创作中心
使用STB库在C++中操作JPG图片
引用
CSDN
1.
https://blog.csdn.net/weixin_51133173/article/details/137955624
本文将介绍如何使用STB库在C++中操作JPG图片。通过具体的代码示例,我们将展示如何读取图片、获取像素值、保存图片以及修改像素值等基本操作。
测试图
s_l.jpg
读图
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
}
宽: 400
高: 328
channel: 3
取像素值
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
cout << "宽: " << w << endl;
cout << "高: " << h << endl;
cout << "channel: " << n << endl;
cout << "---------\n";
int x=200,y=100;
cout << data[y*w*n + x*n + 0] << endl;
cout << data[y*w*n + x*n + 1] << endl;
cout << data[y*w*n + x*n + 2] << endl;
cout << "---------\n";
cout << int(data[y*w*n + x*n + 0]) << endl;
cout << int(data[y*w*n + x*n + 1]) << endl;
cout << int(data[y*w*n + x*n + 2]) << endl;
}
宽: 400
高: 328
channel: 3
---------
^
7
(
---------
94
55
40
保存图片
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
修改像素值
中间-1列
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 255;
data[w*i*n+red_col*n+1] = 0;
data[w*i*n+red_col*n+2] = 0;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 0;
data[w*i*n+red_col*n+1] = 255;
data[w*i*n+red_col*n+2] = 0;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_col = w/2;
for(int i=0; i<h; i++)
{
data[w*i*n+red_col*n+0] = 0;
data[w*i*n+red_col*n+1] = 0;
data[w*i*n+red_col*n+2] = 255;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
中间-1行
红色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_row = h/2;
for(int i=0; i<w; i++)
{
data[red_row*w*n+i*n+0] = 255;
data[red_row*w*n+i*n+1] = 0;
data[red_row*w*n+i*n+2] = 0;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
绿色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_row = h/2;
for(int i=0; i<w; i++)
{
data[red_row*w*n+i*n+0] = 0;
data[red_row*w*n+i*n+1] = 255;
data[red_row*w*n+i*n+2] = 0;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
蓝色
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
using namespace std;
int main()
{
int w,h,n;
unsigned char *data = stbi_load("./s_l.jpg", &w, &h, &n, 0);
int red_row = h/2;
for(int i=0; i<w; i++)
{
data[red_row*w*n+i*n+0] = 0;
data[red_row*w*n+i*n+1] = 0;
data[red_row*w*n+i*n+2] = 255;
}
string name = "s_l_new.jpg";
stbi_write_jpg(name.c_str(), w,h,n,data,95);
}
热门推荐
植物奶能否取代牛奶?解構5種植物奶的營養成分
怎么把excel里是数据转到word
强基计划是什么意思?附强基的好处和坏处
公积金提取指南:次数限制与办理流程详解
宁波什么季节去玩最好? 宁波适合几月份去?
2024年全国31省份GDP排名:广东连续36年总量第一,山西增速最低
Spring中HttpServletRequest的注入机制解析
量化投资中的复权数据详解
从股息再投资视角分析贵州茅台拆股
1000元打造高性价比电脑:硬件配置与预算分配指南
做鱼清蒸好吃还是红烧好吃?搞清楚这4个关键,你也能正确做鱼
最全面最详细的字符集讲解来了!
顶楼露台设计,打造城市中的空中花园
动态域名解析DDNS的工作原理及配置方法
如何设置电脑配置以运行4k画质?
你肯定不知道这些关于海马的事实
观看:雄性海马为什么会生育?
什么是谐波?谐波有什么危害?如何治理谐波?
翻新CPU性能实测对比:揭开真相的面纱
领袖们尊师敬师的故事
想学好中国山水画的六步法
Qt事件处理机制详解:从基础到高级自定义
鼻窦炎用喷雾剂好不好
如何检测AI生成内容:从文本到图像的全面指南
首次使用回声状态网络 (ESN) 和语音特征进行帕金森病 (PD) 预测
心灵的深度对话:探索家庭教育中亲子间非言语沟通的力量
事故发生后未及时报险、报警,保险公司可以拒赔吗?
做项目如何和业主沟通
项目业主是什么意思?含义与角色介绍
龙吐珠的扦插繁殖方式