当前位置:网站首页>图像像素读写操作
图像像素读写操作
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!!!
边栏推荐
- <No. 9> 1805. Number of different integers in the string (simple)
- 平安证券手机行开户安全吗?
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- Attack and defense world ----- summary of web knowledge points
- BGP third experiment report
- File upload vulnerability - upload labs (1~2)
- Tutorial on principles and applications of database system (009) -- conceptual model and data model
- 编译 libssl 报错
- EPP+DIS学习之路(2)——Blink!闪烁!
- gcc 编译报错
猜你喜欢
[pytorch practice] use pytorch to realize image style migration based on neural network
leetcode刷题:二叉树19(合并二叉树)
Session
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
leetcode刷题:二叉树20(二叉搜索树中的搜索)
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
Cookie
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
How to use PS link layer and shortcut keys, and how to do PS layer link
随机推荐
AirServer自动接收多画面投屏或者跨设备投屏
30. Feed shot named entity recognition with self describing networks reading notes
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
ES底层原理之倒排索引
[pytorch practice] use pytorch to realize image style migration based on neural network
Completion report of communication software development and Application
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Utiliser la pile pour convertir le binaire en décimal
Attack and defense world ----- summary of web knowledge points
idea 2021中文乱码
[pytorch practice] image description -- let neural network read pictures and tell stories
ENSP MPLS layer 3 dedicated line
【统计学习方法】学习笔记——支持向量机(下)
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
<No. 9> 1805. Number of different integers in the string (simple)
EPP+DIS学习之路(2)——Blink!闪烁!
Processing strategy of message queue message loss and repeated message sending
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
@What happens if bean and @component are used on the same class?