cv has no member BackgroundSubtractorMOG - c++

I am new to opencv and followed instructions to install it as described here:
http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#windows-installation
I used the section "Installation by Making Your Own Libraries from the Source Files", which worked well (using Visual Studio 2013). I am able to run basic commands, like read an image, write an image, run edge detection, video processing etc.
But now I tried to use BackgroundSubtractorMOG and I get the error that BackgroundSubtractorMOG is not a member of cv. The simplest code is below and I don't know where to start. Am I missing something in my installation? Any ideas?
#include "stdafx.h"
#include<opencv2/opencv.hpp>
int main()
{
cv::BackgroundSubtractorMOG bg;
return 0;
}

with opencv3.0, BackgroundSubtractorMOG was moved to the opencv_contrib repo
to use the remaining BackgroundSubtractorMOG2 or BackgroundSubtractorKNN you'd have to use:
Ptr<BackgroundSubtractorMOG2> bgm = createBackgroundSubtractorMOG2(...);

you forgot to include the header
#include <background_segm.hpp>
Reference: http://physics.nyu.edu/grierlab/manuals/opencv/classcv_1_1BackgroundSubtractorMOG.html
path to the header file could be: /opencv2/video/background_segm.hpp

Related

UE5 OpenCV: Can’t grab VideoCapture

I’m trying to read camera input using the built-in OpenCV library in UE5 but no matter what I do, I can’t seem to make cv::VideoCapture.read() return anything. cv::VideoCapture.grab() also returns false every time.
It works fine on the same machine with regular C++ with OpenCV 4.6.0 and the VideoCapture is definitely open and the camera turns on as expected.
Is there something about Unreal’s built-in implementation I need to know about? (Other than it used OpenCV 4.5.5). I can’t seem to find any info online about this.
This is what my header files look like:
#pragma once
#include "PreOpenCVHeaders.h"
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include "PostOpenCVHeaders.h"
#include "CameraReader.generated.h"
And my Plugin’s Build file:
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"OpenCVHelper",
"OpenCV",
// ... add other public dependencies that you statically link with here ...
}
);
Using Windows 11 with Kinect 2.0.
I thought maybe I was missing some OpenCV DLLs since I can only find a custom Unreal version of the OpenCV World DLL, so I tried overriding the default OpenCV plugin and adding the FFMPEG and MSMF DLLs myself but that didn’t change anything.
Edit: After many attempted building OpenCV from source, I only managed to get GStreamer and FFMPEG to work.
The OpenCV library that comes with UE5 is not built with FFmpeg or with the Media Foundation library, so it cannot read video from a file or from a camera.
You can try to build your own version of OpenCV with FFmpeg or Media Foundation support and use that instead of the one that comes with UE5.

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.

openCV libraries call local path that does not exist

i am making a simple c++ program with openCV library included. Eclipse IDE recognises openCV commands and library locations, but when i try to build the project, compiler gives external error, referring to opencv.hpp or core.hpp file calling a "opencv2/core.hpp" path, which does not exist in opencv folder. I figured out that the problem is linked to the way core.hpp is called, but the library files are read-only.
From what i saw in opencv.hpp file, this relative "opencv2/[module].hpp" reference is not only for the core, but all other modules as well. There is no opencv2 folder inside the one to where openCV is installed at all, in fact.
I've tried reinstalling and remaking openCV with different making arguments, using a different IDE and including direct search folders in eclipse. The problem, apparently, lies in the files themselves, or the way it maybe gets installed in the system the wrong way. The problem persists on both my main ubuntu machine and the ARMbian orange pi.
i get this error when trying to include any openCV library that contains
#include "opencv2/[opencv module].hpp" in it
as a result, compilation is terminated with the error message stating "/usr/local/include/opencv4/opencv2/opencv.hpp:52:28: fatal error: opencv2/core.hpp: No such file or directory"
edit 1: GCC c++ compiler options are -Iusr/local/include/opencv4/opencv2 -O3 -Wall -c -fmessage-length=0 and linker's options are -L/usr/local/lib.
The code is a simple displayImage
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
edit 2: $ pkg-config --libs opencv does not see openCV as installed in the system, altho i've made sure to run make install and ldconfig on the path. This may be a signal of faulty installation, but this is just a sidenote, not entirely related to main problem. I have tried reinstalls and to different folders, but this also persists as well as a main problem
apparently, #sgarizvi 's comment was the answer. I just needed to set the include path to I/usr/local/include/opencv4 and it worked. After that, the error was fixed.
I am replying to my own question to close the case, as i cannot upvote/veryfy a comment
In your case, since your include path is /usr/local/include/opencv4/opencv2
Replace the first three lines
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
by
#include <opencv.hpp>
#include <imgproc.hpp>
#include <highgui.hpp>

