图像矩:
矩:主要指几何矩 Mji = ∑ (P(x,y) · x^j · y^i) 其中 (i+j) 和等于几就是几阶矩,
P(x,y)表示图像x,y坐标位置的像素值,i j 为变量x,y
还有中心矩(MUji), 中心归一化矩(NUji)
图像中心 Center(x0, y0) x0 = M10 / M00 , y0 = M01 / M00
此非图像坐标的中心点,而是图像 质 的中心,质点
API:
moments( // 计算矩,返回 0阶到3阶所有的几何矩,2阶到3阶所有的中心矩,2阶到3阶所有的中心归一矩
InputArray array, // 输入数据,findContours 找到的轮廓数据
bool binaryImage=false // 是否为二值图像
)
contourArea( // 求轮廓面积
InputArray contour, // 输入轮廓数据
bool oriented // 默认false、返回绝对值
)
arcLength( // 求轮廓弧长
InputArray curve, // 输入曲线数据
bool closed // 是否是封闭曲线
)
步骤:
- 提取图像边缘
- 发现轮廓
- 计算每个轮廓对象的矩
计算每个对象的中心、弧长、面积
代码:
#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <string>
#include<fstream>
using namespace cv;
using namespace std;
Mat src, gray_src,dst;
int threshold_value = 175;
int threshold_max = 255;
RNG rng(12345);
void get_moments(int, void*)
{
Mat canny_output;
vector<vector<Point>> contours;
//vector<Vec4i> hierachy;
Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);//提取边缘
findContours(canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);//发现轮廓