opencv自定義線性濾波、邊緣處理、Sobel算子(6)

1、自定義線性濾波

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat srcImg = imread("C:/Users/admin/Desktop/1.JPG");
	if (!srcImg.data)
	{
		printf("沒有找到!!!");
		return -1;
	}
	Mat dstImg, dstImg2, dstImg3,dstImg4;
	Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);//sobel
	Mat kernel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
	Mat lap = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);//拉普拉斯算子

	int c = 0;
	int index = 0;
	int ksiz

e = 3;
while (true)
{
c = waitKey(500);
if ((char)c==27)//27鍵盤上esc
{
break;
}
ksize = 4 + (index %20) * 2 + 1;
Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize*ksize);
filter2D(srcImg, dstImg4, -1, kernel, Point(-1, -1));
index++;
imshow(“dstImg4 windows”, dstImg4);
}

filter2D(srcImg, dstImg3, -1, lap, Point(-1, -1));
namedWindow("dstImg3 windows", CV_WINDOW_AUTOSIZE);
imshow("dstImg3 windows",dstImg3);
filter2D(srcImg, dstImg, -1, kernel_x, Point(-1, -1));
namedWindow("dstImg windows", CV_WINDOW_AUTOSIZE);
filter2D(srcImg, dstImg2, -1, kernel_y, Point(-1, -1));
namedWindow("dstImg2 windows", CV_WINDOW_AUTOSIZE);
imshow("dstImg2 windows", dstImg2);
namedWindow("dstImg windows", CV_WINDOW_AUTOSIZE);
imshow("dstImg windows", dstImg);
namedWindow("srcImg windows", CV_WINDOW_AUTOSIZE);
imshow("srcImg windows", srcImg);
waitKey(0);
return 0;

}

2、邊緣處理

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat srcImg = imread("C:/Users/admin/Desktop/1.JPG");
	if (!srcImg.data)
	{
		printf("沒有找到!!!");
		return -1;
	}

	int top = (int)(0.05*srcImg.rows);
	int bottom = (int)(0.05*srcImg.rows);
	int left = (int)(0.05*srcImg.cols);
	int right = (int)(0.05*srcImg.cols);

	RNG rng(12345);
int c = 0;

int borderType = BORDER_DEFAULT;//默認處理方式
Mat dstImg;
while (true)
{
	c = waitKey(500);
	if ((char)c==27)
	{
		break;
	}
	if ((char)c=='r')
	{
		borderType = BORDER_REPLICATE;//用已知的邊緣像素值填充
	}
	else if ((char)c=='w')
	{
		borderType = BORDER_WRAP;//用另一邊的像素來補償填充
	}
	else if ((char)c=='c')
	{
		borderType = BORDER_CONSTANT;//用指定像素來填充
	}
	Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
	copyMakeBorder(srcImg, dstImg, top, bottom, left, right, borderType, color);
	namedWindow("dstImg windows", CV_WINDOW_AUTOSIZE);
	imshow("dstImg windows", dstImg);
}
/*namedWindow("dstImg windows", CV_WINDOW_AUTOSIZE);
imshow("dstImg windows", dstImg);*/
namedWindow("srcImg windows", CV_WINDOW_AUTOSIZE);
imshow("srcImg windows", srcImg);
waitKey(0);

return 0;

}

3、Sobel算子

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat srcImg = imread("C:/Users/admin/Desktop/1.JPG");
	if (!srcImg.data)
	{
		printf("沒有找到!!!");
		return -1;  
	}

	Mat gray_img,dstImg;
	//進行高斯模糊
	GaussianBlur(srcImg, dstImg, Size(3, 3), 0, 0);
	//轉爲灰度圖像
	cvtColor(dstImg, gray_img, CV_BGR2GRAY);
	imshow("gray_img window", gray_img);

	//sobel算子
Mat x_grad, y_grad;
Scharr(gray_img, x_grad, CV_16S, 1, 0);
Scharr(gray_img, y_grad, CV_16S, 0, 1);
//Sobel(gray_img, x_grad, CV_16S, 1, 0, 3); //CV_16S 深度
//Sobel(gray_img, y_grad, CV_16S, 0, 1, 3);
convertScaleAbs(x_grad, x_grad);
convertScaleAbs(y_grad, y_grad);

Mat xy_grad=Mat(x_grad.size(),x_grad.type());

//addWeighted(x_grad, 0.5, y_grad, 0.5, 0, xy_grad);//圖像混合
int width = xy_grad.cols;
int height = xy_grad.rows;
//printf("%d", x_grad.type());
for (int row = 0; row < height; row++)
{
	for (int col = 0; col < width; col++)
	{
		int xg = x_grad.at<uchar>(row, col);
		int yg = y_grad.at<uchar>(row, col);
		
		xy_grad.at<uchar>(row, col) = saturate_cast<uchar>( xg + yg);
	}
}


imshow("xy_grad window", xy_grad);
imshow("x_grad window", x_grad);
imshow("y_grad window", y_grad);

waitKey(0);

return 0;

}

4、拉普拉斯算子

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

//拉普拉斯算子 原理:二階微分,最大變化處的值爲零,即邊緣是零值。通過二階導數計算;
/*
1、高斯模糊-去噪聲GaussianBlur
2、轉化爲灰度圖像 cvtColor()
3、拉普拉斯二階導數計算
4、取絕對值 convertScaleAbs()
5、顯示結果
*/
int main()
{

	Mat srcImg = imread("C:/Users/admin/Desktop/1.JPG");
	if (!srcImg.data)
	{
		printf("沒有找到!!!");
		return -1;
	}
	//1、高斯模糊 - 去噪聲GaussianBlur
	Mat gaussianBlur_image;
GaussianBlur(srcImg, gaussianBlur_image, Size(3, 3), 0, 0);
//2、轉化爲灰度圖像 cvtColor()
Mat gray_img;
cvtColor(gaussianBlur_image, gray_img, CV_BGR2GRAY);
//3、拉普拉斯二階導數計算
Mat laplance_img;
Laplacian(gray_img, laplance_img, CV_16S, 3);
//4、取絕對值 convertScaleAbs()
convertScaleAbs(laplance_img, laplance_img);
//5、顯示結果
imshow("laplance window", laplance_img);

threshold(laplance_img, laplance_img, 0, 255, THRESH_OTSU | THRESH_BINARY);
imshow("laplance1 window", laplance_img);
namedWindow("srcImg windows", CV_WINDOW_AUTOSIZE);
imshow("srcImg windows", srcImg);
waitKey(0);
return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章