使用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);
}
热门推荐
电路故障解析:什么是短路?原理、危害及预防措施全解
今秋必看!避开这3个牛仔裤搭配雷区,轻松变身时尚达人!
揭秘车辆真实公里数查询,三招轻松搞定
新生儿的生理反射及意义
如何保养烤漆家具?烤漆家具保养的要点有哪些?
急!全国仅此一株的银丝贯顶牡丹盛开,只有30秒拍照机会!
70岁奶奶做的豆角烧茄子,少油腻味道香,一周3次不够吃,特下饭
让全日本恐惧的“南海海槽大地震”,破坏性有多大?
《院子楼梯风水讲究:布局与禁忌揭秘》
项目管理流程图解:简化复杂任务
汽车维护与保养的目的和意义
《典韦攻略与出装指南》(了解典韦的技能特点和最佳装备选择)
汽车公里数的计算方法及意义:从仪表盘到车辆估价全解析
春风拂面,送你最温暖的人间三月天
DNS安全的主要关注点及防护措施
《雷雨》中周朴园的人物形象分析
笔记本电脑电池保养方法有哪些?
血压高应该挂什么科室就诊
感染新冠后期咳嗽不止,得了新冠咳嗽得厉害怎么办
兰花水培养护指南,新手必看的水培方法详解
猫咪爱梳毛是天性,也有清洁和放松作用。
元朝是朱元璋的死敌,为何朱元璋对元廷以礼相待?
法国媒体眼中的福建舰:排水量或达10万吨,最大载机数可达85架
病例分享:这样的磨玻璃结节靠边又伴血管进入,随访过仍在,还是切了吧!
电子烟危害小吗?它对身体的伤害,可能超出你的想象!
面向开发人员的3D建模基础知识
【以案释法】非婚生子女应该由谁来抚养?权益如何保障?
孕妇得了过敏性结膜炎怎么办
《清代地方志中的中药茯苓产地(顺治至嘉庆)》知识库发布
近百位孙中山亲属后人寻根,见证“振兴中华”设想正逐步走向现实