opencv c++矩阵掩模操作
程序员文章站
2024-03-25 10:32:22
...
#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
int main()
{
Mat dst;
Mat src = imread("C:/Users/asus/Desktop/tupian/1.jpg");
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("opencv setup demo", WINDOW_AUTOSIZE);
imshow("opencv setup demo", 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* output = dst.ptr<uchar>(row );
for (int col = offsetx; col < cols; col++)
{
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col- offsetx]+ current[col + offsetx]+ previous[col]+ next[col]));
}
}
namedWindow("dst", WINDOW_AUTOSIZE);
imshow("dst", dst);
waitKey(0);
return 0;
}
#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
int main()
{
Mat dst;
Mat src = imread("C:/Users/asus/Desktop/tupian/1.jpg");
if (src.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("opencv setup demo", WINDOW_AUTOSIZE);
imshow("opencv setup demo", src);
double t = getTickCount();
Mat kennel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, src.depth(), kennel);
double timeconsume = (getTickCount() - t) / getTickFrequency();
printf("tim %.4f", timeconsume);
namedWindow("dst", WINDOW_AUTOSIZE);
imshow("dst", dst);
waitKey(0);
return 0;
}
上一篇: OpenCV打开摄像头并显示图像(C++、Python)
下一篇: opencv