본문 바로가기

OpenCV

ch09 hough

#python

import cv2
import numpy as np

def hough_lines():
    src = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch09\\hough\\building.jpg", cv2.IMREAD_GRAYSCALE)

    if src is None:
        print("Image load failed!")
        return

    edge = cv2.Canny(src, 50, 150)

    lines = cv2.HoughLines(edge, 1, np.pi / 180, 250)

    dst = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR)

    if lines is not None:
        for line in lines:
            rho, theta = line[0]
            cos_t = np.cos(theta)
            sin_t = np.sin(theta)
            x0 = rho * cos_t
            y0 = rho * sin_t
            alpha = 1000
            pt1 = (int(x0 - alpha * sin_t), int(y0 + alpha * cos_t))
            pt2 = (int(x0 + alpha * sin_t), int(y0 - alpha * cos_t))
            cv2.line(dst, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow("src", src)
    cv2.imshow("dst", dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def hough_line_segments():
    src = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch09\\hough\\building.jpg", cv2.IMREAD_GRAYSCALE)

    if src is None:
        print("Image load failed!")
        return

    edge = cv2.Canny(src, 50, 150)

    lines = cv2.HoughLinesP(edge, 1, np.pi / 180, 160, 50, 5)

    dst = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR)

    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(dst, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow("src", src)
    cv2.imshow("dst", dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def hough_circles():
    src = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch09\\hough\\coins.png", cv2.IMREAD_GRAYSCALE)

    if src is None:
        print("Image load failed!")
        return

    blurred = cv2.blur(src, (3, 3))

    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, 50, param1=150, param2=30)

    dst = cv2.cvtColor(src, cv2.COLOR_GRAY2BGR)

    if circles is not None:
        circles = np.uint16(np.around(circles))
        for circle in circles[0, :]:
            center = (circle[0], circle[1])
            radius = circle[2]
            cv2.circle(dst, center, radius, (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow("src", src)
    cv2.imshow("dst", dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def main():
    hough_lines()
    hough_line_segments()
    hough_circles()

if __name__ == "__main__":
    main()

// C++

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

using namespace cv;
using namespace std;

void hough_lines();
void hough_line_segments();
void hough_circles();

int main(void)
{
	hough_lines();
	hough_line_segments();
	hough_circles();

	return 0;
}

void hough_lines()
{
	Mat src = imread("building.jpg", IMREAD_GRAYSCALE);

	if (src.empty()) {
		cerr << "Image load failed!" << endl;
		return;
	}

	Mat edge;
	Canny(src, edge, 50, 150);

	vector<Vec2f> lines;
	HoughLines(edge, lines, 1, CV_PI / 180, 250);

	Mat dst;
	cvtColor(edge, dst, COLOR_GRAY2BGR);

	for (size_t i = 0; i < lines.size(); i++) {
		float rho = lines[i][0], theta = lines[i][1];
		float cos_t = cos(theta), sin_t = sin(theta);
		float x0 = rho * cos_t, y0 = rho * sin_t;
		float alpha = 1000;

		Point pt1(cvRound(x0 - alpha * sin_t), cvRound(y0 + alpha * cos_t));
		Point pt2(cvRound(x0 + alpha * sin_t), cvRound(y0 - alpha * cos_t));
		line(dst, pt1, pt2, Scalar(0, 0, 255), 2, LINE_AA);
	}

	imshow("src", src);
	imshow("dst", dst);

	waitKey(0);
	destroyAllWindows();
}

void hough_line_segments()
{
	Mat src = imread("building.jpg", IMREAD_GRAYSCALE);

	if (src.empty()) {
		cerr << "Image load failed!" << endl;
		return;
	}

	Mat edge;
	Canny(src, edge, 50, 150);

	vector<Vec4i> lines;
	HoughLinesP(edge, lines, 1, CV_PI / 180, 160, 50, 5);

	Mat dst;
	cvtColor(edge, dst, COLOR_GRAY2BGR);

	for (Vec4i l : lines) {
		line(dst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 2, LINE_AA);
	}

	imshow("src", src);
	imshow("dst", dst);

	waitKey(0);
	destroyAllWindows();
}

void hough_circles()
{
	Mat src = imread("coins.png", IMREAD_GRAYSCALE);

	if (src.empty()) {
		cerr << "Image load failed!" << endl;
		return;
	}

	Mat blurred;
	blur(src, blurred, Size(3, 3));

	vector<Vec3f> circles;
	HoughCircles(blurred, circles, HOUGH_GRADIENT, 1, 50, 150, 30);

	Mat dst;
	cvtColor(src, dst, COLOR_GRAY2BGR);

	for (Vec3f c : circles) {
		Point center(cvRound(c[0]), cvRound(c[1]));
		int radius = cvRound(c[2]);
		circle(dst, center, radius, Scalar(0, 0, 255), 2, LINE_AA);
	}

	imshow("src", src);
	imshow("dst", dst);

	waitKey(0);
	destroyAllWindows();
}

C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저


'OpenCV' 카테고리의 다른 글

ch10 coloreq  (0) 2024.05.21
ch10 backproj  (0) 2024.05.21
ch09 edges  (0) 2024.05.20
ch08 perspective  (0) 2024.05.20
ch08 affine  (0) 2024.05.20