#include<opencv2/opencv.hpp>
using namespace cv;
void main() {
Mat src = imread("F:/VisualStudioSpace/OpenCV/Resource/card.jpg");
imshow("src", src);
// 降噪
Mat gussian;
GaussianBlur(src, gussian, Size(3, 3), 0);
// 灰度
Mat gray;
cvtColor(gussian, gray, COLOR_BGR2GRAY);
// 拉普拉斯算法
Mat dst;
Mat lpls = (Mat_<char>(3, 3) <<
0, -1, 0,
-1, 4, -1,
0, -1, 0
);
filter2D(
gray,
dst,
gray.depth(),
lpls,
Point(-1, -1)
);
imshow("lpls", dst);
cvWaitKey(0);
}
#include<opencv2/opencv.hpp>
using namespace cv;
void main() {
Mat src = imread("F:/VisualStudioSpace/OpenCV/Resource/card.jpg");
imshow("src", src);
// 降噪
Mat gussian;
GaussianBlur(src, gussian, Size(3, 3), 0);
// 灰度
Mat gray;
cvtColor(gussian, gray, COLOR_BGR2GRAY);
// 拉普拉斯算法
Mat lpls;
Laplacian(
gray,
lpls,
CV_16S, // 卷积核的精度
5 // 卷积核大小
);
convertScaleAbs(lpls, lpls);
imshow("lpls", lpls);
cvWaitKey(0);
}
#include<opencv2/opencv.hpp>
using namespace cv;
void main() {
Mat src = imread("F:/VisualStudioSpace/OpenCV/Resource/card.jpg");
imshow("src", src);
// canny 算法
Mat canny;
// L1gradient: x,y 梯度绝对值求和
// L2gradient: x,y 求平方根
Canny(
src,
canny,
50, // 高低阈值1
150, // 高低阈值2, 在 [高低阈值1, 高低阈值2] 之间取 255, 否则取 0
3, // 卷积核大小
false // 梯度合并类型, L1gradient is false, L2gradient is true
);
imshow("canny", canny);
cvWaitKey(0);
}
#include<opencv2/opencv.hpp>
#include<vector>
using namespace cv;
using namespace std;
void main() {
Mat src = imread("F:/VisualStudioSpace/OpenCV/Resource/card.jpg");
imshow("src", src);
// 1. Canny 边缘检测
Mat binary;
Canny(src, binary, 50, 150);
imshow("binary", binary);
// 2. 轮廓查询
vector<vector<Point>> contours;
findContours(
binary,
contours,
RETR_LIST,
CHAIN_APPROX_SIMPLE
);
// 3. 绘制轮廓
Mat contour_mat = Mat::zeros(src.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++) {
// 筛选出大于画布一半的轮廓绘制出来
Rect rect = boundingRect(contours[i]);
if (rect.width > contour_mat.cols >> 1 && rect.height > contour_mat.rows >> 1) {
// 绘制轮廓
drawContours(
contour_mat, // 画布
contours, // 轮廓集合
i, // 轮廓索引
Scalar(0, 255, 0) // 绘制颜色
);
// 绘制矩形框
rectangle(contour_mat,
Point(rect.x, rect.y),
Point(rect.x + rect.width, rect.y + rect.height),
Scalar(255, 255, 255)
);
break;
}
}
imshow("contour_mat", contour_mat);
cvWaitKey(0);
getchar();
}