Segmentation fault with OpenCV and Jetson multimedia API integration

For context I’m trying to integrate the jetson multimedia api sample 05_jpeg_encode with OpenCV, the idea is very simple, I need read a frame with OpenCV and save as a JPEG image with NVJPEGENC.
Now here’s the problem even though I can compile the program just fine, but if I execute it once I reach the cv::imread function the application crashes and prints “segmention fault”, this only happens if the image actually exists, otherwise it keeps going until another function realizes that the mat it’s actually empty.
The code is in a new folder “jpeg_encoder” that I created inside /usr/src/jetson_multimedia_api/samples/

Here’s the relevant code.

#include "NvUtils.h"
#include "NvJpegEncoder.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

int main()
    std::cout << "begin" << std::endl;

    //Fails right here
    cv::Mat im = cv::imread("sample.jpeg", cv::IMREAD_COLOR);
    std::cout << "read" << std::endl;

    encodeJpeg(im, 40);
    return 0;

I think the problem may lie with the Makefile. Since I’m not that well versed in the use of Makefile, my attempt to add OpenCV may be the cause. To make sure nothing wrong was going on with OpenCV I tried to run another small test that only reads an image with imread, prints the width and height and had no problems.

Here’s the Makefile

include /usr/src/jetson_multimedia_api/samples/

APP := nvjpeg_test

SRCS := \
	nvjpeg_test.cpp \
	$(wildcard $(CLASS_DIR)/*.cpp)

OBJS := $(SRCS:.cpp=.o)
CV_CFLAGS = `pkg-config --cflags opencv4`
CV_LIBS = `pkg-config --libs opencv4`

all: $(APP)

$(CLASS_DIR)/%.o: $(CLASS_DIR)/%.cpp

%.o: %.cpp
	@echo "Compiling: $<"
	$(CPP) $(CPPFLAGS) -c $<

$(APP): $(OBJS)
	@echo "Linking: $@"

	$(AT)rm -rf $(APP) $(OBJS)

OpenCV links to and does not work with please check explanation in

You can try to decode the image through NvJpegDecoder instead of cv::imread().

I see, this was just a little test. In my real app the cv::mat comes from my camera so I never use imread, so there shouln’t be a problem. Thanks a lot I though I was crazy for a moment.