openCV学习笔记(十二)-- 图像矩,点多边形距离测试

  1. 图像矩
  2. 点多边形距离测试

1. 图像矩
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第1张图片
图像中心计算
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第2张图片
计算矩的API
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第3张图片
具体实现步骤
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第4张图片
再找出图像的中心质点后,可以在输出图像中画出来
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第5张图片
实现代码:

//图像矩
	#include
#include
#include

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);
	findContours(canny_output, contours, hoerachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

	//存放图像中心矩信息
	vector<Moments> contours_moments(contours.size());
	vector<Point2f> css(contours.size());

	for (size_t i = 0; i < contours.size(); i++)
	{
		//计算出图像的中心距
		contours_moments[i] = moments(contours[i]);
		//根据中心距结果,计算图像的中心质点
		//static_cast--强制类型转换
		css[i] = Point(static_cast<float>(contours_moments[i].m10 / contours_moments[i].m00), static_cast<float>(contours_moments[i].m01 / contours_moments[i].m00));

	}

	//画出边缘及中心点
	Mat drawImg;
	src.copyTo(drawImg);
	for (size_t i = 0; i < contours.size(); i++)
	{
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		printf("center point x : %.2f y : %.2f\n", css[i].x, css[i].y);
		//计算出图像对象面积及边缘长度
		printf("contours %d area: %2f, arc length:%2f\n", i, contourArea(contours[i]), arcLength(contours[i], true));
		drawContours(drawImg, contours, i, color, 2, 8, hoerachy, 0, Point(0, 0));
		circle(drawImg, css[i], 2, color, 2, 8);
	}
	imshow(output_win, drawImg);
	return;

}

2. 点多边形距离测试
主要时测量一个像素点是否在多边形的什么位置,越靠近轮廓值越小(负值则越大)
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第6张图片
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第7张图片
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第8张图片
openCV代码:

#include
#include
#include

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) {	
	//画一个六边形作为轮廓
	const int r = 100;
	Mat src = Mat::zeros(r * 4, r * 4, CV_8UC1);

	vector<Point2f> vert(6);
	vert[0] = Point(3 * r / 2, static_cast<int>(1.34 * r));
	vert[1] = Point(1 * r    , 2*r);
	vert[2] = Point(3 * r / 2, static_cast<int>(2.866 * r));
	vert[3] = Point(5 * r / 2, static_cast<int>(2.866 * r));
	vert[4] = Point(3 * r, 2*r);
	vert[5] = Point(5 * r / 2, static_cast<int>(1.34 * r));

	for (int i = 0; i < 6; i++)
	{
		line(src, vert[i], vert[(i + 1) % 6], Scalar(255), 3, 8,0);
	}

	const char* output_win = "point polygon test demo";
	char input_win[] = "input_image";
	namedWindow(output_win, WINDOW_AUTOSIZE);
	namedWindow(input_win, WINDOW_AUTOSIZE);

	imshow(input_win, src);

	vector<vector<Point>> contours;
	vector<Vec4i> hierachy;
	Mat csrc;
	src.copyTo(csrc);
	findContours(csrc, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	Mat raw_dist = Mat::zeros(csrc.size(), CV_32FC1);
	//对csrc图像的每个像素点测试距离
	for (int row = 0; row < raw_dist.rows; row++)
	{
		for (int col = 0; col < raw_dist.cols; col++)
		{
			//测试距离
			double dist = pointPolygonTest(contours[0], Point2f(static_cast<float>(col), static_cast<float>(row)),true);
			//将距离值赋到对应位置的像素值
			raw_dist.at<float>(row, col) = static_cast<float>(dist);
		}
	}
	double minValue, maxValue;
	//在rawdiat中找到最大最小值
	minMaxLoc(raw_dist, &minValue, &maxValue, 0, 0, Mat());
	//给图像上色并输出
	//图像轮廓外上红色,内上蓝色色,计算式首先将像素/最大/小值做归一化,因为最小值为负,取绝对值
	//越靠近边缘值越小,所以越来接近满值
	Mat drawImg = Mat::zeros(src.size(), CV_8UC3);
	for (int row = 0; row < drawImg.rows; row++) {
		for (int col = 0;  col < drawImg.cols;  col++)
		{
			float dist = raw_dist.at<float>(row, col);
			if (dist > 0) {
				//轮廓内
				drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(1.0 - (dist / maxValue)) * 255);
			}
			else if(dist<0){
				//轮廓外
				drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(1.0 - (dist / minValue)) * 255);
			}
			else {
				//轮廓边上
				//边缘上白色,因为dist==0
				drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(255 - dist));
				drawImg.at<Vec3b>(row, col)[1] = (uchar)(abs(255 - dist));
				drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(255 - dist));
			}
		}
	}
	imshow(output_win, drawImg);

	waitKey(0);
	return 0;

}

效果展示:
openCV学习笔记(十二)-- 图像矩,点多边形距离测试_第9张图片

你可能感兴趣的:(openCV)