OpenCV快速垫元和邻居访问 [英] OpenCV fast mat element and neighbour access

查看:138
本文介绍了OpenCV快速垫元和邻居访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV(C ++)Mat为我的矩阵,并希望尽快访问单个Mat元素。从OpenCV教程,我找到了有效的访问代码:

I use OpenCV (C++) Mat for my matrix and want to acces single Mat elements as fast as possible. From OpenCV tutorial, I found code for efficient acces:

for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            p[j] = table[p[j]];
        }
    }

对于我的问题,我需要访问一个Mat元素及其邻居(i-1,j-1)进行计算。我如何适应给定的代码访问单个垫元素及其周围的元素?由于速度很重要,我想避免 Mat.at<>()
什么是最有效的方法来获取Mat值及其邻域值?

For my problem, I need to access a Mat element and its neighbours (i-1,j-1) for a calculation. How can I adapt the given code to acces a single mat element AND its surrounding elements? Since speed matters, I want to avoid Mat.at<>(). What is the most efficient way to acces a Mat value and its neighbour values?

推荐答案

像素及其邻域像素可以形成一个 cv :: Rect ,那么你可以简单地使用:

The pixel and its neighbor pixels can be formed a cv::Rect, then you can simply use:

cv::Mat mat = ...;
cv::Rect roi= ...; // define it properly based on the neighbors defination
cv::Mat sub_mat = mat(roi);

如果邻居定义不规则,即它们不能形成一个矩形区域,请查看此处的示例。

In case your neighbors definition is not regular, i.e. they cannot form a rectangle area, use mask instead. Check out here for examples.

这篇关于OpenCV快速垫元和邻居访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