OpenCV图像处理03-矩阵的掩膜操作

03-矩阵的掩膜操作

#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main() {
	Mat src, dst;
	src = imread("D:\\heroRcData\\opencvProject\\arrowImg\\01\\01.jpg");
	if (!src.data) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);
	//***********************************************************************************

	int cols = src.cols * src.channels();//输入图像总列数 
	int rows = src.rows;//输入图像行数
	int channels = src.channels();//输入图像通道数
	dst = Mat::zeros(src.size(), src.type());//初始化输出矩阵
	for (int i = 1; i < rows - 1; i++) {//边缘的不能进行掩膜操作 故i,j不取边界值
		const uchar* current = src.ptr<uchar>(i);	//当前行的像素指针
		const uchar* previous = src.ptr<uchar>(i - 1);//上一行的像素指针
		const uchar* next = src.ptr<uchar>(i + 1);//下一行的像素指针
		uchar* output = dst.ptr<uchar>(i);	//输出图像当前行的像素指针
		for (int j = 1; j < cols - 1; j++) {
			//使用公式提高图像对比度:输出图像中该像素点的值 = 5*原图像该像素点值 - (原图像上下左右四个像素点值的和)
			//saturate_cast<uchar>的作用是将<0的像素值映射为0 >255的像素值映射为255 0~255之间的像素值不变 从而确保像素值在0~255范围内
			output[j] = saturate_cast<uchar>(5 * current[j] - (previous[j] + next[j] + current[j - channels] + current[j + channels]));
		}
	}
	namedWindow("output image", CV_WINDOW_AUTOSIZE);
	imshow("output image", dst);
	//***********************************************************************************

	double t = getTickCount();//用于后续求得运行时间 【标记1】
	//下面是使用filter2Da函数实现上述效果
	Mat dst1;
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); //定义掩膜
	filter2D(src, dst1, src.depth(), kernel); //src.depth()是图像深度 可以用-1代替
	namedWindow("output1 image", CV_WINDOW_AUTOSIZE);
	imshow("output1 image", dst1);
	
	double timeConsume = (getTickCount() - t) / getTickFrequency();//从【标记1】到现在消耗的时间
	cout << "time consume " << timeConsume << endl;

	waitKey(0);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值