160 lines
4.0 KiB
C++
160 lines
4.0 KiB
C++
#include <iostream>
|
|
#include "opencv2/opencv.hpp"
|
|
|
|
void hough_circles() {
|
|
cv::Mat src = cv::imread("../../resources/images/coins.png", cv::IMREAD_GRAYSCALE);
|
|
cv::Mat blurred;
|
|
|
|
cv::GaussianBlur(src, blurred, cv::Size(), 2.f);
|
|
|
|
cv::imshow("s", blurred);
|
|
cv::waitKey();
|
|
|
|
std::vector<cv::Vec3f>circles;
|
|
cv::HoughCircles(blurred, circles, cv::HOUGH_GRADIENT, 1, 50, 150, 30);
|
|
|
|
cv::Mat dst;
|
|
cv::cvtColor(src, dst, cv::COLOR_GRAY2BGR);
|
|
|
|
for (auto c : circles) {
|
|
cv::Point pt1(cvRound(c[0]), cvRound(c[1]));
|
|
int radius = cvRound(c[2]);
|
|
|
|
cv::circle(dst, pt1, radius, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
}
|
|
|
|
cv::imshow("src", src);
|
|
cv::imshow("dst", dst);
|
|
|
|
cv::waitKey();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
void hough_lines() {
|
|
cv::Mat src = cv::imread("../../resources/images/building.jpg", cv::IMREAD_GRAYSCALE);
|
|
cv::Mat blurred;
|
|
|
|
cv::GaussianBlur(src, blurred, cv::Size(), 2.f);
|
|
|
|
cv::Mat dx, dy, edge;
|
|
|
|
cv::Sobel(blurred, dx, CV_16SC1, 1, 0, cv::FILTER_SCHARR);
|
|
cv::Sobel(blurred, dy, CV_16SC1, 0, 1, cv::FILTER_SCHARR);
|
|
|
|
cv::Canny(dx, dy, edge, 50, 100);
|
|
|
|
std::vector<cv::Vec2f>lines;
|
|
cv::HoughLines(edge, lines, 1, CV_PI / 180, 250);
|
|
|
|
cv::Mat dst;
|
|
cv::cvtColor(edge, dst, cv::COLOR_GRAY2BGR);
|
|
|
|
for (size_t i = 0; i < lines.size(); i++) {
|
|
float r = lines[i][0], t = lines[i][1];
|
|
double cos_t = std::cos(t), sin_t = std::sin(t);
|
|
double x0 = r * cos_t, y0 = r * sin_t;
|
|
double alpha = 1000;
|
|
|
|
cv::Point pt1(cvRound(x0 + alpha * (-sin_t)), cvRound(y0 + alpha * cos_t));
|
|
cv::Point pt2(cvRound(x0 - alpha * (-sin_t)), cvRound(y0 - alpha * cos_t));
|
|
|
|
cv::line(dst, pt1, pt2, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
}
|
|
|
|
cv::imshow("src", src);
|
|
cv::imshow("dst", dst);
|
|
|
|
cv::waitKey();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
void canny_edge() {
|
|
cv::Mat src = cv::imread("../../resources/images/lenna.bmp", cv::IMREAD_GRAYSCALE);
|
|
cv::Mat blurred;
|
|
|
|
cv::GaussianBlur(src, blurred, cv::Size(), (double)3);
|
|
|
|
cv::Mat dx, dy, dst1, dst2, phase;
|
|
|
|
cv::Sobel(blurred, dx, CV_32FC1, 1, 0, cv::FILTER_SCHARR);
|
|
cv::Sobel(blurred, dy, CV_32FC1, 0, 1, cv::FILTER_SCHARR);
|
|
|
|
cv::phase(dx, dy, phase, true);
|
|
cv::normalize(phase, phase, 0, 255, cv::NORM_MINMAX);
|
|
phase.convertTo(phase, CV_8UC1);
|
|
|
|
cv::Sobel(blurred, dx, CV_16SC1, 1, 0, cv::FILTER_SCHARR);
|
|
cv::Sobel(blurred, dy, CV_16SC1, 0, 1, cv::FILTER_SCHARR);
|
|
|
|
cv::Canny(dx, dy, dst1, 50, 100);
|
|
cv::Canny(dx, dy, dst2, 50, 150);
|
|
|
|
cv::imshow("src", src);
|
|
cv::imshow("dst1", dst1);
|
|
cv::imshow("dst2", dst2);
|
|
cv::imshow("phase", phase);
|
|
|
|
cv::waitKey();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
void scharr() {
|
|
cv::Mat src = cv::imread("../../resources/images/lenna.bmp", cv::IMREAD_GRAYSCALE);
|
|
|
|
cv::Mat dx, dy, fmag, mag, phase;
|
|
|
|
cv::Sobel(src, dx, CV_32FC1, 1, 0, cv::FILTER_SCHARR);
|
|
cv::Sobel(src, dy, CV_32FC1, 0, 1, cv::FILTER_SCHARR);
|
|
|
|
cv::magnitude(dx, dy, fmag);
|
|
fmag.convertTo(mag, CV_8UC1);
|
|
|
|
cv::phase(dx, dy, phase, true);
|
|
cv::normalize(phase, phase, 0, 255, cv::NORM_MINMAX);
|
|
phase.convertTo(phase, CV_8UC1);
|
|
|
|
cv::Mat edge = mag > 150;
|
|
|
|
cv::imshow("src", src);
|
|
cv::imshow("mag", mag);
|
|
cv::imshow("edge", edge);
|
|
cv::imshow("phase", phase);
|
|
|
|
cv::waitKey();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
void partial() {
|
|
cv::Mat src = cv::imread("../../resources/images/lenna.bmp", cv::IMREAD_GRAYSCALE);
|
|
cv::Mat edge(src.rows, src.cols, CV_8UC1);
|
|
|
|
std::array<std::float_t, 3> filter = {-1, 0, 1};
|
|
|
|
cv::Mat kernalx(1, 3, CV_32F, filter.data());
|
|
cv::Mat kernaly(3, 1, CV_32F, filter.data());
|
|
|
|
cv::Mat derx, dery;
|
|
|
|
cv::filter2D(src, derx, -1, kernalx, cv::Point(-1, -1), 128);
|
|
cv::filter2D(src, dery, -1, kernaly, cv::Point(-1, -1), 128);
|
|
|
|
for (int k = 0; k < 256; k++) {
|
|
for (int i = 0; i < edge.rows; i++)
|
|
for (int j = 0; j < edge.cols; j++) {
|
|
if (std::sqrt(std::pow(derx.at<std::int8_t>(i, j), 2) + std::pow(dery.at<std::int8_t>(i, j), 2)) > k)
|
|
edge.at<std::int8_t>(i, j) = 0;
|
|
else
|
|
edge.at<std::int8_t>(i, j) = 255;
|
|
}
|
|
std::cout << k << std::endl;
|
|
cv::imshow("edge", edge);
|
|
cv::waitKey();
|
|
}
|
|
|
|
cv::waitKey();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
int main() {
|
|
hough_circles();
|
|
} |