Install opencv for C++ on Mac m1 chip properly - c++

I've tried to install openCV for C++ using this tutorial : https://www.youtube.com/watch?v=KaTA-yK7dWA
and then try to set it up on Xcode using this video:
https://www.youtube.com/watch?v=2FYm3GOonhk&t=1266s
But when I ran the code:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
///////////////// Webcam //////////////////////
int main() {
VideoCapture cap(1);
Mat img;
while (true) {
cap.read(img);
imshow("Image", img);
waitKey(1);
}
return 0;
}
I got the error:
ld: library not found for -lopencv_photo.4.5.5
clang: error: linker command failed with exit code 1 (use -v to see invocation)
don't know what to do, I'll be glad for help and guidance, it was much easier in python
Thanks in advance !

As the linker says - it cannot find the library lopencv_photo.4.5.5 to link against your program.
In Xcode the easiest option is to append the flag -lopencv_photo to Other Linker Flags in your Project Preferences.
Also - you wont be able to access a webcam on MacOS. There is no direct USB (serial) access to the video feed and it would require entitlements that requires a MacOS App project, not a command line tool.

Related

Library not loaded: libopencv_saliency.406.dylib

I installed OpenCV 4.6.0 via homebrew and added all the libraries and headers through the OpenCV folder. After running the below code got the following message back in the terminal.
dyld[11080]: Library not loaded: '/opt/homebrew/opt/opencv/lib/libopencv_saliency.406.dylib'
Referenced from: '/Users/lavishgupta/Library/Developer/Xcode/DerivedData/OPEN_CV-fkdewjttbimvjverbezyqfwisorv/Build/Products/Debug/OPEN CV'
Reason: tried: '/Users/lavishgupta/Library/Developer/Xcode/DerivedData/OPEN_CV-fkdewjttbimvjverbezyqfwisorv/Build/Products/Debug/libopencv_saliency.406.dylib' (no such file),
'/usr/lib/system/introspection/libopencv_saliency.406.dylib' (no such file), '
I tried some approaches to fix this like uninstalling and reinstalling OpenCV via brew and again added all the libraries and header. but that didn't work. Also tried something I found on the internet but nothing worked. I'm on macOS Monterey if that matters.
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
string path = "Resources/test.png";
Mat img = imread(path);
imshow("Image", img);
waitKey(0);
return 0;
}

"opencv_world454.dll was not found"

hello I had imported OpenCV into my C++ project but for some reason my code gives me this error.
CODE:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
int main()
{
std::string path = "Resources/test.png";
cv::Mat img = cv::imread(path);
imshow("Image", img);
cv::waitKey(0);
return 0;
}
error:
I think it might be because of my paths but I'm not sure I think I got them right.
enter image description here
Edit: I reinstalled the setup and installed openCV in a new path and now I do have .dll files but it keeps giving me an error here are new screenshots
enter image description here
Maybe it will help someone
Instructions:
Open Project->Property
Configuration Properties->Debugging->Environment
PATH=C:\opencv\build\x64\vc15\bin;
You need your path to the folder
The solution was to install a new setup and install OpenCV into another path thank you to everyone who helped.

How to deal with "DNN module was not built with CUDA backend; switching to CPU" warning in C++?

I am trying to run YOLOv3 on Visual Studio 2019 using CUDA 10.2 with cuDNN v7.6.5 on Windows 10 using NVidia GeForce 930M. Here is part of the code I used.
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace dnn;
using namespace std;
int main()
{
// Load names of classes
string classesFile = "coco.names";
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
// Give the configuration and weight files for the model
String modelConfiguration = "yolovs.cfg";
String modelWeights = "yolov3.weights";
// Load the network
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
// Open the video file
inputFile = "vid.mp4";
cap.open(inputFile);
// Get frame from the video
cap >> frame;
// Create a 4D blob from a frame
blobFromImage(frame, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
// Sets the input to the network
net.setInput(blob);
// Runs the forward pass to get output of the output layers
vector<Mat> outs;
net.forward(outs, getOutputsNames(net));
}
Although I add $(CUDNN)\include;$(cudnn)\include; to Additional Include Directories in both C/C++ and Linker, added CUDNN_HALF;CUDNN; to C/C++>Preprocessor Definitions, and added cudnn.lib; to Linker>Input, I still get this warning:
DNN module was not built with CUDA backend; switching to CPU
and it runs on CPU instead of GPU, can anyone help me with this problem?
I solved it by using CMake, but I had first to add this opencv_contrib then rebuilding it using Visual Studio. Make sure that these WITH_CUDA, WITH_CUBLAS, WITH_CUDNN, OPENCV_DNN_CUDA, BUILD_opencv_world are checked in CMake.
I had a similar issue happen to me about a week ago, but I was using Python and Tensorflow. Although the languages were different compared to C++, I did get the same error. To fix this, I uninstalled CUDA 10.2 and downgraded to CUDA 10.1. From what I have found, there might be a dependency issue with CUDA, or in your case, OpenCV hasn't created support yet for the latest version of CUDA.
EDIT
After some further research it seems to be an issue with Opencv rather than CUDA. Referencing this github thread, if you installed Opencv with cmake, remove the arch bin version below 7 on the config file, then rebuild/reinstall Opencv. However, if that doesn't work, another option would be to remove CUDA arch bin version < 5.3 and rebuild.

Struggling with dlib, linker errors and save_jpeg in XCode

First, I am new to C++ and dlib but I have successfully built the examples and started working on my own project. Things have been progressing smoothly until I try to save a jpeg. Attempting to compile code using dlib::save_jpeg throws a linker error and I cannot track down the solution. I have attempted to add #define DLIB_JPEG_SUPPORT above and below my #includes but no luck. I am using XCode and used cmake -G "Xcode" .. when I compiled the examples. Relevant code below. Since I am on a Mac, I have added header and library search paths for X11 (for dlib gui), OpenCV, and DLIB. I have libjpeg.dylib and linked that to my project with and without #define DLIB_JPEG_SUPPORT in main.cpp. Is there some other build setting I need to specify? Thank you in advance for your help.
Finally, I have seen other questions and pages about dlib and libjpeg issues but no luck yet. And yes I have source.cpp included in the project.
// the standard stuff
#include <string>
#include <iostream>
#include <unistd.h>
// opencv mat object
#include <opencv2/opencv.hpp>
// dlib>
#include <dlib/opencv.h>
#include <dlib/image_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
int main(int argc, const char * argv[]) {
// retrieving images from a TCP connection
// decode data stream
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
// perform image processing
dlib::cv_image<dlib::bgr_pixel> d_image(img);
// finally save the result to jpg
std::string fname = argv[1] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(d_image, fname); // <- line that won't compile
return 0;
}
After quit a bit of struggling and side-by-side comparisons I finally found the issue. In XCode go to to Build Settings and modify Other Linker Flags, Run Search Paths, and Other C++ Flags to match the compiled and working face_ex example. I wholesale copied all of those flags and included a missing libjpeg.dylib and was able to get things running. It should look something like this for the C++ flags . Hope this helps the next person.

Opencv Mingw Build

After trying without succed to install opencv with Mingw and codeblocks, i finnaly give up and download the precompiled libs from :
https://github.com/drewcrawford/drew-face/tree/master/opencv/build
But when i try to run some opencv code, my application crashed when it start and i have this message :
"The application was unable to start correctly (0xc0000005). Click OK to close the application."
Her is my code test :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
cv::Mat image;
image = cv::imread("4010194A00101a_090.bmp",0);
return 0;
}