- 图像矩
- 点多边形距离测试
1. 图像矩
图像中心计算
计算矩的API
具体实现步骤
再找出图像的中心质点后,可以在输出图像中画出来
实现代码:
//图像矩
#include<opencv2/opencv.hpp>
#include<math.h>
#include<iostream>
using namespace cv;
using namespace std;
Mat src,dst,gray_src,temp,dst1;
const char* output_win = "output_img";
RNG rng(12345);
int threshold_v = 100;
int threshold_max = 255;
void Demo_Moments(int, void*);
int main(int argc, char** argv) {
src = imread("C:/Users/18929/Desktop/博客项目/项目图片/06.jpg");
if (src.empty()) {
printf("could not load image");
return -1;
}
namedWindow("input_image", WINDOW_AUTOSIZE);
namedWindow(output_win, WINDOW_AUTOSIZE);
cvtColor(src, gray_src, CV_BGR2GRAY);
//模糊一下,二值化时减少噪点
blur(gray_src, gray_src, Size(3, 3), Point(-1, -1));
imshow("input_image", gray_src);
createTrackbar("Threshold Value", output_win, &threshold_v, threshold_max, Demo_Moments);
Demo_Moments(0, 0);
waitKey(0);
return 0;
}
void Demo_Moments(int, void*) {
Mat canny_output;
vector<vector<Point>> contours;
vector<Vec4i> hoerachy;
Canny(gray_src, canny_output, threshold_v, threshold_v * 2, 3, false