Giving error malloc(): memory corruption: and Segementation fault

asked 2016-09-15 02:26:45 -0600

ak1 gravatar image

updated 2016-09-15 04:57:14 -0600

Pose.h

    #ifndef POSE_H
    #define POSE_H

    #include "opencv2/objdetect/objdetect.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/calib3d/calib3d.hpp"
    #include "opencv2/core/core.hpp"
    #include <iostream>
    #include <math.h>


    using namespace std;
    using namespace cv;

    class Pose
    {
    public:
    Pose();

    double* contour_detection(Mat threshold,Mat src);
    double* pose_estimation(const std::vector<cv::Point3f> &objectPoints,const std::vector<cv::Point2f> &imagePoints);
    private:
vector<Point3f> objectPoints ;
Mat  cameraIntrinsicParams ;
Mat distCoeffs;
    int index[100];
    double area_array[100];
};
    #endif // POSE_H

Pose.cpp

    #include "Pose.h"


    Pose::Pose()
    {
    objectPoints.push_back(cv::Point3f(-50.0f,-50.0f,0.0f));
objectPoints.push_back(cv::Point3f(-50.0f,50.0f,0.0f));
objectPoints.push_back(cv::Point3f(50.0f,50.0f,0.0f));
objectPoints.push_back(cv::Point3f(50.0f,-50.0f,0.0f));



    cameraIntrinsicParams=Mat(Size(3,3),CV_32FC1);


    cameraIntrinsicParams.at<float>(0,0)=  694.507824f;
cameraIntrinsicParams.at<float>(0,1)= 0 ;
cameraIntrinsicParams.at<float>(0,2)= 329.707540f;//304.729528f;
cameraIntrinsicParams.at<float>(1,0)= 0 ;
cameraIntrinsicParams.at<float>(1,1)=  694.579263f;
cameraIntrinsicParams.at<float>(1,2)= 260.350615f;//235.217420f;
cameraIntrinsicParams.at<float>(2,0)= 0 ;
cameraIntrinsicParams.at<float>(2,1)= 0 ;
cameraIntrinsicParams.at<float>(2,2)= 1 ;



   distCoeffs=Mat(Size(5,1),CV_32FC1);
   distCoeffs.at<float>(0,0)=0.015950f;
   distCoeffs.at<float>(1,0)=-0.224494f;
   distCoeffs.at<float>(2,0)=0.002544f;
   distCoeffs.at<float>(3,0)=0.000194f;
   distCoeffs.at<float>(3,0)=0.00f;
   }

   double* Pose::contour_detection(Mat threshold, Mat src)
   {
int flag =0;
double largest_area=0.0;
int largest_contour_index=0;
int lowThreshold;
int ratio=3;
int kernel_size=3;
    int const max_lowThreshold = 100;
    double *location;
    static double check[3]={0.0,0.0,0.0};
    int j=0;
    //int index[100];
    //double area_array[100];
    //double *condition;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
Rect boundRect;
RotatedRect box;
vector<Point>Points ;
vector< vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
Mat detected_edges;
blur(threshold, detected_edges, Size(3,3) );

    /// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
vector< vector<Point> > contours0;
    findContours( detected_edges, contours0, hierarchy,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

contours.resize(contours0.size());
std::vector<Point2f> imagePoints;
    std::vector<Point2f> preciseCorners(4);
    int k;

    for( k=0; k < contours0.size(); k++ )
    {
           cout<<"size"<<contours0.size()<<endl;
            //double eps = contours0[k].size() * 0.05;
    approxPolyDP(contours0[k], contours[k],9, true);

            if (contours[k].size() != 4)
    continue;
            if (!cv::isContourConvex(contours[k]))
    continue;

            if(contours[k].size()==4)
    {

    index[j]=k;
            j=j+1;

    }

     }

    if (contours0.size()==0)
    {             
    return check;
    }

if(j>1)
{

            cout<<"5"<<endl;
    for( int i = 0; i<j; i++ )
    {
              cout<<"6"<<endl;

                    area_array[i]=contourArea( contours[index[i]],false);
            if(area_array[i]>largest_area)
        {
        largest_area=area_array[i];
            largest_contour_index=i;                //Store the index of largest contour
        }
    }

}   

    if(j>1) 
{    
    for( int i = 0; i<j; i++ )
            {

        double ratio =largest_area/area_array[i];

        if(ratio>=3.0 || ratio<=5.0)
                {    

            for (int c=0;c<4;c++)
            {       
            preciseCorners[c] = contours[index[largest_contour_index]][c];
            }

            cv::cornerSubPix(threshold, preciseCorners, cvSize(5 ...
(more)
edit retag flag offensive close merge delete

Comments

Please help me !!

ak1 gravatar imageak1 ( 2016-09-15 02:27:39 -0600 )edit

What's the question? Where do you get the error?

Did you try to debug your program?

kbarni gravatar imagekbarni ( 2016-09-15 03:12:59 -0600 )edit

please show the code for pose_estimation().

and i guess - you're returning a pointer to something local.

using double* is already a red flag here !

berak gravatar imageberak ( 2016-09-15 03:22:45 -0600 )edit

@ berak , I have added the pose_estimation() function at the end of code.

ak1 gravatar imageak1 ( 2016-09-15 04:58:37 -0600 )edit

@ kbarni , i have tries to debug the code. I found out the part which might me giving error. the for loop of approximating the contours ... for( k=0; k < contours0.size(); k++ ) { ... }

ak1 gravatar imageak1 ( 2016-09-15 05:01:12 -0600 )edit
1

That pointer return in the pose_estimation() function should be avoided, as berak said. I wouldn't declare static variable in a function either.

Replace that static double position[4]; with Vec4f position and make the function to return Vec4f.

kbarni gravatar imagekbarni ( 2016-09-15 05:03:03 -0600 )edit

okay i will try it @kbarni

ak1 gravatar imageak1 ( 2016-09-15 05:09:54 -0600 )edit

@berak i tried the way you suggest but giving me same memory corruption error.

ak1 gravatar imageak1 ( 2016-09-15 06:00:24 -0600 )edit

sorry @ kbarni

ak1 gravatar imageak1 ( 2016-09-15 06:02:35 -0600 )edit