使用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);
}
热门推荐
苏姓历史溯源:深入了解苏姓的起源与演变
马斯克的思维方式:第一性原理(118页PPT解读)
如何梳理专业文献数据库
李佳佳:杭州六小龙与钱学森之问
月亮岛街道杨丰社区流动党员"三转变"工作方法:为社区发展注入源头活水
胖东来停售蒙牛伊利背后:一场关于“标准、利润与信任”的零售暗战
如何打造一个同心的团队
突触可塑性与STDP:神经网络中的自我调整机制
如何深入了解期货市场的交易策略优化?这些优化如何提升交易效果?
全球痛风研究进展:新疗法与病理机制的深度剖析
2025年集成电路行业现状与发展趋势分析
患了甲亢,医生却开了甲减药?左甲状腺素钠片,你吃对了吗?
如何学习战略性思维
买卖犀牛角是否违法?了解相关法律规定
Spring的基本概念和结构
住建部明确2025年工作重点,现房销售还有多远?
日语培训网站的课程证书有用吗?
GB 4943标准中的电气安全和隔离要求
怎么使excel重复数字标红
千年古都的“绿野仙踪”——西安建设公园城市见闻
古今名人珍惜时间的励志小故事7篇
黄金空头出现!分析师警告未来五年价格或跌去38%
挂耳咖啡的选择与冲泡方式
胃反酸烧心时应避免的六类食物
纪录片宣称揭秘“比特币之父”真实身份,当事人坚决否认
樱桃的授粉时间与方式方法详解(如何让樱桃更加丰收?-樱桃授粉技巧分享)
快充隐患与慢充优势:超算充电桩如何守护新能源汽车电池健康
甲状腺结节能否喝咖啡
火花塞可以自己换吗?很多车主不清楚,内行人告诉你真相!
图片无法导入Photoshop?这5个原因及解决方案帮你轻松应对