OPEN CV Program execution error? - opencv

I am running my below code in eclipse, i have include the path and libraries successfuly, but when run the code it shows an error.
#include <cv.h>
#include<stdio.h>
#include <highgui.h>
//using namespace cv;
int main()
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
printf("this is open cv programming");
return 0;
}

Your main() signature is incomplete
try
int main(int argc, char* argv[])
these parameters represent:
argc // an int indicating the number of arguments passed in to the function
argv[] // an array of character strings, the actual arguments.
The first argument argv[0] is the program name ... so argc is always a minimum of 1.
The second argument, argv[1] will be the first argument your user passes in, bringing argc up to 2. That is what your program is expecting, a single argument from the user, argc == 2.

Try to use the latest version of OpenCV i.e. 2.4.3....however right now you can try to link the debug libraries e.g. opencv_core2.4.xd and run the program to get the Mat image format working.
what is the version of opencv you are using?
try the following code and test...get some picture and run it....
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat im = imread("C:\\some_picture.jpg");
if(im.empty())
return -1;
imshow("TEST",im);
waitKey();
return 0;
}

Related

OpenCv 3.1.0 undefined reference to imread

This is what my code looks like, I got the undefined reference to imread and so on:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, const char** argv )
{
Mat img = imread("MyPicture.jpg", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
I am using Codeblocks and the g++ compiler. Also I have linked the opencv_world310d.lib to debug and opencv_world310.lib to release.
Plus I have given the path in searchdirectory compiler and linker.
Any hints?

opencv c++ HSV image channel separation exception

I know this question has been asked a number of times and I'm trying to implement their answers but its causing an exception in my code.
OS: Windows 7
OpenCV: 2.4.9
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from webcam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
Mat imgHSV;
_sleep(100); //give the camera a chance to wake up
while (true)
{
Mat imgOriginal;
bool success = cap.read(imgOriginal);
if (!success) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
vector<Mat> hsv_planes;
split( imgHSV, hsv_planes );
//hsv_planes[0] // H channel
//hsv_planes[1] // S channel
//hsv_planes[2] // V channel
imshow(thresholded_window, hsg_planes[2]); //show the S channel
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Exception is thrown on the split line: "Unhandled exception at 0x62B978C9 (opencv_core249.dll) in TrackColour.exe: 0xC0000005: Access violation writing location 0xFEEEFEEE."
I figured out my problem, incase this happens to anyone else.
In VS I was doing a debug build but I was linking to the non debug versions of opencv. Notice in my error message opencv_core249, it should have been opencv_core249d (Note the 'd'). I updated my CV linking to use the debug libraries and it works.
The other opencv calls performed fine using the wrong libraries, something must be unique with split.

What is wrong with this OpenCv code?

I am trying frame difference in this opencv code (C API).
It gives me an error:
Assertion failed (src1.size() == dst.size() && src1.type() == dst. type()) in unknown function, file ........\ocv\opencv\src\cxcore\cxarithm.cpp , line 1563.
The code is as follow. (When I try to run a video file, this program seems to run without any error, but when I am trying to capture from laptop camera, it gives this error. How do I fix this?
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
int key=0;
//CvCapture *capture=cvCreateCameraCapture(0);
CvCapture *capture=cvCaptureFromAVI("cmake.avi");
IplImage *frame=cvQueryFrame(capture);
IplImage *currframe=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IplImage *dstframe=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow("output",CV_WINDOW_NORMAL);
while(key!='x'){
currframe=cvCloneImage(frame);
frame=cvQueryFrame(capture);
//cvCopy(frame,currframe,0);
frame=cvQueryFrame(capture);
cvSub(frame,currframe,dstframe);
if(key==27) break;
cvShowImage("output",dstframe);
key = cvWaitKey( 1000 / fps );
}
cvReleaseCapture(&capture);
cvDestroyWindow("output");
return 0;
}
The error is saying that you are trying to do an operation that needs images of the same size and type. If you run your code in a debugger you can see which line this occurs on.
It is probably one of the destination images you are creating. A t least in the C++ api it is best not to create destination images but just declare them and let the function allocate what it needs

Assertion error with imshow

OK, i know this question may not be new and I've already gone through a few posts covering the same issue but it hasn't really helped. I am new to opencv and I am trying to load an image (in a folder that's different from the one where the executable file's stored) using imread and display it using imshow. Its the part of a much bigger code but I've shown the part that covers the issue as a separate code here:
#include <stdio.h>
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv/highgui.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include <fstream>
#include <sstream>
#include <cctype>
#include <iterator>
#include <cstring>
#include <highgui.h>
#include <string.h>
int disp(char * filename);
using namespace std;
using namespace cv;
int main()
{
disp("file.txt");
}
int disp(char * filename)
{
FILE * fp;
char shr[50];
char ch;
if( !(fp = fopen(filename, "rt")) )
{
fprintf(stderr, "Can\'t open file %s\n", filename);
return 0;
}
for(i=0;ch != '\n';i++)
{
ch = fgetc(imgListFile);
shr[i] = ch;
}
string str(shr);
Mat image=imread(str.c_str(),1);
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image",image);
cvWaitKey(0);
}
The "file.txt" is a text file containing the full path of the image i want to load and display. I am reading it into a character array, converting it to a string and passing it to the imshow/imread functions. I am not getting any errors while compiling, however, i am getting an error while i run the code:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/ubuntu/Desktop/OpenCV/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
terminate called after throwing an instance of 'cv::Exception'
what(): /home/ubuntu/Desktop/OpenCV/opencv-2.4.6.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
Aborted (core dumped)
I tried debugging the code, even re-compiled opencv; but i am getting the same issue again & again. I need help !!!
Hope i've explained my issue properly. Thanks in advance !!!
P.S: The text file actually contains a number before every image path; and i need to remove the number before i can feed the path to the imshow/imread functions; that's the reason i am trying to read the text file and store in a character array (so that i can get rid of the first 2 characters first).
The error message tells you that the image a 0 rows and/or 0 columns. This is usually caused by an incorrect path to image, or by an an image type that is not handled by your installation of OpenCV.
To debug it, you need to print out the argument of imread() and compare it with the catual location of the file on your system.
your for loop here is broken:
for(i=0;ch != '\n';i++)
{
ch = fgetc(imgListFile);
// you have to check the value of ch *after* reading
if ( ch == '\n' )
break; // else, you still append the '\n' to your filename
shr[i] = ch;
}
// z terminate
shr[i] = 0;
so, - you get empty images because of broken path.
There are some typos in your code. Also, it appears as if you haven't completely grasped the concepts of file handling and string allocations using c++; I would advise you to read up on those.. I have re-written your code as follows,
int main()
{
disp("file.txt");
return EXIT_SUCCESS;
}
void disp(char* filename)
{
ifstream myReadFile;
char shr[500];
myReadFile.open(filename);
if(myReadFile.is_open())
while(!myReadFile.eof())
myReadFile >> shr;
string str(shr);
Mat image=imread(str.c_str(),1);
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image",image);
cvWaitKey(0);
}
Hope this helps.

cv.h: file not found opencv

I'm running this code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0; }
But it isn't working for me because it shows this error on my screen when I write cmake . in terminal:
cv.h: file not found.
Could anyone help me? It was working one month ago.
you have linking problem, try this out;
g++ `pkg-config --cflags --libs opencv` filename.cpp -o filename
You can change the linker properties in case of Visual studio.
For Makefile you need to make sure that the opencv_dir is set correctly.

Resources