Library not loaded: libopencv_saliency.406.dylib - c++

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;
}

Related

Install opencv for C++ on Mac m1 chip properly

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.

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

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.

OpenCV - Webcam imshow not displaying live feed, gray screen instead

I am working with OpenCV version 3.2.0 in Visual Studio 2015 and have been able to access my webcam until all of a sudden when I was working on it this morning. I can't figure out where this problem is coming from. I now get:
It doesn't throw any errors but it also doesn't show any input through the webcam
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
printf("--(!)Error opening video capture\n");
return -3;
}
Mat image;
namedWindow("Image", CV_WINDOW_AUTOSIZE);
while (1)
{
cap.read(image);
imshow("Image", image);
waitKey(30);
}
return 0;
}
Has anyone encountered this error before?
Edit:
Things I have looked at:
My webcams that I have work in things like Google Hangouts so I don't think it's a webcam issue.
Also, I uninstalled Visual Studio 2015 and installed Visual Studio 2017 to see if reinstalling would work and still get the same results.
Edit:
I am getting the error <information not available, no symboles loaded for opencv_world320d.dll> when I create a new VideoCapture object. I am pretty sure I have everything included correctly.
Configuration Properties -> C/C++ -> Additional Include Directories:
$(OPENCV_BUILD)\include
Configuration Properties -> Linker -> General:
$(OPENCV_BUILD)\x64\vc14\lib
Configuration Properties -> Linker -> Input:
opencv_world320d.lib
I encountered the same problem after obtaining Opencv via compiling and building source using CMake. Then, I deleted them and installed Opencv from prebuilt binaries. I have run the code again and there was no problem.
As suggested by #michael scolfield, it was a problem with my antivirus blocking my webcam. I couldn't figure out how to exclude my Visual Studio directory so I just tried uninstalling it and it worked. It would be nice to have antivirus and have this working so I'll need to figure that out. But for temps this will work.

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;
}