0

I know this question has been answered, but when I tried the solutions, It did not take me anywhere.

Below is the given code I have written to get the left and the right most boundary of an image, that was thresholded using canny edge detector, OpenCV.

#include<iostream>
#include<vector>
#include<math.h>
#include<string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
        int thresh = 100,rows = 0,cols = 0;;
        Mat src,src_gray,canny_output;
        src = imread( argv[1]);
        cvtColor( src, src_gray, CV_BGR2GRAY );
        blur( src_gray, src_gray, Size(3,3) );
        Canny( src_gray, canny_output, thresh, thresh*3, 3 );

        Mat boundary_image = Mat::zeros( canny_output.size(), CV_8UC1 );
        rows = canny_output.rows;
        cols = canny_output.cols;

        cout<<rows<<endl<<cols<<endl;

        for(int i=0;i<rows;i++)
        {
                for(int j=0;j<cols;j++)
                {
                        cout<<canny_output.at<uchar>(i,j)<<endl;
                        if(canny_output.at<uchar>(i,j) == 255)
                        {
                                boundary_image.at<uchar>(i,j) = 255;
                                break;
                        }
                }
                for(int k = cols;k>0;k--)
                {
                        if(canny_output.at<uchar>(i,k) == 255)
                        {
                                 boundary_image.at<uchar>(i,k) = 255;
                                 break;
                        }
                }
        }
        imshow("boundary_image",boundary_image);
        waitKey(0);
        return 0;
}

Whether the algorithm works or not is secondary, but am not able to view the value of the canny edge detected image. It is showing some symbols or empty values. Can you please tell me where I am going wrong?

Note: The question is with the print statement

cout<<canny_output.at<uchar>(i,j)<<endl;

which is not giving me any reasonable values at the output to view the pixel values. Similar questions where posted and the answer was to use uchar as the data type, but in my case, it is not working. It may sound rudimentary, but your help is greatly appreciated.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92
  • What exactly are you trying to achieve (ie, what is the desired output) ? The 2 inner loops over the columns are not very clear (at least to me). – sansuiso Apr 16 '15 at 13:32
  • @sansuiso the question is am not able to view the value of the pixels of the canny edge detected image. the line "cout<(i,j)< – Lakshmi Narayanan Apr 16 '15 at 13:34

1 Answers1

2

To correctly cout a unsigned char / uchar, you should cast it first, e.g.,

cout << (int)canny_output.at<uchar>(i,j) << endl;

To read on, check out Why "cout" works weird for "unsigned char"?

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174