Ask Your Question
1

Corner detection after edge detection

asked 2014-10-27 11:07:56 -0600

JavaEria gravatar image

updated 2020-11-06 05:26:24 -0600

I have used canny edge detection using the following code :

        blur( src, src_gray, Size(3,3) );
        Canny( src_gray, detected_edges, 20, 40, kernel_size );
        dst = Scalar::all(0);
        src.copyTo(dst,detected_edges);

This give me the right edged form , but if i use corner detection by taking the variable dst as input array for corner detection, i get exception although have converted dst to float type 32:

        dst.convertTo(temp, CV_32FC1); 
        cornerHarris( temp, dst, blockSize, apertureSize, k, BORDER_DEFAULT );
       normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
        convertScaleAbs( dst_norm, dst_norm_scaled );

        /// Drawing a circle around corners
        for( int j = 0; j < dst_norm.rows ; j++ )
        {
            for( int i = 0; i < dst_norm.cols; i++ )
            {
               if( (int) dst_norm.at<float>(j,i) > thresh )
               {
                      circle( dst_norm_scaled, Point( i, j ), 5,  Scalar(0), 2, 8, 0 );
          count++;
          }
      }
 }

I m using visual studio and it shows something like this:

image description

P.S: if i use the detected_edges variable instead of dst , i get no exception but wrong image: original image:

image description

result:

image description

SCREENSHOT Showing exception:

image description

                                Mat dst, dst_norm, dst_norm_scaled;
                //dst = Mat::zeros( src.size(), CV_32FC1 );
                temp = Mat::zeros( src.size(), CV_32FC1 );

                  /// Detector parameters
                int blockSize = 3;
                int apertureSize = 3;
                double k = 0.04;
                blur( src_gray, detected_edges, Size(3,3) );
                Canny( detected_edges, detected_edges, 20, 40, kernel_size );
                   dst = Scalar::all(0);
                   detected_edges.convertTo(detected_edges,CV_8U);
                   src.convertTo(src, CV_8UC1);
                   src.copyTo(dst,detected_edges);
                try{
                cornerHarris( dst, temp, blockSize, apertureSize, k, BORDER_DEFAULT );

                /// Normalizing
                 normalize( temp, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
                     convertScaleAbs( dst_norm, dst_norm_scaled );
                  /// Drawing a circle around corners
                for( int j = 0; j < dst_norm.rows ; j++ )
                    { for( int i = 0; i < dst_norm.cols; i++ )
                          {
                                if( (int) dst_norm.at<float>(j,i) > thresh )
                                    {
                                        circle( dst_norm_scaled, Point( i, j ), 5,  Scalar(0), 2, 8, 0 );
                                    }
                          }
                    }
                namedWindow( corners_window, CV_WINDOW_AUTOSIZE );
                imshow( corners_window, dst_norm_scaled );
                }
                catch(Exception e)
                {
                    e.err;
                }
edit retag flag offensive close merge delete

Comments

What is the exception?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 11:11:53 -0600 )edit
1

Have you tried my answer? CV_8FC1 should be the problem, I think is the size of the image element.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 11:42:44 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2014-10-27 11:16:41 -0600

thdrksdfthmn gravatar image

updated 2014-10-27 12:07:13 -0600

If you just look in the cornerHarris() documentation, you will see that:

src – Input single-channel 8-bit or floating-point image.

This means that you must convert it to CV_8UC1 or CV_32F (which is the value of all the 3 channels: 32 = 8*3).

edit flag offensive delete link more

Comments

Can u please tell me how will i change this conversion to the required one(I am new to opencv): dst.convertTo(temp, CV_32FC1);

JavaEria gravatar imageJavaEria ( 2014-10-27 11:37:38 -0600 )edit

I tried with single channel 8 bit:

               src.copyTo(dst,detected_edges); 
               dst.convertTo(temp, CV_8UC1); 
               cornerHarris( temp, dst, blockSize, apertureSize, k, BORDER_DEFAULT );
           normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_8UC1, Mat() );
               convertScaleAbs( dst_norm, dst_norm_scaled );

It still gives exception

JavaEria gravatar imageJavaEria ( 2014-10-27 11:49:50 -0600 )edit

It is CV_8FC1. float, not unsigned char ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 11:51:27 -0600 )edit

But its showing me this error before building :

1 IntelliSense: identifier "CV_8FC1" is undefined

JavaEria gravatar imageJavaEria ( 2014-10-27 11:54:33 -0600 )edit

you are right, there is not such a type...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 12:04:19 -0600 )edit

Ohh, so wat shud i do then ?

JavaEria gravatar imageJavaEria ( 2014-10-27 12:07:34 -0600 )edit

Sorry about that, what are the values of blockSize and apertureSize and k?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 12:09:04 -0600 )edit

Here you go ,

             /// Detector parameters
             int blockSize = 2;
             int apertureSize = 3;
             double k = 0.04;
JavaEria gravatar imageJavaEria ( 2014-10-27 12:11:56 -0600 )edit

it is not mentioned, but have you tried int blockSize = 3; or grater than 2? I had not time to test your code, but I'll do it today, and I'll tell you what I am getting.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-28 03:17:39 -0600 )edit

I tried blocksize 3, and used CV_32F and CV_8FC1 both giving exception still, but i have found that after canny edge detectiion when i copy the masked output of detected edges to the source using this

                    src.copyTo(dst,detected_edges);

Im getting exception as when i directly use detected_edges in corner there is no exception but the image is not right, so then there is a problem in this function even though i converted dst to 32F as well as 8UC1

                                       src.convertTo(dst, CV_32F);
                                       cornerHarris( dst, temp, blockSize, apertureSize, k, BORDER_DEFAULT );

Giving exception still

JavaEria gravatar imageJavaEria ( 2014-10-31 05:35:03 -0600 )edit

Question Tools

Stats

Asked: 2014-10-27 11:07:56 -0600

Seen: 1,013 times

Last updated: Nov 01 '14