In this post I will show some ways to detect edges using OpenCV.
Nest post, mostrarei algumas formas de detectar bordas utilizando OpenCV.
     
We'll use this image:
Nós vamos usar esta imagem:
Galaxy Breakout1
First we going to detect edges using the Scharr function, we start by adding the OpenCV headers.
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.
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.
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:
Galaxy Breakout1
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.
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:
Galaxy Breakout1
You can see the full code at: tensaidn->opencv_experiments
Você pode ver o código completo em: tensaidn->opencv_experiments

 
 
 
 
 

完了