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

本文是在学习基于c++的opencv图像处理时所做的一些个人笔记,希望可以帮助大家回忆起来容易忘记的东西,本人也是初学,如有不足之处,还望指导指正,谢谢。
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
//canny边缘检测
Mat src = imread(“shuiguo.jpg”);
Mat src1 = src.clone();
imshow(“原图”, src);
Mat srcImage;
Canny(src, srcImage, 150, 100, 3);
imshow(“canny边缘检测”, srcImage);

Mat dst, edge, gray;
dst.create(src1.size(), src.type());
cvtColor(src1, gray, COLOR_BGR2GRAY);
blur(gray, edge, Size(3, 3));
Canny(edge, edge, 3, 9, 3);
dst = Scalar::all(0);
src1.copyTo(dst, edge);
imshow("高阶canny边缘检测", dst);
//sobel边缘检测
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y, dst1;
Mat srcImage_sobel = imread("shuiguo.jpg");
Sobel(srcImage_sobel, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
imshow("sobel_x边缘检测", abs_grad_x);
Sobel(srcImage_sobel, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);
imshow("sobel_y边缘检测", abs_grad_y);
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst1);
imshow("sobel边缘检测", dst1);
//Laplacian边缘检测
Mat srcImage_lpls, srcImage_lpls_gray, dst2, abs_dst2;
srcImage_lpls = imread("shuiguo.jpg");
GaussianBlur(srcImage_lpls, srcImage_lpls, Size(3, 3), 0, 0, BORDER_DEFAULT);
cvtColor(srcImage_lpls, srcImage_lpls_gray, COLOR_BGR2GRAY);
Laplacian(srcImage_lpls_gray, dst2, CV_16S, 3, 1, 0, BORDER_DEFAULT);
convertScaleAbs(dst2, abs_dst2);
imshow("laplacian边缘检测", abs_dst2);
//Scharr滤波器
Mat scharr_grad_x, scharr_grad_y;
Mat abs_scharr_grad_x, abs_scharr_grad_y, dst3;
Mat srcImage_scharr = imread("shuiguo.jpg");
Scharr(srcImage_scharr, scharr_grad_x, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT);
convertScaleAbs(scharr_grad_x, abs_scharr_grad_x);
imshow("scharr_x边缘检测", abs_scharr_grad_x);
Scharr(srcImage_scharr, scharr_grad_y, CV_16S, 0, 1, 1, 0, BORDER_DEFAULT);
convertScaleAbs(scharr_grad_y, abs_scharr_grad_y);
imshow("scharr_y边缘检测", abs_scharr_grad_y);
addWeighted(abs_scharr_grad_x, 0.5, abs_scharr_grad_y, 0.5, 0, dst3);
imshow("scharr边缘检测", dst3);

//标准霍夫线变换
Mat srcImage_hough = imread("jianzhu.jpg");
imshow("原图", srcImage_hough);
Mat midImage, dstImage_hough;
Canny(srcImage_hough, midImage, 50, 200, 3);
imshow("边缘检测", midImage);
cvtColor(midImage, dstImage_hough, COLOR_GRAY2BGR);
vector<Vec2f>lines;
HoughLines(midImage, lines, 1, CV_PI / 180,200, 0, 0);
for (size_t i = 0; i < lines.size(); i++)
{
	float rho = lines[i][0], theta = lines[i][1];
	Point pt1, pt2;
	double a = cos(theta), b = sin(theta);
	double x0 = a * rho, y0 = b * rho;
	pt1.x = cvRound(x0 + 1000 * (-b));
	pt1.y = cvRound(y0 + 1000 * (a));
	pt2.x = cvRound(x0 - 1000 * (-b));
	pt2.y = cvRound(y0 -1000 * (a));
	line(dstImage_hough, pt1, pt2, Scalar(55, 100, 195), 1, LINE_AA);
}
imshow("标准霍夫线变换", dstImage_hough);

//累计概率霍夫变换
Mat srcImage_houghp = imread("jianzhu.jpg");
//imshow("原图", srcImage_houghp);
Mat midImage_p, dstImage_houghp;
Canny(srcImage_houghp, midImage_p,10,250, 3);
//imshow("边缘检测", midImage_p);
cvtColor(midImage_p, dstImage_houghp,COLOR_GRAY2BGR);
vector<Vec4i>linesp;
HoughLinesP(midImage_p, linesp, 1, CV_PI / 180.0, 80, 50, 10);
for (size_t i = 0; i < linesp.size(); i++)
{
	Vec4i l = linesp[i];
	line(dstImage_houghp, Point(l[0],l[1]),Point(l[2],l[3]), Scalar(55, 100, 195), 3, LINE_AA);
}
imshow("累计概率霍夫线变换", dstImage_houghp);

