• 1.3矩阵的掩码操作


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

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

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

    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;
    }
    

      

  • 相关阅读:
    关于程序员认知和编程学习,没有任何一篇文章会讲得如此透彻
    Found 1 slaves: Use of uninitialized value in printf at /usr/local/percona-toolkit/bin/pt-online-schema-change line 8489
    alert 多语言的处理
    #!/bin/sh & #!/bin/bash区别
    mysql 常用
    java.io.FileNotFoundException
    struts1 & jquery form 文件异步上传
    简单的数据库连接池实例(java语言)
    null id in com.rocky.** entry 错误处理
    java unsupported major.minor version 51.0 解决
  • 原文地址:https://www.cnblogs.com/zuoyou151/p/9577007.html
Copyright © 2020-2023  润新知