OpenCV Lesson 3 : Mask operations on matrices

矩阵上的掩码运算

Mask operations on matrices are quite simple. The idea is that we recalculate each pixel's value in an image according to a mask matrix (also known as kernel). This mask holds values that will adjust how much influence neighboring pixels (and the current pixel) have on the new pixel value. From a mathematical point of view we make a weighted average, with our specified values.

矩阵上的掩模运算非常简单。这个想法是我们根据掩模矩阵(也称为内核)重新计算图像中每个像素的值。该掩码保存的值将调整相邻像素(和当前像素)对新像素值的影响程度。从数学的角度来看,我们使用指定的值进行加权平均值。

Let's consider the issue of an image contrast enhancement method.

让我们考虑图像对比度增强方法的问题。

I ( i , j ) = 5 ∗ I ( i , j ) − [ I ( i − 1 , j ) + I ( i + 1 , j ) + I ( i , j − 1 ) + I ( i , j + 1 ) ] I(i,j) = 5 * I(i,j) - [I(i-1, j) + I(i+1, j)+I(i,j-1)+I(i,j+1)] I(i,j)=5∗I(i,j)−[I(i−1,j)+I(i+1,j)+I(i,j−1)+I(i,j+1)]

⟺    I ( i , j ) ∗ M , w h e r e M = i / j − 1 0 + 1 − 1 0 − 1 0 0 − 1 5 − 1 + 1 0 − 1 0 \iff I(i,j)*M, where M = \begin{matrix} {i \ / j} & -1 & 0 & +1 \\ -1 & 0 & -1 & 0 \\ 0 & -1 & 5 & -1 \\ +1 & 0 & -1 & 0 \end{matrix} ⟺I(i,j)∗M,whereM=i /j−10+1−10−100−15−1+10−10

The first notation is by using a formula, while the second is a compacted version of the first by using a mask. You use the mask by putting the center of the mask matrix (in the upper case noted by the zero-zero index) on the pixel you want to calculate and sum up the pixel values multiplied with the overlapped matrix values. It's the same thing, however in case of large matrices the latter notation is a lot easier to look over.

第一个表示法是使用公式,而第二个表示法是使用掩码的第一个表示法的压缩版本。您可以通过将掩码矩阵的中心(以零零索引表示的大写字母)放在要计算的像素上来使用掩码,并将像素值与重叠矩阵值相乘求和。这是同样的事情,但是在大型矩阵的情况下,后一种表示法更容易查看。

Built-in filter2D

First, we load one image

bash 复制代码
cv::Mat dst0, dst1;
cv::Mat src = imread( "Lena.png", IMREAD_GRAYSCALE);

Then, we need a kernel

bash 复制代码
    Mat kernel = (Mat_<char>(3,3) <<  0, -1,  0,
                                   -1,  5, -1,
                                    0, -1,  0);

Finally, do filter2D

bash 复制代码
filter2D( src, dst1, src.depth(), kernel );

Hand written

bash 复制代码
void Sharpen(const Mat& myImage,Mat& Result)
{
    CV_Assert(myImage.depth() == CV_8U);  // accept only uchar images
 
    const int nChannels = myImage.channels();
    Result.create(myImage.size(),myImage.type());
 
    for(int j = 1 ; j < myImage.rows-1; ++j)
    {
        const uchar* previous = myImage.ptr<uchar>(j - 1);
        const uchar* current  = myImage.ptr<uchar>(j    );
        const uchar* next     = myImage.ptr<uchar>(j + 1);
 
        uchar* output = Result.ptr<uchar>(j);
 
        for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
        {
            output[i] = saturate_cast<uchar>(5*current[i]
                         -current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
        }
    }
 
    Result.row(0).setTo(Scalar(0));
    Result.row(Result.rows-1).setTo(Scalar(0));
    Result.col(0).setTo(Scalar(0));
    Result.col(Result.cols-1).setTo(Scalar(0));
}

Use getTickCount(), and getTickFrequency() get the time passed

bash 复制代码
t = ((double)getTickCount() - t)/getTickFrequency();
相关推荐
秦明月135 分钟前
【原创教程】自动化工程案例01:8工位插针装配机02
人工智能·机器人·自动化
我可以将你更新哟7 分钟前
【OpenCV-图像形态学操作】礼帽与黑帽、梯度运算、开运算与闭运算、形态学-膨胀操作、形态学-腐蚀操作
人工智能·opencv·计算机视觉
路人甲ing..18 分钟前
openVX加速-常见问题:适用场景、AI加速、安装方式等
c++·图像处理·人工智能·图论·openvx
FL162386312919 分钟前
[数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别
人工智能·yolo·目标检测
jndingxin19 分钟前
OpenCV结构分析与形状描述符(21)计算包围给定点集的最小面积三角形函数minEnclosingTriangle()的使用
人工智能·opencv·计算机视觉
Q83431581934 分钟前
海思SD3403(21AP10, 108DC2910 )4K60 的 ISP 图像处理能力,4Tops INT8算力
linux·图像处理·人工智能·嵌入式硬件·视觉检测·视频编解码
机器不会学习CL39 分钟前
分类预测|基于麻雀优化支持向量机的Adaboost集成的数据分类预测Matlab程序SSA-SVM-Adaboost
人工智能·算法·机器学习·支持向量机·matlab·分类
科技资讯快报1 小时前
容联云容犀Copilot&Agent入选《中国 AI Agent 产品罗盘》
大数据·人工智能·copilot
爱研究的小牛1 小时前
ESRGAN——老旧照片、视频帧的修复和增强,提高图像的分辨率
人工智能·深度学习·自动化·aigc·音视频
ClonBrowser1 小时前
社交媒体的未来:Facebook如何通过AI技术引领潮流
人工智能·媒体·facebook