Problem "segmentation fault" opencv !

asked 2014-06-11 08:47:38 -0500

abir gravatar image

Hello When compiling the program I have a defect that called segmentation fault. I do not know the cause of this error. help me think you

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream>
    #include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

using namespace std;
using namespace cv;

        void show_histogram(std::string const& name, cv::Mat & image)
        {
            // Set histogram bins count
            int bins = 180;
            int histSize[] = {bins};
            // Set ranges for histogram bins
            float lranges[] = {0, 179};
            const float* ranges[] = {lranges};
            // create matrix for histogram
            cv::Mat hist;
            int channels[] = {0};

            // create matrix for histogram visualization
            int const hist_height = 180;
            cv::Mat3b hist_image = cv::Mat3b::zeros(hist_height, bins);

            cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges, true, false);

            double max_val=0;
            minMaxLoc(hist, 0, &max_val);

            // visualize each bin
            for(int b = 0; b < bins; b++) {
                float const binVal = hist.at<float>(b);
                int   const height = cvRound(binVal*hist_height/max_val);
                cv::line
                    ( hist_image
                    , cv::Point(b, hist_height-height), cv::Point(b, hist_height)
                    , cv::Scalar::all(180)
                    );
            }
            cv::imshow(name, hist_image);
        }

/**
 * @function main
 */

int main( int argc, char** argv )
{
 clock_t start, end;

  start = clock ();
  {
//    /* Portion de code a chronometrer */

  Mat src, src_gray;
  Mat resultat,concatMat;
  char* window_name = "Image";
  //char* window_name1 = "Sobel Dx";
  //char* window_name2 = "Sobel Dy";
  int scale = 1;
  int delta = 0;
  int ddepth = CV_16S;//
  bool angleInDegrees;
  int c;
  string  name;
  float min_value,max_value;
  int min_idx, max_idx;
  short i;
  short j;
  short k;
  short l;
  short temp;

/// Load an image

    Mat C = imread("I2.tif",1);
        if(C.empty())
            {
            cout << "Could not open or find the image" << std::endl ;
            return -1;
            }

/// Convert it to gray
cvtColor( C, src_gray, CV_RGB2GRAY );
/// Create window
    int h = C.rows;
    int w = C.cols;
    printf("Processing a %dx%d image\n",h,w);

/// Generate grad_x and grad_y
    Mat grad_x, grad_y,dst;
    Mat abs_grad_x, abs_grad_y;

/// Gradient X
    Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );

    convertScaleAbs(grad_x, abs_grad_x);

/// Gradient Y
    Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

    convertScaleAbs(grad_y, abs_grad_y);

/// Total Gradient (approximate)
//addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

    Mat angle = Mat::zeros(abs_grad_x.rows, abs_grad_y.cols,CV_32F);
    Mat magnitude = Mat::zeros(abs_grad_x.rows, abs_grad_y.cols,CV_32F);
    grad_x.convertTo(grad_x,CV_32F);
    grad_y.convertTo(grad_y,CV_32F);

    cartToPolar(grad_x, grad_y, magnitude , angle , angleInDegrees=true);
    show_histogram(name,angle);

    vector< float> Vf2;
/// copy mat to vector
    Vf2.assign((float*)angle.datastart, (float*)angle.dataend);
    int ind=0;
//confirm
     int cnt=0;
     for(int i=0; i< w; ++i)
      {
      for(int j=0; j< h; ++j)
      Vf2[cnt++];
//printf("%3.0f ", Vf2[cnt++]);
     }

/// Nombre d'occurence
    int iOcc[180];

    for ( i=0; i<=180; i++) iOcc[i] = 0;

    for (i=0; i<w*h; i++)
        {
        if (Vf2[i]>180)
            {
            Vf2[i]=Vf2[i]-180;
            }
        j = Vf2[i];
        iOcc[j] += 1;
        }

    for (i=0; i<=180; i++)
        {
printf("angle : %d --> Occurrence : %d\n", i, iOcc[i]);
        }

 }
      end = clock ();
  printf ("Temps en secondes : %f\n ...
(more)
edit retag flag offensive close merge delete

Comments

3

buffer overrun:

int iOcc[180];

for ( i=0; i<=180; i++) iOcc[i] = 0; // <-- please, < instead of <=

berak gravatar imageberak ( 2014-06-12 01:02:24 -0500 )edit