Imread() failes when I try to load image BUT ONLY when I try to compile with CMAKE

Hi!
I’m writing a program that uses both OpenCV and Jetson Multimedia Api.
Now I’m trying to compile this very simple program:

#include “NvUtils.h”
#include <errno.h>
#include
#include
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <opencv2/opencv.hpp>

using namespace std;

inline cv::Mat loadImg(string address = “//home//jetson//Desktop//img1.jpeg”) {
cout << “add: " << address << endl;
cv::Mat mtr = cv::imread(address);
cout << “rows: " << mtr.rows << endl;
cv::imshow(””,mtr);
cv::waitKey();
cout << “cvtColor();” << endl;
cv::cvtColor(mtr, mtr, cv::COLOR_BGR2YUV); //I420?
return mtr;
}

int main(int argc, char *argv) {
cv::Mat img = loadImg();
cv::imshow(“”, img);
cv::waitKey();
}

Now it works well with this compilation line (G++):

g++ -o prog jpeg_encode_main.cpp pkg-config --cflags --libs opencv4 -lpthread

But when I try to run it when it was compiled with CMAKE (files content below) it throws:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.8.0) /home/jetson/opencv/modules/highgui/src/window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

What am I missing here?
The code is exactly the same code and the address is hardcoded so there is no chance I made a typo.

I’m working on a copy of jetson_multimedia_api that I made in a folder on the Desktop, maybe it somehow affected the dirs?

Thanks for the help :slight_smile:

makefile:

include ../Rules.mk

APP := jpeg_encode

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

OBJS := $(SRCS:.cpp=.o)

all: $(APP)

$(CLASS_DIR)/%.o: $(CLASS_DIR)/%.cpp
	$(AT)$(MAKE) -C $(CLASS_DIR)

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

$(APP): $(OBJS)
	@echo "Linking: $@"
	$(CPP) -o $@ $(OBJS) $(CPPFLAGS) $(LDFLAGS)

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

Rules.mk (in the outer library):

CPPFLAGS :=
LDFLAGS :=

# Verbose flag
ifeq ($(VERBOSE), 1)
AT =
else
AT = @
endif

# ARM ABI of the target platform
ifeq ($(TEGRA_ARMABI),)
TEGRA_ARMABI ?= aarch64-linux-gnu
endif

# Location of the target rootfs
ifeq ($(shell uname -m), aarch64)
TARGET_ROOTFS :=
else
ifeq ($(TARGET_ROOTFS),)
$(error Please specify the target rootfs path if you are cross-compiling)
endif
endif

# Location of the CUDA Toolkit
CUDA_PATH 	:= /usr/local/cuda

# Use absolute path for better access from everywhere
TOP_DIR 	:= $(shell pwd | awk '{split($$0, f, "/samples"); print f[1]}')
CLASS_DIR 	:= $(TOP_DIR)/samples/common/classes
ALGO_CUDA_DIR 	:= $(TOP_DIR)/samples/common/algorithm/cuda
ALGO_TRT_DIR 	:= $(TOP_DIR)/samples/common/algorithm/trt

ifeq ($(shell uname -m), aarch64)
CROSS_COMPILE :=
else
CROSS_COMPILE ?= aarch64-unknown-linux-gnu-
endif
AS             = $(AT) $(CROSS_COMPILE)as
LD             = $(AT) $(CROSS_COMPILE)ld
CC             = $(AT) $(CROSS_COMPILE)gcc
CPP            = $(AT) $(CROSS_COMPILE)g++
AR             = $(AT) $(CROSS_COMPILE)ar
NM             = $(AT) $(CROSS_COMPILE)nm
STRIP          = $(AT) $(CROSS_COMPILE)strip
OBJCOPY        = $(AT) $(CROSS_COMPILE)objcopy
OBJDUMP        = $(AT) $(CROSS_COMPILE)objdump
NVCC           = $(AT) $(CUDA_PATH)/bin/nvcc -ccbin $(filter-out $(AT), $(CPP))

# Specify the logical root directory for headers and libraries.
ifneq ($(TARGET_ROOTFS),)
CPPFLAGS += --sysroot=$(TARGET_ROOTFS)
LDFLAGS += \
	-Wl,-rpath-link=$(TARGET_ROOTFS)/lib/$(TEGRA_ARMABI) \
	-Wl,-rpath-link=$(TARGET_ROOTFS)/usr/lib/$(TEGRA_ARMABI) \
	-Wl,-rpath-link=$(TARGET_ROOTFS)/usr/lib/$(TEGRA_ARMABI)/tegra \
	-Wl,-rpath-link=$(TARGET_ROOTFS)/$(CUDA_PATH)/lib64
endif

# All common header files
CPPFLAGS += -std=c++11 \
	-I"$(TOP_DIR)/include" \
	-I"$(TOP_DIR)/include/libjpeg-8b" \
	-I"$(ALGO_CUDA_DIR)" \
	-I"$(ALGO_TRT_DIR)" \
	-I"$(TARGET_ROOTFS)/$(CUDA_PATH)/include" \
	-I"$(TARGET_ROOTFS)/usr/include/$(TEGRA_ARMABI)" \
	-I"$(TARGET_ROOTFS)/usr/include/libdrm" \
	-I"$(TARGET_ROOTFS)/usr/include/opencv4" 
# All common dependent libraries
LDFLAGS += \
	-lpthread -lv4l2 -lEGL -lGLESv2 -lX11 \
	-lnvbuf_utils -lnvjpeg -lnvosd -ldrm \
	-lcuda -lcudart `pkg-config --cflags --libs opencv4`\
	-L"$(TARGET_ROOTFS)/$(CUDA_PATH)/lib64" \
	-L"$(TARGET_ROOTFS)/usr/lib/$(TEGRA_ARMABI)" \
	-L"$(TARGET_ROOTFS)/usr/lib/$(TEGRA_ARMABI)/tegra"

CPPFLAGS += $(shell pkg-config --cflags opencv4)
LDLIBS += $(shell pkg-config --libs opencv4)

Problem solved using g++:

g++ -std=c++11 -I/home/jetson/Desktop/b/jetson_multimedia_api/include -I/home/jetson/Desktop/b/jetson_multimedia_api/include/libjpeg-8b -I/home/jetson/Desktop/b/jetson_multimedia_api/samples/common/algorithm/cuda -I/home/jetson/Desktop/b/jetson_multimedia_api/samples/common/algorithm/trt -I/usr/local/cuda/include -I/usr/include/aarch64-linux-gnu -I/usr/include/libdrm -I/usr/include/opencv4 -o program jpeg_encode_main.cpp -pthread -lv4l2 -lEGL -lGLESv2 -lX11 -lnvbuf_utils -lnvjpeg -lnvosd -ldrm -lcuda -lcudart pkg-config --cflags --libs opencv4 -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu -L/usr/lib/aarch64-linux-gnu/tegra