i'm working on a project of image stitching using OpenCV 2.3.1 on Visual Studio 2012.
For an unknown reason my programme don't work !!
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
Mat getHomography()
{
vector<Point2f> points1;
vector<Point2f> points2;
points1.push_back(Point2f(10, 100));
points1.push_back(Point2f(100, 10));
points1.push_back(Point2f(200, 20));
points1.push_back(Point2f(80, 30));
points2.push_back(Point2f(220, 20));
points2.push_back(Point2f(10, 220));
points2.push_back(Point2f(90, 120));
points2.push_back(Point2f(100, 20));
Mat H = findHomography(Mat(points1), Mat(points2), 8, 3.0);
return H;
}
int main(){
Mat HH = getHomography();
cout<<HH;
system("pause");
return 0;
}
my problem :
any help plzzz
you need at least 4 points in each vec. 2 just aren't enough
I solved my problem by changing from vs2012 to vs 2010 :D
Related
I am trying the compile the following code to compute the chamfer distance. However I get the following error while compiling it. I am using opencv-3.2 on ubuntu 18.04, 64 bit.
fatal error: opencv2/contrib/contrib.hpp: No such file or directory
Synaptic package manager says that libopencv-contrib-dev and libopencv-contrib-3.2 are installed at /usr/include/opencv2 and /usr/lib/x86_64-linux-gnu respectively. I checked for contrib.hpp but found no file or folder named contrib in these locations.
The code for the chamfer distance computation is as below:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread(argv[1], 0);
Mat tpl = imread(argv[2], 0);
Mat cimg;
cvtColor(img, cimg, CV_GRAY2BGR);
vector<vector<Point> > results;
vector<float> costs;
int best = chamerMatching( img, tpl, results, costs );
return 0;
}
My question is: How to add the correct headers and get the above chamfer distance code working in opencv 3.2?
It looks like the contrib module is not available in OpenCV 3.2. This module was removed in OpenCV 4.0 and is no longer part of the library. If you want to use the Chamfer matching algorithm in your code, you can try using the matchShapes function from the imgproc module instead. This function can be used to compare the shapes of two contours and return a measure of their similarity:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread(argv[1], 0);
Mat tpl = imread(argv[2], 0);
// Find the contours of the images
vector<vector<Point> > img_contours;
vector<vector<Point> > tpl_contours;
findContours(img, img_contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
findContours(tpl, tpl_contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
// Compute the Chamfer distance between the contours
double chamfer = matchShapes(img_contours[0], tpl_contours[0], CHAMFER_DIST_L2, 0);
cout << "Chamfer distance: " << chamfer << endl;
return 0;
}
I am having a bit of trouble in trying to get the opencv face detection to work in QT with my basler cam; I have tried many different approaches to get it to work, using many different sample codes online. I just can’t seem to get it to work at all; in addition the attempts I have made have lowered my frame rate.
The code I used to capture a video with the basler cam is working great, I’m just having trouble implementing the face detection part. I will paste the code I have so far for the camera and opencv below. The code does get me a few red boxes appearing now and then, but it isn’t stable. I am also getting this error
Failed to load OpenCL runtime
I’m not sure what I am doing wrong, also is there a way to implement the face detection without lowering the frame rate, as it is already slow
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <pylon/PylonIncludes.h>
//#include <pylon/PylonGUI.h>
//#ifdef PYLON_WIN_BUILD
//#include <pylon/PylonGUI.h>
//#endif
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace cv;
// Namespace for using pylon objects.
using namespace Pylon;
// Namespace for using cout.
using namespace std;
static const uint32_t c_countOfImagesToGrab = 100;
cv::CascadeClassifier faceCade;
String faceCascadeName = "/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
String FaceDetectWindow = "Face Detector Window";
String FaceDetectGrayWindow = "Face Detector Gray Window";
size_t i;
vector<Rect> faces;
cv::Mat camFrames, grayFrames;
int main()
{
// The exit code of the sample application.
int exitCode = 0;
// Automagically call PylonInitialize and PylonTerminate to ensure
// the pylon runtime system is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
faceCade.load( faceCascadeName );
CGrabResultPtr ptrGrabResult;
namedWindow("CV_Image",WINDOW_AUTOSIZE);
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
camera.Open();
GenApi::CIntegerPtr width(camera.GetNodeMap().GetNode("Width"));
GenApi::CIntegerPtr height(camera.GetNodeMap().GetNode("Height"));
Mat cv_img(width->GetValue(), height->GetValue(), CV_8UC3);
camera.StartGrabbing();
CPylonImage image;
CImageFormatConverter fc;
fc.OutputPixelFormat = PixelType_BGR8packed;
while(camera.IsGrabbing()){
camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded()){
fc.Convert(image, ptrGrabResult);
cv_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer());
//cvtColor(cv_img, grayFrames, cv::COLOR_BGR2GRAY);
//equalizeHist(grayFrames, grayFrames);
faceCade.detectMultiScale(cv_img, faces, 1.1, 2, 0, Size(160, 160));
for (int i = 0; i < faces.size(); i++)
{
//Mat faceROI = grayFrames(faces[i]);
rectangle(cv_img, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35), Scalar(0, 0, 255), 1, 1, 0);
Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);
}
imshow("CV_Image",cv_img);
//imshow("FaceDetectGrayWindow", grayFrames);
waitKey(1);
if(waitKey(30)==27){
camera.StopGrabbing();
}
}
}
}
}
Thank you
i'm not quite sure about this but detectMultiScale function works with image in cv_8u type , and as i see you are using cv_8uc3, as i know cv_8u it's 8 bit pixel with 1 channel, cv_8uc3 it's alos 8 bit but 3 channels, you need to convert your image to gray scale , i saw you did that but you comment it?!!!
look at this link opencv_face_detection.
maybe that will fix your problem, and some people advice you to install opencl
sudo apt-get install ocl-icd-opencl-dev
when I try debug my project, I get Debug Assertion Failed, at the end of the execution up. You can see it below.
I think that the problem is related to FAST function from OpenCV, because when I comment out the line which contains this function, it works fine.
This is my code:
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
string table[2] = { "background_2.jpg","foreground_2.jpg" };
Mat firstImage = imread(table[0], IMREAD_COLOR);
Mat secondImage = imread(table[1], IMREAD_COLOR);
Mat result;
subtract(firstImage, secondImage, result);
Mat resultGray, firstImageGray, secondImageGray;
cvtColor(firstImage, firstImageGray, COLOR_BGR2GRAY);
cvtColor(secondImage, secondImageGray, COLOR_BGR2GRAY);
cvtColor(result, resultGray, COLOR_BGR2GRAY);
Canny(firstImageGray, firstImageGray, 33, 100, 3);
Canny(secondImageGray, secondImageGray, 33, 100, 3);
Canny(resultGray, resultGray, 33, 100, 3);
vector <KeyPoint> keyPoints;
FAST(resultGray, keyPoints, 9, true);//Probably here is the problem.
Mat resultKeyPoints;
drawKeypoints(resultGray, keyPoints, resultKeyPoints, 156);
return 0;
}
I build my project in Visual Studio 2015 Preview.
When I clicked "Retry" to break at that assertion, I got the following exception:
Unhandled exception at 0x00007FF851891B4B (ucrtbased.dll) in
OpenCVProject.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
This is in xmemory0 file from VS in 120 line.
I found the answer. I changed in Property Pages in my project Platform Toolset from Visual Studio 2015 (v140) to Visual Studio 2013 (v120). Now it works fine. It seems that the problem wasn't in FAST function, but in this case, that I use VS 2015 with OpenCV 3.0. #drescherjm thank you for help me do this.
I'm new to using OpenCV. I'm trying to convert a double type matrix to an image, like using imagesc function in MATLAB. I know that we can use imshow to do that. But it shows 'Access Violation' error when I tried to use a matrix of size 600 * 900. I'm using Visual Studio 2010 on Windows 8 with OpenCV 2.4.5 .
#include <stdio.h>
#include "device_launch_parameters.h"
#include "cuda_runtime_api.h"
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <iostream>
int main()
{
int iter1;
float *A;
A=(float*)malloc(sizeof(float) * 50 * 50);
for(iter1=0; iter1<(50 * 50); iter1++)
{
A[iter1] = iter1;
}
cv::Mat test(50, 50, CV_32F, &A);
cv::imshow("test", test);
cv::waitKey();
cudaDeviceReset();
return 0;
}
The above is the code I used for producing an image out of 50 * 50 matrix which worked fine. Kindly let me know if you find the problem with bigger matrix or give me some sample code if you have any.
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.