opencv: call to "Mat" is ambiguous - c++

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

Related

Tessaract gives poor results on Binary Image

I am trying to detect Alphabets in a picture using the tesseract library, which comes as an extra module for OpenCv3.
After a lot of preprocessing, I was able to get a very decent enough picture of the Alphabet.
However the tesserct library fails to recognise it at all.
Here is what I have written so far:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/text/ocr.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdlib.h>
using namespace cv;
using namespace std;
using namespace cv::text;
Mat img=imread("result.jpg",0);
if(img.empty()){
cout<<"Image not loaded!!\n";
return 1;
}
string output_text;
vector<Rect> detected;
vector<string> list;
vector<float> conf;
Ptr<OCRTesseract> ocr = OCRTesseract::create();
ocr->run(img,output_text,&detected,&list,&conf,0);
for(int i=0; i<detected.size(); i++){
Scalar color = Scalar(255,255,150);
rectangle( img, detected[i].tl(), detected[i].br(), color, 2, 8, 0 );
}
//cout<<"x="<<detected[0].x<<" y="<<detected[0].y
cout<<"Text found is:\n"<<output_text<<endl;
imshow("My",img);
waitKey(0);
return 0;
People have said that Tesseract is my best option. However I am not getting any result with the same.
Here is the link to Tesseract Library Documentation for opencv3.
http://docs.opencv.org/trunk/d7/ddc/classcv_1_1text_1_1OCRTesseract.html#gsc.tab=0
Also, is there any difference between using the native Tesseract Lib vs OpenCV version ?

Exception thrown writing to InputOutputArray

I'm trying to test some motion estimation in Visual Studio 2013, using OpenCV v3.0 (which is probably my 1st mistake!). I got an unhandled exception trying to use createOptFlow_DualTVL1() and createOptFlow_Farneback(), and then, for testing, tried cv::accumulate(), which threw the same exception.
It seems that OpenCV can't write to the Mat object that I'm passing these functions. I can't read the actual cvException because I don't have the PDB files, because I didn't compile this version myself. That might be my next stop, but before I do I figured I'd see if anyone's seen this behaviour before.
Here's a minimal working example:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
VideoCapture captureDevice;
std::string videoname = "example.mp4";
captureDevice.open(videoname);
//setup image files used in the capture process
Mat currFrame, dst;
captureDevice >> currFrame;
accumulate(currFrame, dst, cv::noArray());
imshow("outputCapture", dst);
//pause for 33ms
waitKey(33);
return 0;
}
dst should be of same size as that currFrame and of type CV_32FC3.
So, add this line of code before calling accumulate -
dst.create(currFrame.size(), CV_32FC3);
Since dst is of float type, you will need to convert it to uchar to display it. For that, convert as shown below -
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
Additionally, as you accumulate more frames in dst, you will need to normalize by number of frames(let's say N) cached in dst. Simply, divide dst by the N, then convert the result into CV_8UC3 and display. For example, if you accumulated 1000 frames in dst do as shown below,
// Accumulate 1000 frames
for(int i = 0; i < 1000; i++)
accumulate(currFrame, dst, cv::noArray());
// Normalize
dst = dst/ 1000;
// Display the frame
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
else, you might get an all white image.
UPDATE
From #berak's comment below.
For normalization, simply use
dst.convert(dst_disp, CV_8UC3, 1.0/N);
where N in example above will be 1000.

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 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.

Missing Scope variable OpenCV 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"