图像处理--图像卷积c++底层实现--opencv模块

目录

 

配置

原理知识

卷积核

底层代码

使用封装的代码


配置

关于配置vs2019 的opencv 环境,可以参考这篇博文,传送门

原理知识

关于图像做卷积的原理知识,因为知识很基本就不写了,可以参考这篇博文,传送门

卷积核

底层代码

#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<iostream>
#include<math.h>

using namespace cv;
int main(int argc, char** argv)
{
	Mat gray,dst;
	Mat src = imread("22.jpg");//读取图像
	namedWindow("Window Title", WINDOW_NORMAL);//打开窗口,参数一是窗口名子
	if (src.empty())
	{
		printf("picture is wrong");
	}
	else
	{
		imshow("Window Title", src);//显示图像
		int cols = (src.cols - 1) * src.channels();//获取图像的列
		int offsetx = src.channels();//图像通道数
		int rows = src.rows;//获取图像的行

		dst = Mat::zeros(src.size(),src.type());//生成和原图相等大小的空矩阵
		//遍历图像像素
		for (int row = 1; row<(rows-1); row++)
		{
			const uchar* previous = src.ptr<uchar>(row - 1);//获取上一行指针
			const uchar* current = src.ptr<uchar>(row);//获取当前行指针
			const uchar* next = src.ptr<uchar>(row + 1);//获取下一行指针
			uchar* ouput = dst.ptr<uchar>(row);

			for (int col = offsetx; col < cols; col++)//遍历每一行中像素
			{
				ouput[col] = saturate_cast<uchar>(5*current[col]-(current[col-offsetx]+current[col+offsetx]+previous[col]+next[col]));//执行滤波运算
			}
		}
		namedWindow("outout window",WINDOW_AUTOSIZE);
		imshow("outout window", dst);
	}

	/*imwrite("21.png",gray);*/
	
	waitKey(0);
	return 0;
}

使用封装的代码

#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<iostream>
#include<math.h>

using namespace cv;
int main(int argc, char** argv)
{
	Mat gray,dst;
	Mat src = imread("22.jpg");//读取图像
	namedWindow("Window Title", WINDOW_NORMAL);//打开窗口,参数一是窗口名子
	if (src.empty())
	{
		printf("picture is wrong");
	}
	else
	{
		imshow("Window Title", src);//显示图像
		namedWindow("outout window",WINDOW_AUTOSIZE);
		double t = getTickCount();//获取时间
        Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); //定义滤波器大小
		filter2D(src,dst,src.depth(),kernel);//进行卷积操作
		double timeconsume = (getTickCount()-t)/getTickFrequency();//计算时间
		printf("time consume %3.f\n",timeconsume);
		imshow("outout window", dst);
	}

	/*imwrite("21.png",gray);*/
	
	waitKey(0);
	return 0;
}

如有帮组,请点赞,谢谢。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪猪派对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值