I wrote a simple program in OpenCV that detects SURF feature in a given image and diplays the detected features in a namedWindow.
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
using namespace cv;
int main(int argc,char** argv)
{
if(argc!=3)//Check cmd number of argumets
{
std::cout<<"Usage: "<<argv[0]<<" <image-file> <method>"<<std::endl;
return -1;
}
//LOAD THE SOURCE IMAGE
Mat Img = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)//Check correct image load
{
std::cout<<"Cannot read image file. Check file path!"<<std::endl;
return -1;
}
//COMPUTE FEATURES
SurfFeatureDetector detector;
std::vector<KeyPoint> features;
detector.detect(Img,features);
//SHOW RESULT
Mat ImgF;
drawKeypoints(Img,features,ImgF);
namedWindow("Features", CV_GUI_NORMAL);
imshow("Features",ImgF);
waitKey();
return 0;
}
Everything is OK, the programs do what it have to do. The problem is when pressing a key to terminate the program a crash error occurs.
It doesn't crash for me... but in order for me to compile your code, I had to add
#include <opencv2/nonfree/features2d.hpp>
because SURF was moved to the nonfree module at some point.
So, I would have to recommend trying the newest version (2.4.6 as of today).
Related
I am going through the book Learning OpenCV 3 and test out the video example 2.3. I could edit, compile and run it, but the problem is that it closed down immediately.
// DisplayPicture.cpp : Defines the entry point for the console application.
//
//#include "opencv2/opencv.hpp" // Include file for every supported OpenCV function
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <opencv2/videoio.hpp>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
namedWindow("video3", WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open( string(argv[1]));
int tell = 0;
Mat frame;
for (;;) {
cap >> frame;
//waitKey(30);
if (frame.empty())
{
break;
//end of film
}
imshow("video3", frame);
}
return 0;
}
I found that my computer processed the data too fast. It could not read the next frame fast enough. if (frame.empty()) became true the program reached the break statement and ended.
By adding a waitkey of 30 millisec before viewing the image frame, the video program works very well. At least I can view the video. Since this example is from the 'bible' it should work, but not with my computer.
I am running a MSI gt72 2PE computer with nvidia gtx880m. Not sure if that matters.
I assume that adding a waitKey(30) is not appropriate, so I am seeking suggestions as to what could be done differently.
Every time i compile this code the webcam streaming is mirrored like I lift up my right hand and it appears like if it was my left hand on the screen instead and after a couple of re-compiling an error message appeared and the code never worked again.
The error:
Unhandled exception at 0x00007FFB3C6DA1C8 in Camera.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000D18AD5F610.
And no other option left except to break the process.
The code:
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
imshow("window", image);
waitKey(33);
}
}
I don't know if something wrong with my code I've just started learning opencv !
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
flip(image,image,1)
imshow("window", image);
waitKey(33);
}
}
just Flip the image horizontally that will do Find More Here
There is nothing wrong when your image is mirrored on the vertical (/x) axis (I guess in your example you are using a built-in (laptop) webcam).
A (very) simple code for capturing and showing your image could be the following:
// imgcap in opencv3
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
int main() {
cv::VideoCapture cap(0); // camera 0
cv::Mat img;
while(true) {
cap >> img;
cv::flip(img,img,1);
cv::imshow("live view", img);
cv::waitKey(1);
}
return 0;
}
When using OpenCV 3 you should include headers like:
#include <opencv2/highgui.hpp>
I am currently doing some C++ image processing with openCV. I developed the application on a Mac with Xcode 6.3.2 and it works perfectly in both debug and release. In order to have a Windows executable program, I am now working on Windows with Visual Studio Express 2013. The program is running well on debug mode but crashes in release mode on this part of the code :
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/photo/photo.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
vector<Mat> stacked_images;
Mat medianr_eq, mediang_eq, medianb_eq, objrgb;
medianr_eq = imread("C:\\Path\\medianr_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
mediang_eq = imread("C:\\Path\\mediang_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
medianb_eq = imread("C:\\Path\\medianb_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
objrgb = Mat(medianr_eq.size(), CV_16UC3);
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(stacked_images, objrgb);
}
The error I get is :
OpenCV Error : Assertion failed <mv && n > 0> in cv::merge, file C:\builds\master_PackSlave_Win64-vc12-shared\opencv\modules\core\src\convert.cpp, line 941
I can't see where I could have done something wrong... Indeed, it is pretty basic OpenCV !
The images I used are downloadable with this link : https://transfert.u-psud.fr/gs67
For astronomy lovers it is the Stephan's Quintet, taken with Calar Alto Observatory's 1.23m telescope where I currently am an intern.
Thank you in advance for your help,
Arnaud.
I recently had the exact same error, appearing with a valid openCV code running perfectly fine in debug mode but not in release mode.
Looking into the openCV source code, one can find that the function called has this code (modules/core/src/convert.cpp, line 341):
void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
{
CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
ocl_merge(_mv, _dst))
std::vector<Mat> mv;
_mv.getMatVector(mv);
merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
}
the last line here calls the function 'void cv::merge(const Mat mv, size_t n, OutputArray _dst)*', which has the infamous CV_Assert( mv && n > 0 ); instruction in its first line, causing the crash at runtime.
This error tells us that the vector of Mat is either a null pointer/reference or empty, which it clearly isn't in your code. I strongly suspect that the error is in the getMatVector function call, not copying the contents of _mv into mv. This leaves an empty array that is passed to the merge function, causing the error raised by CV_Assert.
In my case, the fix was to use directly the prototype of the function defined at line 200 in convert.cpp. For you, that would mean (copying only the last few lines of your code):
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(&stacked_images[0], stacked_images.size(), objrgb);
This solution worked in my case, and my code is now happily running in debug and release mode !
PS: I know it has been a while, but thought I would post the answer anyway in case somebody running into the same problem would arrive on this SO question.
PS2: Here is a full example code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
namedWindow( "Display window", WINDOW_AUTOSIZE );
Mat result;
Mat R = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat G = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat B = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
// Changing channel values for final result that should look largely blue
R = 0.1*R;
G = 0.1*G;
B = 1.5*B;
std::vector<cv::Mat> array_to_merge ;
array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);
// This line triggers a runtime error in Release mode
//cv::merge(array_to_merge,result);
// This line works both in Release and Debug mode
cv::merge(&array_to_merge[0], array_to_merge.size(), result);
cv::imshow( "Display window", result );
cv::waitKey(0);
}
Although you could write it in a lot fewer lines , this code is valid.
Hey please can someone help me to found the solution of this error :
Assertion failed in unknown function ../../ocv/opencv/modules/imgproc/src/imgwarp.cpp
i try to compile the code that exist in this link : http://ipwithopencv.googlecode.com/svn/trunk/ThinPlateSpline/ThinPlateSpline/
im using 2 classes and the main:
here is my main code :
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "CThinPlateSpline.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// load a nice picture
cv::Mat img = cv::imread("lena.jpg");
// generate some generic points
// usually you would use a interest point detector such as SURF or SIFT
std::vector<cv::Point> iP, iiP;
// push some points into the vector for the source image
iP.push_back(cv::Point(50,50));
iP.push_back(cv::Point(400,50));
iP.push_back(cv::Point(50,400));
iP.push_back(cv::Point(400,400));
iP.push_back(cv::Point(256,256));
iP.push_back(cv::Point(150,256));
// push some point into the vector for the dst image
iiP.push_back(cv::Point(70,70));
iiP.push_back(cv::Point(430,60));
iiP.push_back(cv::Point(60,410));
iiP.push_back(cv::Point(430,420));
iiP.push_back(cv::Point(220,280));
iiP.push_back(cv::Point(180,240));
// create thin plate spline object and put the vectors into the constructor
CThinPlateSpline tps(iP,iiP);
// warp the image to dst
Mat dst;
tps.warpImage(img,dst,0.01,INTER_CUBIC,BACK_WARP);
// show images
cv::imshow("original",img);
cv::imshow("distorted",dst);
//cv::waitKey(0);
//Sleep(5);
cv::waitKey(5000);
return 0;
}
here is the is the imagewarp method :
void CThinPlateSpline::warpImage(const Mat& src, Mat& dst, float lambda, const int interpolation,const TPS_INTERPOLATION tpsInter)
{
Size size = src.size();
dst = Mat(size,src.type());
// only compute the coefficients new if they weren't already computed
// or there had been changes to the points
if(tpsInter == BACK_WARP && !FLAG_COEFFS_BACK_WARP_SET)
{
computeSplineCoeffs(pSrc,pDst,lambda,tpsInter);
}
else if(tpsInter == FORWARD_WARP && !FLAG_COEFFS_FORWARD_WARP_SET)
{
computeSplineCoeffs(pSrc,pDst,lambda,tpsInter);
}
computeMaps(size,mapx,mapy);
remap(src,dst,mapx,mapy,interpolation);
}
there is to other classes that exist in the link also CthinPlateSpline.cpp and CthinPlateSpline.h ...please i really need help and sorry for my bad english
"Assertion failed" in OpenCV usually happens when your input Mat has incorrect size, e.g. zero (empty Mat) or less than the requirement of the function.
Would you mind to check img after cv::Mat img = cv::imread("lena.jpg");
by img.empty() or display img by imshow?
I can run these on my mac with OpenCV 2.4.7
The result image is a twisted lena
I just change two places as following
First of all, I change the included file in main.cpp to become
#include <opencv2/opencv.hpp>
#include "CThinPlateSpline.h"
#include <iostream>
#include <vector>
And included file in CThinPlateSpline.cpp to become
#include <vector>
#include <opencv2/opencv.hpp>
#include "CThinPlateSpline.h"
I use only 3 files main.cpp, CThinPlateSpline.cpp, and CThinPlateSpline.h
libraries linked are
-lopencv_imgproc -lopencv_core -lopencv_highgui
Verify if the image was loaded correctly. In my case (OpenCV for Python) my image was not loaded at all.
Try to verify what image exactly are you passing to the function.
Im using Opencv 2.3.1 on Visual studio 2010 (vc10)
I have configured opencv based on many tutorials and can compile & run C-syntax program like:
#include "StdAfx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main ()
{
IplImage* img = cvLoadImage("D:\cat_helmet.jpg", CV_LOAD_IMAGE_UNCHANGED);
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
However, I cannot run the C++ syntax program like
#include "StdAfx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
Mat image;
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
if(! image.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
imshow( "Display window", image );
waitKey(0);
return 0;
}
I got the error messages (in the function calls: namedWindow, imread, imshow)
First-chance exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
Unhandled exception at 0x5361fcc3 in FirstOpencv2.3.exe: 0xC0000005: Access violation reading location 0x2079616c.
How can I fix this?
You say that you have followed a multitude of guides and tutorials. I've had great success with this one
http://www.anlak.com/using-opencv-2-3-1-with-visual-studio-2010-tutorial/
The thing is that this guy walks you through the 'park' and helps you unravel two major issues whilst setting up OpenCV 2.3.1; one of which is placement of .dll files in your project folder. The other is a missing .dll 'tbb_debug.dll' (the absense of this .dll is considered a bug in OpenCV 2.3.1).
He also provides some decent code-snippets for your to try out (in c++ syntax).
Good luck.
The above mentioned answers doesn't make sense. I am also facing the same problem. The main reason for this exception is that you are trying to display image (read by imread) which is empty. The main problem in the program is the line
image = imread("D:\cat_helmet", CV_LOAD_IMAGE_COLOR);
I think imread function is not behaving the way it is expected. One more thing, while going through the references i came across a following link:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)
Here, imread is used via call by reference method. I am not a C++ expert, but i feel it could be the problem.
int main()
{
std::string imgPath("splash.bmp"); //Add your file name
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
This code worked for me. Also, I put the file next to executable to decrease complexity.