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.
Related
I am trying to compare two image and check whether they belongs to same person. This code giving Debug assertion failed Like here. I have checked, it can access to photos. Then what is the problem. People say that you are trying to access the something which is not actually exist. However, I could not find anything about it in my code.
Regards;
#include <iostream>
#include <string>
#include "opencv2\core\core.hpp"
#include "opencv2\core.hpp"
#include "opencv2\face.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\opencv.hpp"
#include<direct.h>
#include "vector"
using namespace std;
using namespace cv;
using namespace cv::face;
int main() {
vector<Mat> img;
img[0]= imread("C://Users//Gökhan//Desktop//faces//faces442.jpg");
Mat img2 = imread("C://Users//Gökhan//Desktop//faces//faces448.jpg");
vector<int> label;
label[0] = 1;
Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();
model->train(img[0], label[0]);
int predictedLabel = model->predict(img2);
if (predictedLabel == 1) {
cout << "found";
}
}
You are trying to access elements of vectors that have no elements.
You have to allocate elements before accessing.
You can use the constructor with specifying the number of elements to allocate to allocate elements, for example. To do this, change the lines
vector<Mat> img;
vector<int> label;
to
vector<Mat> img(1);
vector<int> label(1);
I'm trying to use the library ZBar to read QRCode with c++. I am not an expert about this language. I have the following problem:
#include <iostream>
#include <zbar.h>
using namespace std;
using namespace zbar;
int main()
{
cout << "Hello World!" << endl;
//Image image;
return 0;
}
This code does actually print the "Hello World" as expected. But if I remove the comment, obtaining the following code:
#include <iostream>
#include <zbar.h>
using namespace std;
using namespace zbar;
int main()
{
cout << "Hello World!" << endl;
Image image;
return 0;
}
Everything is still compiled correctly, with no errors or warnings, but I don't obtain any output!! The program reaches the end without executing any instruction!
I just used this code to semplify, but the problem actually is that, as soon as I add an instruction using classes from zbar, it seems like I am "clearing" the main! How is it possible? What should I do? Thank you!
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 am having build errors when declaring prototype functions within Xcode. I am writing in C++. The script is pulled from my professor's lecture. Attached below is a picture of the build errors, along with the script itself.
Note: I only run into build issues when trying to declare prototype functions. It is as if Xcode is trying to pull the functions from a Library, and not recognizing it.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int calcSquare (int num) ;
int main ()
{
int num = 5;
int result;
result = calcSquare(num);
cout << "The Square of " << num << " is " << result << endl;
return 0;
}
Errors: https://farm3.staticflickr.com/2871/33406384892_68ee0843c7_b.jpg
The problem with your program is that you forgot to DEFINE the function prototype later in your source code.
Right now, you simply have a function prototype ( int calcSquare ), but you called that function anyways.
That throws you the linker exception as shown.
In other words, define the function somewhere in your source code to use the <cmath>, <cctype>, <cstdlib> modules and libraries.
After the function prototype, declare the function later:
int calcSquare (int num) {
...
}
I hope this helps! :)
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);