I have an application that takes pictures from the camera, encode them, do some stuff and then decode them. It works on Ubuntu, but on OSX imgdecode is generating the following exception:
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(4.2.0) /tmp/opencv-20200404-3398-7w1b75/opencv-4.2.0/modules/imgcodecs/src/loadsave.cpp:732: error: (-215:Assertion failed) buf.checkVector(1, CV_8U) > 0 in function 'imdecode_'
Abort trap: 6
I tried to encode and decode the image in the same script to have a minimal verifiable example. The script is the following:
#include "opencv2/opencv.hpp"
#include<unistd.h>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat frame;
VideoCapture cam(0);
std::vector<uchar> buf_in;
u_char *buf ;
while(1)
{
cam>>frame;
imencode(".png",frame, buf_in);
//encode image and put data into the vector buf
frame = cv::imdecode(cv::Mat(3, buf_in.size(), CV_8UC3, buf_in.data()), 1);
imshow("window", frame);
waitKey(2);
}
return 0;
}
and it is still generating the exception on OSX. Why is that? Why is the exception generated? Why only on OSX?
POST SCRIPTA
If one needs to compile the code, the following Makefile can be used:
CXX = g++
CXXFLAGS = -std=c++11 -pg
INC_PATH = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
SOURCEDIR := ./
SOURCES := $(wildcard $(SOURCEDIR)/*.cpp)
OBJDIR=$(SOURCEDIR)/obj
OBJECTS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.o, $(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.cpp,$(OBJDIR)/%.d,$(SOURCES))
WARNING := -Wall -Wextra
.PHONY: all clean
all: openCV
clean:
$(RM) $(OBJECTS) $(DEPENDS) openCV
openCV: $(OBJECTS)
$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $# $(LIBS)
-include $(DEPENDS)
$(OBJDIR):
mkdir -p $(OBJDIR)
$(OBJDIR)/%.o: $(SOURCEDIR)/%.cpp Makefile | $(OBJDIR)
$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $#
I solved changing the argument of imdecode to be a vector<u_char> containing the raw data, instead of initialising a new Mat structure.
#include "opencv2/opencv.hpp"
#include<unistd.h>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat frame;
VideoCapture cam(0);
std::vector<uchar> buf_in;
u_char *buf ;
while(1)
{
cam>>frame;
imencode(".png",frame, buf_in);
//encode image and put data into the vector buf
buf_out.assign(buf_in.data, buf_in.data+buf_in.size());
frame = cv::imdecode(buf_out, 1);
imshow("window", frame);
waitKey(2);
}
return 0;
}
Related
I have a c project and I'm trying to link the C binding for imgui to it using Makefiles but I'm having some trouble. I've already generated the .dll using cmake in a build folder, these are the contents of it:
C:\path\to\cimgui\build:
-CMakefiles
-cimgui.dll
-cmake_install.txt
-CMakeCache.txt
-libcimgui.dll.a
-Makefile
and this is my makefile
PROJ := proj
TARGET := $(PROJ)
OBJ := sub.o main.o
INCLUDE_PATH := -IC:\path\to\SDL2 -IC:\path\to\cimgui
LIB_PATH := -LC:\path\to\SDL2\lib -LC:\path\to\cimgui\build
LINKER_FLAGS := -lSDL2 -lopengl32 -lcimgui -lcimgui.dll
CFLAGS := $(INCLUDE_PATH)
.PHONY : build clean
build : $(TARGET).exe
$(TARGET).exe: $(OBJ)
gcc $^ $(LIB_PATH) $(LINKER_FLAGS) -o $#
sub.o: sub.c sub.h
gcc -c $(CFLAGS) $< -o $#
main.o: main.c sub.h
$(CC) $(CFLAGS) $< -o $#
clean :
#rm -fv *.o
#rm -fv *.exe
and this is my main.c project
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_opengl.h>
int main(int argc, char ** argv)
{
return 0;
}
This is the error I get when i try to compile my makefile:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lcimgui.dll
I did try to compile a OpenCV-Project with MinGW (under Windows 10). The OpenCV-library had compiled without problem and I try to do couple of test.
main3.cpp
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
I have follows Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o
LINKOBJ = obj/main3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
All compiled and linked without mistakes nad run fine. I try to expand the test with new file 'second3.cpp'.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
extern "C" {
}
New the Makefile:
# Project: Proj400-v3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = obj/main3.o obj/second3.o
LINKOBJ = obj/main3.o obj/second3.o
LIBS = -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
INCS = -I"C:/RD/TMP/Dev-Cpp/MinGW64/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/RD/TMP/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
CXXINCS = -I"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/inc" -I"C:/RD/TMP/Work/entw/OCV-SVD/proj4.0.0.0"
BIN = obj/Proj400-v3.exe
CXXFLAGS = $(CXXINCS) -m32 -std=c++11 -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400
CFLAGS = $(INCS) -m32
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
obj/main3.o: main3.cpp
$(CPP) -c main3.cpp -o obj/main3.o $(CXXFLAGS)
obj/second3.o: second3.cpp
$(CPP) -c second3.cpp -o obj/second3.o $(CXXFLAGS)
After compiling I have follows list of mistakes:
- Command: mingw32-make.exe -f "C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win" all
g++.exe obj/main3.o obj/second3.o -o obj/Proj400-v3.exe -L"C:/RD/TMP/Dev-Cpp/MinGW64/lib32" -L"C:/RD/TMP/Work/entw/OCV-SVD/ocv4.0.0/lib" -lopencv_core400 -lopencv_imgcodecs400 -lopencv_highgui400 -m32
obj/main3.o:main3.cpp:(.text+0xc3): undefined reference to `cv::imread(std::string const&, int)'
obj/main3.o:main3.cpp:(.text+0x181): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2.exe: error: ld returned 1 exit status
C:\RD\TMP\Work\entw\OCV-SVD\proj4.0.0.0\Makefile.win:25: recipe for target 'obj/Proj400-v3.exe' failed
mingw32-make.exe: *** [obj/Proj400-v3.exe] Error 1
Can some body explane me, what is wrong? Why with one source in the project it work fine und with many files in the project I have 'undefined reference'?
PS: If I change the project from two source-files to one-source, but do not remove the obj/second3.o, I have again problem with 'undefined reference'.
Just before you read just know that i'm not english, so hopefully i won't misspell my writing here.
Anyway. I was trying to compile my first SDL program, so i followed online tutorials to install SDL2 libraries. The code (copied from here min 13:00) i used is this :
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main(void){
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL init failed.\n";
return 1;
}
cout << "SDL init succeeded";
SDL_Quit();
return 0;
}
The error i get is this
C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o) In function `main_getcmdline':
71 s:\rs\valve\release\SDL\SDL2-2.0.12-source\src\main\windows\SDL_windows_main.c undefined reference to `SDL_main'
C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\collect2.exe [Error] ld returned 1 exit status
25 C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\Makefile.win recipe for target 'SDL_.exe' failed
I tried running my Dev-Cpp.exe on administrator, since the installation folder is on the desktop, but that didn't solve the problem.
The Makefile (Whatevere it is, i don't have the minimal idea) is this. If needed ¯_(ツ)_/¯.
# Project: Progetto3
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = SDL_.o
LINKOBJ = SDL_.o
LIBS = -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/lib" -static-libgcc -mwindows -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lglu32
INCS = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/include"
BIN = SDL_.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
SDL_.o: SDL_.cpp
$(CPP) -c SDL_.cpp -o SDL_.o $(CXXFLAGS)
If you need any other info just ask. Thank You.
SDL hijacks the main function with its own in order to do some initial setup. It then calls whatever you have written as the main function. Because it is calling your main function it expects it to be defined in a specific way.
Try this and it should resolve the errors you are encountering:
int main(int argc, char* args[])
{
// whatever
return 0;
}
My Makefile:
CXX = g++
CXXFLAGS = -g -Wall -std=c++14
LDFLAGS = -lboost_system -lcrypto -lssl -lcpprest -lpthread
SRC := $(shell find . -name *.cpp)
OBJ = $(SRC:%.cpp=%.o)
BIN = run
all: $(BIN)
$(BIN): $(OBJ)
$(CXX) $(LDFLAGS)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $< -o $#
clean:
find . -name *.o -delete
rm -f $(BIN)
It scans for all files *.cpp files in all subdirectories and creates corresponding *.o files. Then it tries to link everything into a final binary, but I get the following error. I don't have any idea how to resolve this issue.
/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../lib/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
Directory structure:
Makefile
sources/
directory1
...cpp
directory2
...cpp
...
main.cpp
main.cpp content:
#include <iostream>
#include <signal.h>
#include "application/application_launcher.hpp"
Alastriona::Application::Launcher launcher;
void shutdown(int signal);
int main(int argc, const char * argv[])
{
struct sigaction sa;
sa.sa_handler = &::shutdown;
sa.sa_flags = SA_RESTART;
sigfillset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
launcher.loadArguments(argc, argv);
launcher.loadConfiguration();
launcher.startApplication();
}
void shutdown(int signal)
{
launcher.stopApplication();
}
int main(int argc, const char * argv[])
Is an overload due to the constness, which is not allowed, and considered ill formed §2 by the standard. The signature you need to use is
int main(int argc, char * argv[])
Edit : Your are not using any prerequisite when trying to build the target.
You should have
$(BIN): $(OBJ)
$(CXX) $^ $(LDFLAGS)
I'm trying to make a cpp file that simply displays an image :
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(){
Mat img;
img = imread("img.jpg",1);
if (!img.data){
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("my window", 1);
int key = 0;
while (key != 27){
imshow("my window", img);
waitKey(10);
}
destroyAllWindows();
return 0;
}
and I am using this Makefile:
CC=g++
CFLAGS=-W -Wall -ansi -pedantic -I/usr/include/opencv2
LDFLAGS=-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect
EXEC=DetectRT
SRC= $(wildcard *.cpp)
OBJ= $(SRC:.cpp=.o)
all: $(EXEC)
$(EXEC): $(OBJ)
$(CC) -o $# $^ $(LDFLAGS)
%.o: %.cpp
$(CC) -o $# -c $< $(CFLAGS)
.PHONY: clean mrproper
clean:
rm -rf *.o
mrproper: clean
rm -rf $(EXEC)
However, when I'm making I get errors like :
Test.o: In function `main':
Test.cpp:(.text+0x46): undefined reference to `cv::imread(cv::String const&, int)'