当前位置:网站首页>图像像素读写操作
图像像素读写操作
2022-07-07 10:33:00 【什么时候上岸?】
示例代码如下:
void demo::pixel_demo(Mat &image)
{
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if (dims == 1)//灰度图像
{
int p = image.at<uchar>(i, j);
image.at<uchar>(i, j) = 255 - p;
}
if (dims == 3)//彩色图像
{
Vec3b g = image.at<Vec3b>(i, j);
image.at<Vec3b>(i, j)[0] = 255 - g[0];
image.at<Vec3b>(i, j)[1] = 255 - g[1];
image.at<Vec3b>(i, j)[2] = 255 - g[2];
}
}
}
imshow("像素图像", image);
}
这次主要是进一步学习Mat(矩阵)
通过image.at<uchar>(i,j)可以获得图像image在位置(i,j)的像素大小,因为灰度图像像素点只有一个值,uchar表示一个unsigned char 的大小。
在opencv中Vec3b表示三个数的列表,也存在Vec3i和Vec3f。
结果如下:
下面是用指针的方式改变像素
void demo::pixel_demo(Mat &image)
{
int w = image.cols;
int h = image.rows;
int dims = image.channels();for (int i = 0; i < h; i++)
{
uchar*p = image.ptr<uchar>(i);
for (int j = 0; j < w; j++)
{
if (dims == 1)
{
*p++ = 255 - *p;//先赋值再加加
}
if (dims == 3)
{
*p++ = 255 - *p;//先赋值再加加
*p++ = 255 - *p;//先赋值再加加
*p++ = 255 - *p;//先赋值再加加
}
}
}
imshow("像素图像", image);}
结果相同:
over!!!
边栏推荐
- On valuation model (II): PE index II - PE band
- EPP+DIS学习之路(2)——Blink!闪烁!
- The left-hand side of an assignment expression may not be an optional property access. ts(2779)
- leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
- Completion report of communication software development and Application
- Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
- [statistical learning methods] learning notes - Chapter 4: naive Bayesian method
- Epp+dis learning path (1) -- Hello world!
- Realize all, race, allsettled and any of the simple version of promise by yourself
- Experiment with a web server that configures its own content
猜你喜欢
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
<No. 9> 1805. Number of different integers in the string (simple)
Attack and defense world - PWN learning notes
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Session
Learning and using vscode
idea 2021中文乱码
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Epp+dis learning road (2) -- blink! twinkle!
JS to convert array to tree data
随机推荐
Inverted index of ES underlying principle
VSCode的学习使用
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
静态Vxlan 配置
idm服务器响应显示您没有权限下载解决教程
Tutorial on the principle and application of database system (011) -- relational database
Realize a simple version of array by yourself from
盘点JS判断空对象的几大方法
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Solutions to cross domain problems
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
Cenos openssh upgrade to version 8.4
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
IPv6 experiment
[statistical learning methods] learning notes - Chapter 5: Decision Tree
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
SQL head injection -- injection principle and essence
How to use PS link layer and shortcut keys, and how to do PS layer link
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Tutorial on principles and applications of database system (007) -- related concepts of database