(学习opencv二刷) 形态学算法处理

  1. 腐蚀,膨胀,开闭操作        
#include"stdafx.h"
#include <opencv2/opencv.hpp>    
#include <opencv2/imgproc/imgproc.hpp> 
using namespace std;
using namespace cv;
int main()
{

	Mat mSrc1 = imread("test1.jpg");
	resize(mSrc1, mSrc1, Size(512, 512));
	Mat mSrc2 = imread("test2.jpg");
	resize(mSrc2, mSrc2, Size(512, 512));
	Mat Diff;
	//= abs(mSrc1 - mSrc2);
	subtract(mSrc1, mSrc2, Diff);
	abs(Diff);
	imshow("Diff", Diff);
	waitKey(0);

	Mat erode_Diff;
	Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(0, 0));
	erode(Diff, erode_Diff, element);
	Mat clean_Diff;
	dilate(erode_Diff, clean_Diff, element);
	imshow("clean_Diff", clean_Diff);
	waitKey(0);

	/*Mat open_Diff;
	morphologyEx(Diff, open_Diff, MORPH_GRADIENT, element);
	imshow("open_Diff", open_Diff);
	waitKey(0);*/

	return 0;
}

原图:



运行结果:


2.图像降采样(缩小)

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
	Mat srcImage = imread("牛吃草.jpg");
	imshow("【原图】", srcImage);

	//其实就是缩小操作
	Mat dstImage;
	pyrDown(srcImage, dstImage, Size(srcImage.cols / 2, srcImage.rows / 2));

	imshow("【处理后的图片】", dstImage);

	waitKey(0);

	return 0;
}
运行后的结果:


3.图像升采样(插值)

4.图像透视变换

设计思路:

1.图片预处理,利用霍夫变换检测出图片中长度超过10的直线
2.延长得到的直线
3.计算延长线的交点,找到四边形的四个边界点
4.利用得到的点拟合四边形
5.对得到的四边形的四个点排序
   5.1得到中心点
   5.2对得到的边界点排序
   5.3 透视变换

#include"stdafx.h"
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream> 
using namespace std;
using namespace cv;

Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b)
{
	int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
	float denom;

	if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)))
	{
		cv::Point2f pt;
		pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
		pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
		return pt;
	}
	else
		return cv::Point2f(-1, -1);
}
void OnDrawDotline(CvPoint s, CvPoint e, Mat workimg)
{
	CvPoint pa, pb;
	double k = (s.y - e.y) / (s.x - e.x + 0.000001);//不加0.000001 会变成曲线,斜率可能为0,即e.x-s.x可能为0    

	double h = workimg.rows;
	double w = workimg.cols;

	pa.x = w;
	pa.y = s.y + k*(w - s.x);

	line(workimg, e, pa, CV_RGB(255, 0, 255), 2, CV_AA, 0);   //向右画线    


	pb.y = e.y - k*e.x;
	pb.x = 0;

	line(workimg, pb, s, CV_RGB(255, 255, 0), 2, CV_AA, 0); //向左画线    

}
void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center)
{
	std::vector<cv::Point2f> top, bot;

	for (int i = 0; i < corners.size(); i++)
	{
		if (corners[i].y < center.y)
			top.push_back(corners[i]);
		else
			bot.push_back(corners[i]);
	}
	corners.clear();

	if (top.size() == 2 && bot.size() == 2) {
		cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
		cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
		cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
		cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];


		corners.push_back(tl);
		corners.push_back(tr);
		corners.push_back(br);
		corners.push_back(bl);
	}
}
int main()
{
	Mat mSrcImg;
	mSrcImg = imread("timg.jpg");
	if (mSrcImg.empty())
		return -1;
	Mat mSrcCopy;
	mSrcImg.copyTo(mSrcCopy);
	//1.预处理
	Mat mSrcCvt;
	cvtColor(mSrcImg, mSrcCvt,CV_BGR2GRAY);
	Mat mSrcBlur;
	blur(mSrcCvt, mSrcBlur, Size(3, 3));
	Mat mSrcCanny;
	Canny(mSrcBlur, mSrcCanny, 20, 200,3);

	vector<Vec4i>lines;
	HoughLinesP(mSrcCanny, lines, 1, CV_PI / 360, 70, 50, 20);
	Point2f pLeft, pRight;
	//2.延长每条线
	for (int i = 0; i < lines.size(); i++)
	{
		pLeft = Point2f(lines[i][0], lines[i][1]);
		pRight = Point2f(lines[i][2], lines[i][3]);
		line(mSrcCopy, pLeft, pRight, Scalar(200, 0, 200), 2, 8);
		OnDrawDotline(pLeft, pRight, mSrcCopy);
		imshow("mSrcCopy", mSrcCopy); waitKey(0);
	}
	//3.计算延长线的交点,找到四边形的四个交点
	vector<Point2f> corners;
	for (int i = 0; i < lines.size(); i++)
	{
		for (int j = i + 1; j < lines.size(); j++)
		{
			Point2f pIntersection;
			pIntersection = computeIntersect(lines[i], lines[j]);
			if (pIntersection.x>0&& pIntersection.y>0&& pIntersection.x<mSrcImg.cols&&pIntersection.y<mSrcImg.rows)
			{
				corners.push_back(pIntersection);
				circle(mSrcCopy, pIntersection, 8, Scalar(255, 0, 0), 2, 8);
				imshow("mSrcCopy", mSrcCopy); waitKey(0);
			}
			 
		}
	}
	//C++: void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
	//参数详解;
	//InputArray curve : 一般是由图像的轮廓点组成的点集
	//	OutputArray approxCurve:表示输出的多边形点集
	//	double epsilon:主要表示输出的精度,就是另个轮廓点之间最大距离数,5, 6, 7,,8,,, , ,
	//	bool closed:表示输出的多边形是否封闭
	//4.拟合四边形
	vector<Point2f>approx;
	approxPolyDP(Mat(corners), approx, arcLength(Mat(corners), true)*0.02, true);
	if (approx.size()!=4)
	{
		cout << "不是四边形!" << endl;
		return -1;
	}
	//5.对得到的四边形的四个点排序
	//5.1得到中心点
	Point2f center;
	for (int i = 0; i < corners.size(); i++)
		center += corners[i];
	center *= (1. / corners.size());
	circle(mSrcCopy, center, 5, Scalar(0, 0, 255), 2, 8);
	imshow("mSrcCopy", mSrcCopy); waitKey(0);
	sortCorners(corners, center);

	if (corners.size() == 0)
	{
		cout << "The corners were not sorted correctly! "<<endl;
		return -1;
	}
	Mat quad = Mat(300, 200, CV_8UC3);
	vector<Point2f> quad_corns;
	quad_corns.push_back(Point2f(0, 0));
	quad_corns.push_back(Point2f(quad.cols, 0));
	quad_corns.push_back(Point2f(quad.cols, quad.rows));
	quad_corns.push_back(Point2f(0, quad.rows));

	Mat transmax = getPerspectiveTransform(corners, quad_corns);
	warpPerspective(mSrcImg,quad, transmax,quad.size());



	imshow("quad", quad); waitKey(0);
	return 0;
}

 原图:


运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值