Ask Your Question
0

Color edges with Canny Algorithm

asked 2017-05-17 04:18:22 -0500

klinkenstein gravatar image

updated 2017-05-17 06:05:27 -0500

I'm using the Canny Edge Detector with the code from the example on opencv documentary (http://docs.opencv.org/2.4/doc/tutori...)

There are edges with white pixel in the result, but also color pixel depending on the original image. Why isn't the result image not only a greyscale image? When are the color edges used and when are only white pixel given back. I understand the approach from the sobel operator and the following act of the canny algorithm. But I can explain why there are color lines in the result image. Could somebody help me out there?

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables

Mat src, src_gray;
Mat dst, detected_edges;

int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";

/**
 * @function CannyThreshold
 * @brief Trackbar callback - Canny thresholds input with a ratio 1:3
 */
void CannyThreshold(int, void*)
{
  /// Reduce noise with a kernel 3x3
  blur( src_gray, detected_edges, Size(3,3) );

  /// Canny detector
  Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

  /// Using Canny's output as a mask, we display our result
  dst = Scalar::all(0);

  src.copyTo( dst, detected_edges);
  imshow( window_name, dst );
 }


/** @function main */
int main( int argc, char** argv )
{
  /// Load an image
  src = imread( argv[1] );

  if( !src.data )
  { return -1; }

  /// Create a matrix of the same type and size as src (for dst)
  dst.create( src.size(), src.type() );

  /// Convert the image to grayscale
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Create a window
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  /// Create a Trackbar for user to enter threshold
  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );

  /// Show the image
  CannyThreshold(0, 0);

  /// Wait until user exit program by pressing a key
  waitKey(0);

  return 0;
  }
edit retag flag offensive close merge delete

Comments

are you using color images as input ?

berak gravatar imageberak ( 2017-05-17 04:28:06 -0500 )edit

I do, but they get converted into greyscale images.

klinkenstein gravatar imageklinkenstein ( 2017-05-17 04:34:58 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-17 06:40:52 -0500

berak gravatar image

updated 2017-05-17 06:46:04 -0500

look at this:

/// Using Canny's output as a mask, we display our result
dst = Scalar::all(0);

src.copyTo( dst, detected_edges);
imshow( window_name, dst );

so, the "color" is only a result of the visualization. if you want the "raw" Canny output, just display that instead:

imshow("canny",  detected_edges);
edit flag offensive delete link more

Comments

Thank you!

klinkenstein gravatar imageklinkenstein ( 2017-05-17 09:00:48 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-05-17 04:18:22 -0500

Seen: 1,061 times

Last updated: May 17 '17