矩阵的掩码操作:根据掩码矩阵重新计算矩阵中每个像素的值。从数学观点看,利用自己设置的权值,对

像素邻域内的值做了加权平均。突出像素点,图片有了锐化的效果。

对图像的每个像素应用下列公式

I(i,j)=5*I(i,j)-I(i+1,j)-I(i,j+1)-I(i-1,j)-I(i,j-1)

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<windows.h>
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_<char>(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<uchar>(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_<uchar>it, end;
		for (it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it) {
			*it = table[*it];
		}
		break;
	}
	case 3: {
		MatIterator_<Vec3b>it, end;
		for (it = I.begin<Vec3b>(), end = I.end<Vec3b>(); 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;
}

  

posted on 2018-09-03 09:46  zuoyou  阅读(834)  评论(0编辑  收藏  举报