矩阵的掩码操作:根据掩码矩阵重新计算矩阵中每个像素的值。从数学观点看,利用自己设置的权值,对
像素邻域内的值做了加权平均。突出像素点,图片有了锐化的效果。
对图像的每个像素应用下列公式
I(i,j)=5*I(i,j)-I(i+1,j)-I(i,j+1)-I(i-1,j)-I(i,j-1)
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
void CreateLookupTable(uchar* table, uchar divideWith);
Mat& ScanImageAndReduceC(Mat& I, const uchar* table);
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* table);
int main() {
Mat I, J;
I = imread("D:\\OpenCVT\\4.jpg", CV_LOAD_IMAGE_COLOR);
if (!I.data) {
cout << "The image could not be loaded" << endl;
return -1;
}
Mat Kernel = (Mat_(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(I, J, I.depth(), Kernel);
namedWindow("源图像", CV_WINDOW_NORMAL);
imshow("源图像", I);
namedWindow("矩阵掩码操作后", CV_WINDOW_NORMAL);
imshow("矩阵掩码操作后", J);
waitKey(0);
return 0;
}
void CreateLookupTable(uchar* table, uchar divideWith) {
for (int i = 0; i < 256; i++) {
table[i] = (i / divideWith)*divideWith;
}
}
Mat& ScanImageAndReduceC(Mat& I, const uchar* table) {
//检测只能为uchar类型
CV_Assert(I.depth() != sizeof(uchar));
int channels = I.channels();
int nRows = I.rows*channels;
int nCols = I.cols;
if (I.isContinuous()) {
nCols*= nRows;
nRows = 1;
}
int i, j;
uchar *p;
for (i = 0; i < nRows; ++i) {
p = I.ptr(i);
for (j = 0; j < nCols; ++j) {
p[j] = table[p[j]];
}
}
return I;
}
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table) {
CV_Assert(I.depth() != sizeof(uchar));
const int channels = I.channels();
switch (channels) {
case 1: {
MatIterator_it, end;
for (it = I.begin(), end = I.end(); it != end; ++it) {
*it = table[*it];
}
break;
}
case 3: {
MatIterator_it, end;
for (it = I.begin(), end = I.end(); it != end; ++it) {
(*it)[0] = table[(*it)[0]];
(*it)[1] = table[(*it)[1]];
(*it)[2] = table[(*it)[2]];
}
}
}
return I;
}
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* table) {
return I;
}