图像的掩膜操作
[TOC]
注:原創(chuàng)不易,轉(zhuǎn)載請務(wù)必注明原作者和出處,感謝支持!
所涉及的API
void cv::filter2D(InputArray src, // 輸入圖像OutputArray dst, // 輸出圖像int ddepth, // 輸出圖像深度InputArray kernel, // 掩膜矩陣(核)Point anchor = Point(-1, -1),double delta = 0,int borderType = BORDER_DEFAULT );圖像的掩膜操作
什么是圖像的掩膜操作?
掩膜操作是指根據(jù)掩膜矩陣(也稱作核kernel)重新計算圖像中每個像素的值。掩膜矩陣中的值表示了鄰近像素值(包括該像素自身的值)對新像素值有多大的影響。從數(shù)學(xué)的觀點(diǎn)來看,我們用自己設(shè)置的權(quán)值,對像素領(lǐng)域內(nèi)的值做了個加權(quán)平均。
比如,下面這個公式表示用5倍當(dāng)前像素的值減去該像素上、下、左、右四個像素值和,得到的結(jié)果賦值給當(dāng)前像素。使用該公式可以用于提升圖像的對比度。調(diào)節(jié)$I(i,j)$的系數(shù)權(quán)重可以得到不同的對比度提升效果。
\[ I(i, j) = 5 * I(i, j) - [I(i-1, j) + I(i+1, j) + I(i, j-1) + I(i, j+1)] \]上面的公式可以用掩膜矩陣表示成如下的形式。
\[ \begin{bmatrix} 0&-1&0\\ -1&5&-1\\ 0&-1&0 \end{bmatrix} \]如果不使用OpenCV提供的API,直接利用OpenCV提供的對圖像像素的訪問功能,則上述公式所對應(yīng)的掩膜操作可以用下面的代碼實(shí)現(xiàn)。
#include <iostream> #include <opencv2/opencv.hpp>using namespace cv; using namespace std;int main(int argc, char **argv) {// load image and showMat src = imread("D:\\IMG\\lena.jpg", IMREAD_COLOR);if (!src.data){cout << "Error : could not load image." << endl;return -1;}imshow("input image", src);// image sharpenauto rows = src.rows;auto cols = src.cols;auto channels = src.channels();Mat dst = Mat::zeros(src.size(), src.type());decltype(src.rows) row, col;for (row = 1; row < (rows - 1); ++row){// get points pointed to rows of the source imageconst uchar *prev = src.ptr<uchar>(row - 1);const uchar *curr = src.ptr<uchar>(row);const uchar *next = src.ptr<uchar>(row + 1);uchar *output = dst.ptr<uchar>(row);for (col = channels; col < ((cols - 1) * channels); ++col){output[col] = saturate_cast<uchar>(7 * curr[col] - curr[col - channels]- curr[col + channels] - prev[col] - next[col]);}}imshow("sharpen image", dst);waitKey(0);return 0; }如果使用filter2D()則可以將上述公式作如下的實(shí)現(xiàn)。
// by filter2D() Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 9, -1, 0, -1, 0); filter2D(src, dst, src.depth(), kernel);對于上述兩種實(shí)現(xiàn)方式,有一些需要注意的地方: (1) 在不使用API直接手寫實(shí)現(xiàn)方式中,圖像最外邊的一圈像素將會是黑色的。因?yàn)楹说拇笮∈?x3的,無法對圖像的最邊緣一圈像素進(jìn)行掩膜計算。 (2) 使用APIfilter2D()通常的性能會比自己手寫更好。因?yàn)镺penCV針對API的性能進(jìn)行了很好的優(yōu)化。
實(shí)現(xiàn)效果
原圖
直接手寫實(shí)現(xiàn),I(i,j)的權(quán)重分別為5, 7, 9
- 使用filter2D實(shí)現(xiàn),I(i,j)的權(quán)重分別為5, 7, 9
轉(zhuǎn)載于:https://www.cnblogs.com/laizhenghong2012/p/11251593.html
總結(jié)
- 上一篇: PIE SDK剔除栅格块算法
- 下一篇: 从List分组后重新组织数据