Missing Scope variable OpenCV c++ - c++

I have a program that should do template matching on an image and a template, here is the code :
int main()
{
IplImage* imgOriginal = cvLoadImage("image.jpg", 0);
IplImage* imgTemplate = cvLoadImage("template.jpg", 0);
IplImage* imgResult = cvCreateImage(cvSize(imgOriginal->width-imgTemplate->width+1, imgOriginal->height-imgTemplate->height+1), IPL_DEPTH_32F, 1);
cvZero(imgResult);
cvMatchTemplate(imgOriginal, imgTemplate, imgResult, CV_TM_CCORR_NORMED);
double min_val=0, max_val=0;
CvPoint min_loc, max_loc;
cvMinMaxLoc(imgResult, &min_val, &max_val, &min_loc, &max_loc);
cvRectangle(imgOriginal, max_loc, cvPoint(max_loc.x+imgTemplate->width, max_loc.y+imgTemplate->height), cvScalar(0), 1);
printf("%f", max_val);
cvNamedWindow("result");
cvShowImage("result", imgOriginal);
cvWaitKey(0);
return 0;
}
include files :
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include "stdio.h"
using namespace cv;
using namespace std;
When I run the code, I get this error :
templateMatching.cpp:16:75: error: ‘cvMatchTemplate’ was not declared in this scope
Any idea what the problem is? Thanks in advance, Matt

You need include
#include "opencv2/imgproc/imgproc_c.h"

Related

opencv: call to "Mat" is ambiguous

I try to use opencv to calculate the HoG descriptor, but an error call to "Mat" is ambiguous was raised.
I saw the answer under Error: Mat is ambiguous when using OpenCV
and I don't know whether my includes are right.
and my includes for opencv are:
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv/cv.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <opencv2/ml/ml.hpp>
#include <string>
using namespace std;
using namespace cv;
using namespace cv::ml;
here's the code:
for (int j = 0; j < original[i]; j++) {
cv::Mat train_original;
train_original = cv::imread(original_path + std::to_string(j) + ".jpg", 1);
cv::resize(train_original, train_resize_image, cv::Size(128, 128), 0, 0, CV_INTER_LINEAR);
// 计算HOG descriptor
cv::HOGDescriptor hog(cv::Size(128, 128), cv::Size(64, 64), cv::Size(16, 16), cv::Size(8, 8), 9);
hog.compute(train_resize_image,train_descriptor);
cv::Mat descriptor_mat(cv::Mat(train_descriptor).t());
train_descriptors.push_back(descriptor_mat);
}
where train_descriptor is in type of std::vector<float>
The error was raised in line
cv::Mat descriptor_mat(cv::Mat(train_descriptor).t());
Any help will be appreciated! thanks!
You should change the following line:
cv::Mat descriptor_mat(cv::Mat(train_descriptor).t());
with
cv::Mat descriptor_mat = cv::Mat(train_descriptor).t();
since Mat operator = can handle MatExpr object, while Mat constructor can not(check OpenCV documentation). But be careful with this method since it does not copy the data and shares it(like a pointer). Basically, if you want to copy the data you can do the following:
cv::Mat descriptor_mat;
cv::transpose(cv::Mat(train_descriptor),descriptor_mat);

OpenCV 3 SURF feature detection

I am trying to do a simple surf feature detection.
The problem is that during the execution I have two Microsoft Visual C++ Runtime Library Errors with only the use of the detect method :
Debug Assertion Failed ! Program:
C:\Windows\system32\MSVCP120D.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 240
Expression : vector iterators incompatible
And
Debug Assertion Failed ! Program:
C:\opencv\build\x64\vc12\bin\Debug\opencv_xfeatures2d300d.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 241
Expression : "Standard C++ Libraries Invalid Argument" && 0
This is the code :
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv/cv.h"
#include "opencv/highgui.h"
using namespace cv;
using namespace std;
using namespace xfeatures2d;
int main(int argc, const char** argv)
{
// Read image
Mat img1 = imread("box.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread("box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE);
if (img1.empty() || img2.empty())
{
printf("Images non lisibles \n");
return -1;
}
// detecting keypoints
int hessian = 800;
Ptr<SURF> surf = SURF::create(hessian);
vector<KeyPoint> keypoints1, keypoints2;
surf->detect(img1, keypoints1);
surf->detect(img2, keypoints2);
waitKey(0);
return 0;
}

