编程语言
首页 > 编程语言> > 基于c++的opencv图像处理学习笔记四

基于c++的opencv图像处理学习笔记四

作者:互联网

本文是在学习基于c++的opencv图像处理时所做的一些个人笔记,现在还不知道为啥一些头文件或者vector容器内包含的类型会加载不出来,如有程序执行不成功的,解决不了的可以留言,在本人能力范围内尽力帮助大家解决,谢谢。
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
//查找并绘制轮廓findContours()函数和drawContours()函数
Mat srcImage_fc = imread(“weihong.jpg”, 0);
imshow(“灰度图”, srcImage_fc);
Mat dstImage_fc = Mat::zeros(srcImage_fc.rows, srcImage_fc.cols,CV_8UC3);
Mat srcImage_fc1 = srcImage_fc >140;
imshow(“阈值化图”, srcImage_fc1);
vector<vector> contours;
vector hierarchy;
findContours(srcImage_fc1, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
int index = 0;
for (; index >= 0; index = hierarchy[index][0])
{
Scalar color(rand() & 255, rand() & 255, rand() & 255);
drawContours(dstImage_fc, contours, index, color, FILLED, 8, hierarchy);
}
imshow(“轮廓图”, dstImage_fc);

//凸包convexHull()函数
blur(srcImage_fc, srcImage_fc, Size(3, 3));
Mat srcImage_fc_out;
threshold(srcImage_fc, srcImage_fc_out, 50, 255, THRESH_BINARY);
vector<vector<Point>> hull(contours.size());
for (unsigned int i = 0; i < contours.size(); i++)
{
	convexHull(Mat(contours[i]), hull[i], false);
}
Mat drawing = Mat::zeros(srcImage_fc_out.size(), CV_8UC3);
for (unsigned int i = 0; i < contours.size(); i++)
{
	Scalar color(55, 100, 195);
	drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
	drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
}
imshow("凸包", drawing);

//使用多边形包围轮廓
//boundingRect() 返回外部矩形边界、minAreaRect()寻找最小包围矩形、minEnclosingCircle()寻找最小包围圆形
//fillEllipse()用椭圆拟合二维点集、approxPolyDP()逼近多边形曲线
Mat srcImage_min = imread("haidi.jpg", 1);
imshow("海底原图", srcImage_min);
Mat grayImage_min,threshold_min;
cvtColor(srcImage_min, grayImage_min, COLOR_BGR2GRAY);
blur(grayImage_min, grayImage_min, Size(3,3));

vector<vector<Point>>contours_min;
vector<Vec4i> hierarchy_min;
threshold(grayImage_min, threshold_min, 50, 255, THRESH_BINARY);
findContours(threshold_min, contours_min, hierarchy_min, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point>>contours_poly(contours_min.size());
vector<Rect>boundRect(contours_min.size());
vector<Point2f>center(contours_min.size());
vector<float>radius(contours_min.size());
for (unsigned int i = 0; i < contours_min.size(); i++)
{
	approxPolyDP(Mat(contours_min[i]), contours_poly[i],3,true);
	boundRect[i] = boundingRect(Mat(contours_poly[i]));
	minEnclosingCircle(contours_poly[i], center[i], radius[i]);
}
Mat drawing_min = Mat::zeros(threshold_min.size(), CV_8UC3);
for (int unsigned i = 0; i < contours_min.size(); i++)
{
	Scalar color_min(55, 100, 195);
	drawContours(drawing_min, contours_poly, i, color_min, 1, 8, vector<Vec4i>(), 0, Point());
	rectangle(drawing_min, boundRect[i].tl(), boundRect[i].br(), color_min, 1, 8, 0);
	circle(drawing_min, center[i], (int)radius[i], color_min, 1, 8, 0);
}
imshow("矩形圆形轮廓图", drawing_min);

//图像矩 : moments()函数计算矩、contourArea()函数计算轮廓面积、arcLength()函数计算轮廓长度
Mat srcImage_ju = imread("haidi.jpg", 1);
imshow("海底原图", srcImage_ju);
Mat grayImage_ju, canny_ju;
cvtColor(srcImage_ju, grayImage_ju, COLOR_BGR2GRAY);
blur(grayImage_ju, grayImage_ju, Size(3, 3));
vector<vector<Point>>contours_ju;
vector<Vec4i> hierarchy_ju;
Canny(grayImage_ju, canny_ju, 50, 150, 3);
findContours(canny_ju, contours_ju, hierarchy_ju, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Moments>mu(contours_ju.size());
//计算矩
for (unsigned int i = 0; i < contours_ju.size(); i++)
{
	mu[i] = moments(contours_ju[i], false);
}
vector<Point2f>mc(contours_ju.size());
//计算中心矩
for (unsigned int i = 0; i < contours_ju.size(); i++)
{
	mc[i] = Point2f(static_cast<float>(mu[i].m10 / mu[i].m00), static_cast<float>(mu[i].m01 / mu[i].m00));
}
Mat drawing_ju = Mat::zeros(canny_ju.size(), CV_8UC3);
for (int unsigned i = 0; i < contours_ju.size(); i++)
{
	Scalar color_ju(55, 100, 195);
	drawContours(drawing_ju, contours_ju, i, color_ju,1, 8, hierarchy_ju, 0, Point());
	circle(drawing_ju, mc[i], 4, color_ju, -1, 8, 0);
}
imshow("矩效果图", drawing_ju);
printf("面积和轮廓长度\n");
for (unsigned int i = 0; i < contours_ju.size(); i++)
{
	printf("通过m00计算出轮廓[%d]的面积:(M00)=%.2f\n opencv函数计算出的面积=%.2f,长度:%.2f\n\n", i, mu[i].m00,
		contourArea(contours_ju[i]), arcLength(contours_ju[i], true));
	Scalar color_ju(55, 100, 195);
	drawContours(drawing_ju, contours_ju, i, color_ju, 1, 8, hierarchy_ju, 0, Point());
	circle(drawing_ju, mc[i], 4, color_ju, -1, 8, 0);
}
//分水岭算法watershed()函数
//图像修补inpaint()函数
waitKey(0);

}

标签:srcImage,Mat,min,ju,c++,opencv,图像处理,contours,size
来源: https://blog.csdn.net/qingFeng_forYou/article/details/97618084