In this post I will show some ways to detect edges using OpenCV.
この投稿では、OpenCVを使用してエッジを検出する二つの方法を示します。
Nest post, mostrarei algumas formas de detectar bordas utilizando OpenCV.
We'll use this image:
このイメージを使います:
Nós vamos usar esta imagem:
First we going to detect edges using the Scharr function, we start by adding the OpenCV headers.
まずはScharrファンクションを使ってエッジを検出します、OpenCVヘッダーを追加することからはじめます。
Primeiro vamos detectar bordas usando a função Scarr, Começamos adicionandos os cabeçalhos do OpenCV.
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
Creating and loading the image...
イメージを作成してロードします。
Criando e carregando a image...
cv::Mat image, image_blurred, scharrEdges;
image = cv::imread("./squirrel_cls.jpg");
Now we blur the image and convert to gray scale.
イメージをぼかし、グレースケールに変換します。
Agora nós desfocamos a imagem e convertemos para escala cinza.
// Blur image with a Gaussian kernel to remove edge noise
cv::GaussianBlur(image, image_blurred, cv::Size(3, 3), 0, 0);
// Convert to gray
cv::Mat image_gray;
cv::cvtColor(image_blurred, image_gray, cv::COLOR_BGR2GRAY);
Now we calculate the gradients in the X and Y directions.
X方向とY方向の勾配を計算します。
Agora podemos calcular os gradientes nas direções X e Y.
// Gradients in X and Y directions
cv::Mat grad_x, grad_y;
cv::Scharr(image_gray, grad_x, CV_32F, 1, 0);
cv::Scharr(image_gray, grad_y, CV_32F, 0, 1);
Calculating the overall gradient
全体的な勾配を計算します。
Calculando o gradiente geral.
// Calculate overall gradient
cv::pow(grad_x, 2, grad_x);
cv::pow(grad_y, 2, grad_y);
cv::Mat grad = grad_x + grad_y;
cv::sqrt(grad, grad);
Convert the image to 8-bit depth, so we can finally display the final result.
イメージを8ビットの深さに変換します、今度は最終なイメージを見せることができます。
Converte a imagem para 8 bits, para que possamos finalmente exibir o resultado final.
// Convert to 8 bit depth for displaying
grad.convertTo(scharrEdges, CV_8U);
// Display the original image
cv::imshow("Original image", image);
// Display the final image
cv::imshow("Scharr edges", scharrEdges);
And the final image should look like this:
そして最終結果は次のようになります。
E a imagem final deve ficar assim:
Now let’s use the Canny function to find edges in the same image, but to use Canny we have to blur the image first.
それでは、Cannyファンクションを使って同じイメージのエッジを見つけましょう、しかし私たちは最初にイメージをぼかす必要があります。
Agora vamos usar a função Canny para encontrar bordas na mesma imagem, mas para usar Canny temos que desfocar a imagem primeiro.
cv::Mat image, image_blurred, cannyEdges;
image = cv::imread("./squirrel_cls.jpg");
// Blur image with a Gaussian kernel to remove edge noise
cv::GaussianBlur(image, image_blurred, cv::Size(3, 3), 0, 0);
cv::Canny(image_blurred, cannyEdges, 150, 150);
imshow("Original image", image);
imshow("Canny edges", cannyEdges);
And the final image should look like this:
そして最終的なイメージはこのようにみえるべきです。
E a imagem final deve ficar assim:
You can see the full code at:
tensaidn->opencv_experiments
Você pode ver o código completo em:
tensaidn->opencv_experiments
完了