Function inpaint "was not declare in this scope"

I'm trying to use the inpaint function in opencv, but I'm getting this error
loadimg.cpp: In function 'int main(int, char**)':
loadimg.cpp:19:28: error: 'INPAINT_TELEA' is not a member of 'cv'
loadimg.cpp:19:45: error: 'inpaint' was not declared in this scope
From typing this:
C:\Users\Francesco\Desktop\prova>g++ -I"C:\opencv\build\include"
-L"C:\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core245 -lopencv_highgui245
-lopencv_img proc245 -o loadimg
This is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = cv::imread("prova.jpg");
Mat mask;
cvtColor(src, mask, CV_RGB2GRAY);
threshold(mask, mask, 220, 255, CV_THRESH_BINARY);
Mat dst;
inpaint(src, mask, dst, 1, cv::INPAINT_TELEA);
imshow("image", dst);
waitKey(0);
return 0;
}
Can anyone help me? Thank you very much.
cv::inpaint() is declared in the photo module. You need to #include <opencv2/photo/photo.hpp> . Alternatively, you could #include <opencv2/opencv.hpp>, which includes all of OpenCV's functionality.
try:
inpaint(src, mask, dst, 1, INPAINT_TELEA);
Also, include :opencv2/photo/photo.hpp

OpenCV SURF extractor.compute error

I use OpenCV 2.44 and Visual Studio C++ 2010
When I compile this
#include <opencv2/imgproc/imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;
void main()
{
Mat img1 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
// detecting keypoints
FastFeatureDetector detector(15);
vector<KeyPoint> keypoints1;
detector.detect(img1, keypoints1);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1;
extractor.compute(img1, keypoints1, descriptors1);
when I run the code I get Unhandled exception at 0x580f375b in prj.exe: 0xC0000005: Access violation reading location 0x001f7014.
the error is at extractor
I'm using this tutorial link
It's looks like that you forget to init non-free module. Try to call appropriate function before using SurfDescriptorExtractor:
#include <opencv2/nonfree/nonfree.hpp>
...
cv::initModule_nonfree();

OpenCV 2.4.0 C++ goodFeaturesToTrack corrupts heap?

I'm just now starting to learn how to use the openCV libraries. I've downloaded and installed openCV 2.4.0, and have run a few example projects. In this block of code, I'm trying to get the output from goodFeaturesToTrack and plot the points on an image. The code compiles, but every time I run it, it crashes, and I get the following error:
Windows has triggered a breakpoint in Corner.exe.
This may be due to a corruption of the heap, which indicates a bug in Corner.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Corner.exe has focus.
The output window may have more diagnostic information.
The output window does not have more diagnostic information. I've traced the error to the goodFeaturesToTrack function. Here's the offending code:
// Corner.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv.hpp>
#include <opencv_modules.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace cv; //If you don't have this, you won't be able to create a mat...
using namespace std;
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
//Whole bunch of #defines to make editing the code a lot easier
#define MAX_FEATURES 5
#define FILENAME "C:/Users/Mitchell/Desktop/lol.jpg"
int main(void)
{
namedWindow("Out", CV_WINDOW_AUTOSIZE);
namedWindow("In", CV_WINDOW_AUTOSIZE);
Mat Img;
Img = cvLoadImage(FILENAME, CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)
{
fprintf(stderr, "ERROR: Couldn't open picture.");
waitKey();
return -1;
}
else
{
imshow("In", Img);
waitKey();
}
std::vector<cv::Point2f> Img_features;
int number_of_features = MAX_FEATURES;
Mat Out = Mat::zeros(Img.cols, Img.rows, CV_32F);
goodFeaturesToTrack(Img, Img_features, MAX_FEATURES, .01, .1, noArray(), 3, false);
fprintf(stdout, "Got here...");
/*for (int i = 0; i < MAX_FEATURES; i++)
{
Point2f p = Img_features[i];
ellipse(Img, p, Size(1,1), 0, 0, 360, Scalar(255,0,0));
}*/
imshow("Out", Out);
waitKey(0);
return 0;
}
Is this a bug in the library, or am I doing something dumb?
May be Img_features vector should have MAX_FEATURES items before calling goodFeatures? i.e. try Img_features.resize(MAX_FEATURES) before goodFeatures call.