Ask Your Question

Revision history [back]

Segmentation fault (core dumped) | C++

I was trying the tutorial at the following link:-

OpenCV Basics - 04 - Accessing Pixels using at Method

This tutorial uses OpenCV3 whereas I have OpenCV4. So there might be an issue due to that. Below is the code that I am using the modify an image pixel-by-pixel:-


#include <iostream>
#include <stdint.h>
#include <opencv4/opencv2/opencv.hpp>

using namespace cv;
using namespace std;


int main(){    
    Mat original = imread("who1.jpg", IMREAD_GRAYSCALE);
    Mat modified = imread("who1.jpg", IMREAD_GRAYSCALE);
    for(int r = 0; r < modified.rows; r++){
        for(int c = 0; modified.rows;c++){
            modified.at<uint8_t>(r,c) = modified.at<uint8_t>(r,c) * 0.5f;
        }
    }
    imshow("Original", original);imshow("Modified", modified);

    waitKey();

}

And I use the following bash command to to compile & run this code and I get the error:-

g++-9.3 -std=c++11 ./asd.cpp -o asd `pkg-config opencv --cflags --libs`; ./asd
Segmentation fault (core dumped)

Please keep in mind that I am not very literate in C++ as I have just started to learn it. Thank you!