使用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句偈语,悟透一句,福德无量
科学家的故事——核物理学家邓稼先
孩子为什么总爱粘着妈妈?妈妈该如何应对?
力量训练时的呼吸方法:如何通过正确呼吸提升训练效果
写作中连接词有哪些
C语言输出时如何使用换行符
茵陈:古草新韵,医食双绝
90年代台湾经典武侠剧大盘点:古龙、金庸各占鳌头
陕西六大王牌景点,秦始皇兵马俑第二,第1名实至名归,你去过吗
时间的本质是什么?柏格森为什么认为时间的本质是绵延?
幼儿园温馨提示:初春入园幼儿穿衣指南,建议发给家长!
神话何以“神话”
交通系统绩效评价文件的构建指南
米面油选购指南:执行标准号的真相与选购要点
与大型建筑大小相当的小行星可能会在未来十年内撞击地球
农历生日计算器:虚岁计算方法详解
晾衣架常见故障问题及解决方法
一年之计在于春,春季养生四大原则详解
乔丹私人训练师揭秘:12周篮球体能训练计划将如何改变你的比赛表现
慢性非萎缩性胃炎中成药有哪些
胆结石忌讳的食物是什么
面部皮炎激素药怎么停
管理处处要谈判
妊娠期糖尿病患者的日常活动指南
消防设施操作员分几级 分别是什么
客户信用调查:如何全面审查客户信用记录及财务状况?
企业如何应对内部员工带来的网络安全风险
探索中国的美食天堂及其文化意义
央视警告!这4种春菜吃不对,可能变成“慢性毒药”!
中级消防设施操作员证书有效期是多长时间