LoginSignup
5
6

More than 5 years have passed since last update.

OpenCVでエッジ検出

Last updated at Posted at 2019-02-04

前回の続き, 今回はSobel, Laplacian, Canny法を用いたエッジ検出を行う.
今回は, 用意するものなどは割愛する.

ソースコード

Main.cpp
#include<opencv2/opencv.hpp>

int main()
{
    //グレースケールで画像を読み込む
    cv::Mat img = cv::imread("sample.jpg", 0);
    cv::Mat img_c, img_l, img_s_x, img_s_y, img_s;
    //Canny法を用いてエッジ検出
    //閾値は0 ~ 255で任意で決める
    //第1引数が入力, 第2引数が出力
    cv::Canny(img, img_c, 125, 255);

    //Laplacianでエッジ検出
    //第3引数は検出範囲
    cv::Laplacian(img, img_l, 3);
    cv::convertScaleAbs(img_l, img_l, 1, 0);
    //2値化
    cv::threshold(img_l, img_l, 0, 255, cv::THRESH_BINARY|cv::THRESH_OTSU);

    //Sobelでエッジ検出
    //x, y方向の微分
    cv::Sobel(img, img_s_x, CV_8UC1, 1, 0, 3);
    cv::Sobel(img, img_s_y, CV_8UC1, 0, 1, 3);
    //x, y方向に微分をした画像を合成
    img_s = abs(img_s_x) + abs(img_s_y);
    cv::convertScaleAbs(img_s, img_s, 1, 0);
    //2値化
    cv::threshold(img_s, img_s, 0, 255, cv::THRESH_BINARY|cv::THRESH_OTSU);

    //画像の保存と出力
    cv::imwrite("canny.jpg", img_c);
    cv::imwrite("laplace.jpg", img_l);
    cv::imwrite("sobel.jpg", img_s);
    cv::imshow("Canny", img_c);
    cv::imshow("Laplace", img_l);
    cv::imshow("Sobel", img_s);
    cv::waitKey(0);

    return 0;
}

入力画像
sample.jpg
出力画像
Canny法
out.jpg

Laplacian
laplace.jpg

Sobel
sobel.jpg

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6