Ask Your Question
0

Segmentation fault (core dumped) | C++

asked 2020-04-19 00:38:22 -0600

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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-21 04:22:06 -0600

berak gravatar image

imread() CAN fail, so please check like image.empty() before trying to process it.

also -- please try to avoid per-pixel loops, they're slow and error-prone.

your whole code coud be simply written as:

modified *= 0.5;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-04-19 00:38:22 -0600

Seen: 1,384 times

Last updated: Apr 19 '20