티스토리 뷰

영상처리/OpenCV

19. Canny operator

빠리빵 2019. 6. 12. 23:25

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>

using namespace std;
using namespace cv;

#define PI 3.1415926

class EdgeDetector {
private:
    // original image
	Mat img;
	// 16-bit signed int image
	Mat sobel;
	// Aperture size of the Sobel kernel
	int aperture;
	// Sobel magnitude
	Mat sobelMagnitude;
	// Sobel orientation
	Mat sobelOrientation;

public:
	EdgeDetector() : aperture(3) {}

	// Set the aperture size of the kernel
	void setAperture(int a) {
		aperture = a;
	}

	// Get the aperture size of the kernel
	int getAperture() const {
		return aperture;
	}

	// Compute the Sobel
	void computeSobel(const Mat& image) {
		Mat sobelX;
		Mat sobelY;

		// Compute Sobel
		Sobel(image, sobelX, CV_32F, 1, 0, aperture);
		Sobel(image, sobelY, CV_32F, 0, 1, aperture);

		imwrite("sobelX.bmp", sobelX);
		imwrite("sobelY.bmp", sobelY);

		// Compute magnitude and orientation
		cartToPolar(sobelX, sobelY, sobelMagnitude, sobelOrientation);
	}

	// Compute the Sobel
	void computeSobel(const Mat& image, Mat &sobelX, Mat &sobelY) {
		// Compute Sobel
		Sobel(image, sobelX, CV_32F, 1, 0, aperture);
		Sobel(image, sobelY, CV_32F, 0, 1, aperture);

		// Compute magnitude and orientation
		cartToPolar(sobelX, sobelY, sobelMagnitude, sobelOrientation);
	}

	// Get Sobel magnitude
	Mat getMagnitude() {
		return sobelMagnitude;
	}

	// Get Sobel orientation
	Mat getOrientation() {
		return sobelOrientation;
	}

	// Get a thresholded binary map
	Mat getBinaryMap(double threshold) {
		Mat bin;
		cv::threshold(sobelMagnitude, bin, threshold, 255, THRESH_BINARY_INV);

		return bin;
	}

	// Get a CV_8U image of the Sobel
	Mat getSobelImage() {
		Mat bin;

		double minval, maxval;
		minMaxLoc(sobelMagnitude, &minval, &maxval);
		sobelMagnitude.convertTo(bin, CV_8U, 255 / maxval);

		return bin;
	}

	// Get a CV_8U image of the Sobel orientation
	// 1 gray-level = 2 degrees
	Mat getSobelOrientationImage() {
		Mat bin;
		
		sobelOrientation.convertTo(bin, CV_8U, 90 / PI);

		return bin;
	}
};

int main() {
	Mat input_gray = imread("road.jpg", IMREAD_GRAYSCALE);
	
	// Compute Sobel
	EdgeDetector ed;
	ed.computeSobel(input_gray);

	// Apply canny algorithm
	Mat Canny_contours;
	Canny(input_gray,	// gray-level image
		Canny_contours, // output contours
		125,			// low threshold
		350);			// high threshold (255 * rootsquare(2) = 360.624..)

	imwrite("road_gray.bmp", input_gray);
	imwrite("Sobel_magnitude.bmp", ed.getMagnitude());
	imwrite("Sobel_orientation.bmp", ed.getSobelOrientationImage());
	imwrite("Sobel_low_threshold.bmp", ed.getBinaryMap(125));
	imwrite("Sobel_high_threshold.bmp", ed.getBinaryMap(350));	
	imwrite("Canny_contours.bmp", 255 - Canny_contours);
}

'영상처리 > OpenCV' 카테고리의 다른 글

21.Line fitting  (0) 2019.07.08
20. Hough Transform  (0) 2019.07.06
18. Bilateral filter  (0) 2019.06.02
17. Maximally stable external regions  (0) 2019.06.02
16. watershed transformation  (0) 2019.05.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함