Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:27:33+00:00 2026-06-16T07:27:33+00:00

I have the following code compiled in linux terminal (c++ in linux) and am

  • 0

I have the following code compiled in linux terminal (c++ in linux) and am using OpenCv 2.4.3.

However, am getting a segmentation fault in run time and I really have no clue as to why. I have placed differnt cout statements to know if the program processed to the particular stage but in vain. Could you please help me? Please explain me what exactly is this segmentation fault. Am stuck here for a long time.

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

using namespace cv;
using namespace std;

int main()
{           
    cout<<"check"<<flush;
    Mat src,src_gray,dst;
    int kernel_size = 3;
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;
    char* window_name = "sharpness estimate";

    int freq,rows,cols =0;
    double *estimate,*min = 0;
    Point *minLoc,*maxLoc = 0;
    src = imread("/home/siddarth/examplescv/erez images/image53.jpg");
    if( !src.data )
    {
        return -1;
    }

    namedWindow(window_name,CV_WINDOW_AUTOSIZE);
    Mat abs_dst;
    cvtColor(src,src_gray,CV_RGB2GRAY);

    Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
    convertScaleAbs(dst, abs_dst);

    minMaxLoc(dst,min,estimate,minLoc,maxLoc,noArray());
    Size s = dst.size();
    rows = s.height;
    cols = s.width;
    cout<<rows<<endl<<cols<<endl;

    for(int i=0;i<=rows;i++)
    {
        for(int j=0;j<=cols;j++)
        {
            if(dst.at<double>(i,j) >= *estimate-100
               && dst.at<double>(i,j) <= *estimate+100)
            {
                cout<<freq++;
            }
        }
    }

    cout<<"estimate :"<<*estimate<<endl;
    cout<<"frequency :"<<freq<<endl;
    imshow(window_name,abs_dst);
    waitKey(1000);
    return 0;   
 }

The code doesn’t cross the first “check” print statement just after the main function declaration. That is the confusing issue. But once I flushed the first print statement, it got executed. I am still facing issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:27:34+00:00Added an answer on June 16, 2026 at 7:27 am

    Make sure you insert std::endl into cout so that the buffer is flushed. This will probably be why you’re not seeing any output.

    One immediate issue is that your for loops check the condition with <=, meaning that you’re probably going one past the end. But since you’re using at, you should have an exception thrown (assuming this Mat type acts like a standard container).

    Also, you’re creating lots of pointers to pass as some function arguments (for example, double* estimate). This doesn’t actually give you a double object though, just a pointer. Unless the function you’re passing them to is allocating a double for you (which I hope it’s not), you’re doing it wrong. You should be doing:

    double estimate;
    minMaxLoc(/* ... */, &estimate, /* ... */);
    

    You’ll need to do that with all of the values you’re getting through output parameters.

    Another thing to note: Doing int i, j = 0; only initialises j to 0, but not i. You need to do int i = 0, j = 0;.


    Okay, I’m going to explain why fixing the initialisers works. I had to look up the definition of minMaxLoc to see what happens. Basically, the function is something like the following:

    void setToFive(int* x)
    {
      if (x) {
        *x = 5;
      }
    }
    

    This function will take a pointer to an int, and then set that int to the value 5. However, if the pointer passed is a null pointer, the value will not be set (otherwise there’ll be undefined behaviour because you’re derefencing a null pointer). Basically, passing a null pointer says “I don’t care about this value so don’t give it to me”.

    Now when you were initialising your pointers, you were doing:

    double *estimate, *min = 0;
    

    This only sets min to the null pointer. Since estimate is left uninitialized, you can’t rely on its value being null. You need to provide an initialiser for each declarator:

    double *estimate = 0, *min = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code compiled by gcc: #include <iostream> using namespace std; class
I have the following line of code which when compiled with GHC it goes
I have following code snippet that i use to compile class at the run
I am using Lazarus v0.9.30 (32 bit compiler). I have the following code that
I have included the following header files in a C code using openssl libraries.
I'm starting with assembler under Linux. I have saved the following code as testasm.c
I have the following code which compiles without errors under Linux and Mac OS
Sage is supposed to be able to create compiled code using Cython. I have
gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template<typename realT> class B
I have the following assembly code (written for NASM on Linux): ; This code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.