OpenCV on C++/Namespace issue?

I am new to C++ so I get some trouble to use openCV on my C++ project. I'm using Xcode as an IDE.
So I used brew to install opencv using the two command lines:
brew install opencv3 --with-ffmpeg --with-tbb --with-contrib
brew reinstall opencv3 --HEAD --with-python3 --with-ffmpeg --with-tbb --with-contrib
I checked the path to add to my project to load the library using recursivity, so I added on Xcode the path for header path and library:
/usr/local/Cellar/**
I also tried to install it another way, but still got the same issue:
brew install opencv
And adding the path to:
/usr/local/include/**
Everything seems to work since the library is detected, but import is not working because I got namespace errors in the openCV files, for instance:
No type named 'unique_ptr' in namespace 'std'
No member named 'allocator_traits' in namespace 'std'; did you mean 'allocator_arg_t'?
I checked on the internet and maybe it should be due to the the C++ language dialect or standard library, but I use GNU ++ 14 and libc++ . From what I found it should be working in that config, but I still got the issues. Do you have any ideas ?
EDIT: I don't even try to use it yet, I just used the include and print an hello:
#include <iostream>
#include "cv.h"
int main(int argc, char *argv[]){
std::cout<<"hello";
}
I also tried cv.hpp instead of cv.h, still not working
Thanks a lot !
I don't think you are using the correct #include paths, if you look at the OpenCV Example, you need the following for OpenCV 3.0 to open an image:
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
None of these are like the headers that you have, which are likely for older versions.
This tutorial looks like a very sensible one to get up and running with xcode, and the example at the bottom looks like a better start

many undefined functions in opencv 3.0.0 using visual studio 2013

I have just started out to learn opencv but facing many issues. i have installed opencv and linked it to visual studio 2013.
My first program which i copied from a video worked fine, which was
#include <cstdio>
#include <opencv2\opencv.hpp>
void main()
{
std::cout << "OpenCV Version: " << CV_VERSION << std::endl;
}
after this i created anew project and imported all the settings of the first project through import export settings option, but while compiling only it is showing many errors
#include <opencv/cv.h>
#include <opencv/highhui.h>
using namespace std;
using namespace cv;
int main(void)
{
Mat img = imread("C:/Users/shivamkumar07/Documents/Visual Studio 2013/Projects/ConsoleApplication2/shape.jpg");
imshow("inputfile", img);
img.release();
waitKey();
cvDestroyAllWindows();
return 0;
}
i am not able to understand why this is happening, please help me i am a beginner in this and not much tutorials are available on opencv 3.0.0
Thanks in advance!!
The first error is pretty clear, the compiler cannot find the file "opencv/cv.h" you included.
Try adding the correct folder to the include directories in the project settings.
After going through lots of stuff, i found out that i had not properly linked my opencv with this new project. That's why there were many undefined functions in my new project.
A better way to do this by using "Property sheet", once a property sheet is saved then it can be used in all of the new projects which you want.
steps to create Property Sheet
Right click on cpp file and add new items.
add property sheet here.
link opencv with your project by adding all the directories and lib files both in Release and Debug and save it by giving it some name.
steps to copy property sheet in new project
right click and click add existing item.
select the property sheet here and now the new project is linked with your opencv.
Thanks !
Clearly it has problem importing in that path. Try changing the path setting. Hope it helps.