博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv Mat指针读取、修改像素值
阅读量:4100 次
发布时间:2019-05-25

本文共 1691 字,大约阅读时间需要 5 分钟。

1、Mat像素的读取:

指针读取像素运行效率高。如果Mat是CV_32F,也只需要将里面的uchar改成float就行了。

//设置灰度图像素for(int i = 0; i < grayFrame.rows; ++i){
uchar *p = grayFrame.ptr
(i); for(int j = 0; j < grayFrame.cols; ++j) {
p[j] = 255;//白色 }}//设置彩色图像素for(int i = 0; i < colorFrame.rows; ++i){
Vec3b *p3 = colorFrame.ptr
(i); for(int j = 0; j < colorFrame.cols; ++j) {
p3[j][0] = 255;//蓝色 p3[j][1] = 0; p3[j][2] = 0; }}

2、逐像素比较两幅图像,将不同之处设为红色

#include
#include
#include
#include
// c function absusing namespace std;using namespace cv;Point2i compete_pixe1(Mat& src1, Mat& src2, int i, int j) {
Vec3b *pixel1 = src1.ptr
(i); Vec3b *pixel2 = src2.ptr
(i); int threshold = 60; Point2i p = Point2i(-1, -1); if (abs(int(pixel1[j][0]) - int(pixel2[j][0])) > threshold && abs(int(pixel1[j][1]) - int(pixel2[j][1])) > threshold && abs(int(pixel1[j][2]) - int(pixel2[j][2])) > threshold) { p.x = i; p.y = j; } return p;}int main() { Mat Image1 = imread("1.jpg"); Mat Image2 = imread("0.jpg"); Mat show_Image = Image1.clone(); int rowNumber = show_Image.rows; int colNumber = show_Image.cols; Point2d diff_p; double time0 = static_cast
(getTickCount()); //逐像素进行比较(RGB) for (int i = 0; i < rowNumber; ++i) { for (int j = 0; j < colNumber; ++j) { diff_p = compete_pixe1(Image1, Image2, i, j); if (diff_p.x != -1 && diff_p.y != -1) { show_Image.ptr
(i, j)[0] = 0; show_Image.ptr
(i, j)[1] = 0; show_Image.ptr
(i, j)[2] = 255; } } } time0 = ((double)getTickCount() - time0) / getTickFrequency(); cout << "运行时间为:" << time0 << "秒" << endl; imshow("效果图", show_Image); waitKey(0); return 0;}

转载地址:http://yqrii.baihongyu.com/

你可能感兴趣的文章
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
200页!分享珍藏很久的Python学习知识手册(附链接)
查看>>
程序员之神
查看>>
4 岁小女孩给 Linux 内核贡献提交
查看>>
推荐几个私藏很久的技术公众号给大家
查看>>
王垠受邀面试阿里 P9,被 P10 面跪后网上怒发文,惨打 325 的 P10 赵海平回应了!...
查看>>
Python 趣味打怪:147 段简单代码助你从入门到大师
查看>>
卧槽!小姐姐用动画图解 Git 命令,这也太秀了吧?!
查看>>
厉害了!Python 编辑器界的神器 Jupyter ,推出官方可视化 Debug 工具!
查看>>
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>
听说玩这些游戏能提升编程能力?
查看>>
7 年工作经验,面试官竟然还让我写算法题???
查看>>
被 Zoom 逼疯的歪果仁,造出了视频会议机器人,同事已笑疯丨开源
查看>>
上古语言从入门到精通:COBOL 教程登上 GitHub 热榜
查看>>
再见,Eclipse...
查看>>
超全汇总!B 站上有哪些值得学习的 AI 课程...
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
神器面世:让你快速在 iOS 设备上安装 Windows、Linux 等操作系统!
查看>>
沙雕程序员在无聊的时候,都搞出了哪些好玩的小玩意...
查看>>
太赞了!GitHub 标星 2.4k+,《可解释机器学习》中文版正式开放!
查看>>