I am working in Motion Detector Script but when i run my code i get this error every time when i use this function, but i don't know why it's wrong.
I am using opencv3, below is my code. I tried to run other examples i get it from web to same function, but the error still there. Any idea to fix it ?
This is the Error:
cv.cpp: In function ‘int main()’:
cv.cpp:23:4: error: ‘BackgroundSubtractorMOG’ is not a member of ‘cv’
My code :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
#include <sstream>
#include <opencv2/video/background_segm.hpp>
using namespace std;
int main()
{
//Openthevideofile
cv::VideoCapture capture("/home/shar/Desktop/op.mp4");
//checkifvideosuccessfullyopened
if (!capture.isOpened())
return 0;
//currentvideoframe
cv::Mat frame;
//foregroundbinaryimage
cv::Mat foreground;
cv::namedWindow("ExtractedForeground");
//TheMixtureofGaussianobject
//used with all default parameters
cv::BackgroundSubtractorMOG mog;
bool stop(false);
//forallframesinvideo
while(!stop){
//readnextframeifany
if(!capture.read(frame))
break;
//updatethebackground
//andreturntheforeground
mog(frame,foreground,0.01)
//learningrate
//Complementtheimage
cv::threshold(foreground,foreground,128,255,cv::THRESH_BINARY_INV);
//showforeground
cv::imshow("ExtractedForeground",foreground);
//introduceadelay
//orpresskeytostop
if(cv::waitKey(10)>=0)
stop=true;
}
}
As #shar said, the answer is in this post. In order to create a smart pointer to the algorithm you need to do:
cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2 = cv::createBackgroundSubtractorMOG2();
EDIT:
And for use the algorithm:
float learningRate = 0.01; // or whatever
cv::Mat foreground;
pMOG2->apply(frame, foreground, learningRate);
Related
I know there are other threads with this error, but it seems that the error has not 1 single solution and none of the other threads helped me in my case.
I am trying to process an image using dlib. I have tried reading an image, accesing a pixel and saving an image with dlib, and it works. Code that works, for reference:
#include <cstdlib>
#include <iostream>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>
using namespace std;
using namespace dlib;
int main(int argc, char** argv) {
//reading the image
array2d<rgb_pixel> img;
load_image(img, "landscape.bmp");
//accessing a pixel
cout << (int)img[0][0].red << "," << (int)img[0][0].green << "," << (int)img[0][0].blue << endl;
//saving an image
string name = "landscape2.bmp";
save_bmp(img, name);
cout << "press enter to exit..." << endl;
cin.ignore();
return 0;
}
Now, I have tried to create a function that takes an image and processes it. In order to process it I wanted to use some other functions that I defined in other modules. Here is the function from the same main.cpp file as the previous code:
#include "basic_preprocessing_alg.h"
#include <vector>
void create_preprocessed_image(std::string image_name, int image_width = 1280, int image_height = 720, int elem_sim_constant = 1, int new_elem_sim_threshold = 10) {
//reading the image and saving it to a dlib array named img
array2d<rgb_pixel> img;
load_image(img, "landscape.bmp");
// sending the image to another function to be processed
std::vector<rgb_pixel> frame = image_to_frame(img);
}
And now I get the error. The 'img' in 'std::vector frame = image_to_frame(img);' is underlined and it says "cannot be referenced -- it is a deleted function".
The basic_preprocessing_alg.h file contains the folowing:
#include <vector>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> frame);
And the basic_preprocessing_alg.cpp contains the following:
#include "basic_preprocessing_alg.h"
#include <vector>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> frame) {
//for now it only returns an empty vector, just for testing
return std::vector<dlib::rgb_pixel>();
}
I have to mention that I am a C++ beginner, I have coded in Python mostly and just recently tried C++. It might be a rookie mistake or something, but I am utterly confused and I could not find anything useful on Google.
The entire error message I get when I try to build:
1>c:\users\...\main.cpp(51): error C2280: 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d(const dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager> &)': attempting to reference a deleted function
1>c:\users\...\array2d_kernel.h(163): note: see declaration of 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d'
1>c:\users\...\array2d_kernel.h(163): note: 'dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager>::array2d(const dlib::array2d<dlib::rgb_pixel,dlib::default_memory_manager> &)': function was explicitly deleted
I don't understand what is the function that I attempt to reference which was deleted. I don't see how to fix this or what is wrong with my code.
I am using VisualStudio 2017 and Windows 10 if it matters.
The error is about the deleted copy constructor of dlib::array2d:
array2d(const array2d&) = delete; // copy constructor
An accessible copy constructor is required in order to pass parameters by value. Pass frame by reference to fix this error:
std::vector<dlib::rgb_pixel> image_to_frame(dlib::array2d<dlib::rgb_pixel> &frame);
// ^
Note: If you plan to call only const functions on frame, pass it by const reference.
Following code results in build success, but no window. Without "m = Scalar(255,0,0);", it creates black window. Why including scalar does not work?
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat m = Mat::zeros(200,200,CV_8UC3);
m = Scalar(255,0,0); //without this, it creates window.
imshow("m", m);
waitKey();
}
It will not give you any compilation error as Mat and Scalar are basically array types of OpenCV.
This is a possible duplicate of How to set all pixels of an OpenCV Mat to a specific value?
Your code have to work!
take a look at the doc:
C++: void imshow(const string& winname, InputArray mat)
and I can confirm you is working fine on my environment(opencv320, VisualStudio2017):
you need to clean the project and build again.
I have OpenCV 3.1.0 installed.
I want to use BackgroundSubtractorMOG so in my file i have these includes:
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
I declare a pointer to a class of this type:
Ptr<BackgroundSubtractorMOG> pMOG;
And I initialize the pointer like this:
pMOG = createBackgroundSubtractorMOG();
When I try to compile I get this:
/home/edd/Desktop/compvis/FML.cpp:19:5: error: ‘BackgroundSubtractorMOG’ was not declared in this scope
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp:19:28: error: template argument 1 is invalid
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp: In function ‘int main(int, char**)’:
/home/edd/Desktop/compvis/FML.cpp:29:42: error: ‘createBackgroundSubtractorMOG’ was not declared in this scope
pMOG = createBackgroundSubtractorMOG(); //MOG approach
^
I tried to look in the documentation. I couldn't figure out what header to include. I couldn't figure out how to instantiate an instance of the class correctly.
What do ?
In the code you've posted, I don't see any statement regarding namespaces. (e.g. using namespace cv in your preamble). Otherwise, you have to preface OpenCV code with cv:: (e.g. cv::Mat frame to declare a Mat object).
EDIT:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
...
cv::Ptr<cv::BackgroundSubtractor> mog = cv::bgsegm::createBackgroundSubtractorMOG();
...
And by using the namespace:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
using namespace cv
...
Ptr<BackgroundSubtractor> mog = bgsegm::createBackgroundSubtractorMOG();
...
Hope this helps,
MJ
I am new in openCV. I have a c++ code like below.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char *argv[])
{
Mat img(480,640,CV_8UC3,Scalar(255,0,0));
if(img.empty())
{
cout<<"Picture can not load..."<<endl;
return -1;
}
nameWindow("test",CV_WINDOW_AUTOSIZE);
imshow("test",img);
waitKey(0);
destroyWindow("test");
return 0;
}
I try to compile this code in ubuntu 14.04. But when i do
g++ resimac.cpp
it gives an error:
error: ‘nameWindow’ was not declared in this scope
nameWindow("test",CV_WINDOW_AUTOSIZE);
^
What is the problem? How to solve it?
nameWindow("test",CV_WINDOW_AUTOSIZE);
You are missing the 'd'. The correct format should be
namedWindow("test",CV_WINDOW_AUTOSIZE);
http://docs.opencv.org/modules/highgui/doc/user_interface.html#namedwindow
Hello I am trying to implement a Fast Feature Detector code,in the initial phase of it i get the following errors
(1)no instance of overloaded function "cv::FastFeatureDetector::detect" matches the argument list
(2)"KeyPointsToPoints" is undefined
Please help me.
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
int main()
{
Mat img1 = imread("0000.jpg", 1);
Mat img2 = imread("0001.jpg", 1);
// Detect keypoints in the left and right images
FastFeatureDetector detector(50);
Vector<KeyPoint> left_keypoints,right_keypoints;
detector.detect(img1, left_keypoints);
detector.detect(img2, right_keypoints);
vector<Point2f>left_points;
KeyPointsToPoints(left_keypoints,left_points);
vector<Point2f>right_points(left_points.size());
return 0;
}
The problem is in this line:
Vector<KeyPoint> left_keypoints,right_keypoints;
C++ is case-sensitive, it sees that Vector is something different than vector (what it should really be). Why would Vector work is beyond me, I would have expected an error earlier.
cv::FastFeatureDetector::detect only knows how to work with vector, not a Vector, so try to fix this bug and try again.
Also, KeyPointsToPoints does not exist in the OpenCV library (unless you program it yourself), make sure you use KeyPoint::convert(const vector<KeyPoint>&, vector<Point2f>&) to do the conversion.
There are a few small problems with the code as given. First you are missing using namespace std which is needed to use the vectors the way you are using them. You are also missing the include for vectors #include <vector>. vector<KeyPoints> should also have a lower case v. I'm also not sure if KeyPointsToPoints is part of OpenCV. You may have to implement this function on your own. I'm not sure, I just haven't seen it before.
#include <stdio.h>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("0000.jpg", 1);
Mat img2 = imread("0001.jpg", 1);
// Detect keypoints in the left and right images
FastFeatureDetector detector(50);
vector<KeyPoint> left_keypoints,right_keypoints;
detector.detect(img1, left_keypoints);
detector.detect(img2, right_keypoints);
vector<Point2f>left_points;
for (int i = 0; i < left_keypoints.size(); ++i)
{
left_points.push_back(left_keypoints.pt);
}
vector<Point2f>right_points(left_points.size());
return 0;
}
I didn't test the code above. Check out the OpenCV documentation here