#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;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有