//HoughCircles霍夫圆变换
Mat srcImage_houghc = imread("jianzhu.jpg");
Mat midImage_houghc, dstImage_houghc;
imshow("水果原图", srcImage_houghc);
cvtColor(srcImage_houghc, midImage_houghc, COLOR_BGR2GRAY);
GaussianBlur(midImage_houghc, midImage_houghc, Size(9, 9), 2, 2);
vector<Vec3f>circles;
HoughCircles(midImage_houghc, circles, HOUGH_GRADIENT, 1.5, 10, 400, 200, 0, 0);
for (size_t i = 0; i < circles.size(); i++)
{
	Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
	int radius = cvRound(circles[i][2]);
	circle(srcImage_houghc, center, 3, Scalar(55, 100, 195), -1, 8, 0);
	circle(srcImage_houghc, center, radius, Scalar(155, 50, 255), 3, 8, 0);
}
imshow("霍夫圆变换", srcImage_houghc);

//remap()重映射函数
Mat srcImage_r, dstImage_r;
Mat map_x, map_y;
srcImage_r = imread("jianzhu.jpg");
imshow("建筑图", srcImage_r);
dstImage_r.create(srcImage_r.size(), srcImage_r.type());
map_x.create(srcImage_r.size(), CV_32FC1);
map_y.create(srcImage_r.size(), CV_32FC1);
for (int i = 0; i < srcImage_r.rows; i++)
{
	for (int j = 0; j < srcImage_r.cols; j++)
	{
		map_x.at<float>(i, j) = static_cast<float>(srcImage_r.cols - j);
		map_y.at<float>(i, j) = static_cast<float>(i);
	}
}
remap(srcImage_r, dstImage_r, map_x, map_y,INTER_LINEAR,BORDER_CONSTANT,Scalar(0,0,0));
imshow("重映射", dstImage_r);

//getRotationMatrix2D()与warpAffine()仿射变换
Point2f srcTriangle[3], dstTriangle[3];
Mat rotMat(2, 3, CV_32FC1), warpMat(2, 3, CV_32FC1);
Mat srcImage_warp, dstImage_warp, dstImage_warp_rotate;
srcImage_warp = imread("jianzhu.jpg", 1);
dstImage_warp=Mat::zeros(srcImage_warp.rows,srcImage_warp.cols ,srcImage_warp.type());
srcTriangle[0] = Point2f(0, 0);
srcTriangle[1] = Point2f(static_cast<float>(srcImage_warp.cols-1), 0);
srcTriangle[2] = Point2f(0, static_cast<float>(srcImage_warp.rows - 1));
dstTriangle[0] = Point2f(static_cast<float>(srcImage_warp.cols*0.0), static_cast<float>(srcImage_warp.cols*0.33));
dstTriangle[1] = Point2f(static_cast<float>(srcImage_warp.cols*0.65), static_cast<float>(srcImage_warp.cols*0.36));
dstTriangle[2] = Point2f(static_cast<float>(srcImage_warp.cols*0.25), static_cast<float>(srcImage_warp.cols*0.6));
warpMat = getAffineTransform(srcTriangle, dstTriangle);
warpAffine(srcImage_warp, dstImage_warp, warpMat, dstImage_warp.size());

Point center = Point(dstImage_warp.cols / 2, dstImage_warp.rows / 2);
double angle = -30.0;
double scale = 0.6;
rotMat = getRotationMatrix2D(center, angle, scale);
warpAffine(dstImage_warp, dstImage_warp_rotate, rotMat, dstImage_warp.size());
imshow("建筑原图", srcImage_warp);
imshow("warp图", dstImage_warp);
imshow("rotate图", dstImage_warp_rotate);

//直方图均衡化equalizeHist()函数
Mat dstImage_equ,dstImage_equ1;
cvtColor(srcImage_warp, dstImage_equ, COLOR_BGR2GRAY);
equalizeHist(dstImage_equ, dstImage_equ1);
imshow("灰度图", dstImage_equ);
imshow("均衡化图", dstImage_equ1);

waitKey(0);
return 0;

}

上一篇:OpenCV——膨胀与腐蚀


下一篇:10.29html笔记