I write simple application and have problem with raspicam library. I've opened simpletest_raspicamm.cpp:
#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
using namespace std;
int main ( int argc,char **argv ) {
raspicam::RaspiCam Camera; //Cmaera object
//Open camera
cout<<"Opening Camera..."<<endl;
if ( !Camera.open()) {cerr<<"Error opening camera"<<endl;return -1;}
//wait a while until camera stabilizes
cout<<"Sleeping for 3 secs"<<endl;
//capture
Camera.grab();
//allocate memory
unsigned char *data=new unsigned char[ Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
//extract the image in rgb format
Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
//save
std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
cout<<"Image saved at raspicam_image.ppm"<<endl;
//free resrources
delete data;
return 0;
}
And console returns me cout:
/home/pi/Desktop/raspicam-0.0.7/src/private/private_impl.cpp:171 :Private_Impl::retrieve type is not RASPICAM_FORMAT_IGNORE as it should be
Image saved at raspicam_image.ppm
I use raspicam 0.0.7 i tried to use every other version and nothings changes. I compile use command:
g++ -ggdb -o `basename server.cpp .cpp` server1.cpp -I/usr/local/include/ - lraspicam -L/opt/vc/lib
I've tried to use Camera.setFormat(raspicam::RASPICAM_FORMAT_IGNORE) and have no idea how to fix it. I work on raspberry pi 2, but on rpi3 everything works fine.
It appears that the interface of retrieve() has been changed since v0.0.5 according to the comments in the header file.
Basically the parameter is useless now, and can be always set to RASPICAM_FORMAT_IGNORE, or just left blank for the C++ default parameter to kick in. The warning you're seeing basically does nothing and won't have any impact on the performance.
Format can now be set by an independent function
void setFormat ( RASPICAM_FORMAT fmt );
Related
I am compiling on Ubuntu 14.04 using OpenCV 3.1. When trying to open a video file it gives this error:
"Cannot open the video file"
I installed everything i could install : ffmpeg etc. Haven't found a solution checking out similar questions on StackOF.
What do ?
cv::VideoCapture cap(argv[1]);
Where argv[1] is the file name in the same directory as the executable.
In case your constructor is failing, you may want to use the .open() method. So, if you want to open a file that is called "myVideo.mp4" that is in the folder of your project, you would do the following:
cv::VideoCapture cap;
cap.open("myVideo.mp4");
For more detailed informations about this method, check this documentation link
Also, the book Learning OpenCV 3, from the O'Rilley media, on page 26 gives you a good example. Here is a Gist that I made to give you as an example.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main() {
cv::VideoCapture cap;
cap.open("myVideo.mp4" );
cv::namedWindow( "myVideo", cv::WINDOW_AUTOSIZE );
cv::Mat frame;
while(true) {
cap >> frame;
if( frame.empty() ){
std::cout << "Could not load the video frames. \n";
break;
}
cv::imshow( "myVideo", frame );
if( cv::waitKey(27) >= 0 ){
std::cout << "Escape pressed \n";
break;
}
}
return 0;
}
I copy this code from openCV2 book and this code have argc and argv as arguments that I don't know what they are and why assign to 1 (argc=1) and terminate debugging ...my problem that why argc=1? And how I can fix it? because my argc should be 2 (argc==2)...
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
if ( argc ! = 2 )
{
printf( " usage: DisplayImage.out <Image_Path>\n" );
return - 1;
}
Mat image;
image = imread( argv[ 1], 1 );
if ( ! image.data )
{
printf( " No image data \n" );
return - 1;
}
namedWindow( " Display Image" , WINDOW_AUTOSIZE );
imshow( " Display Image" , image);
waitKey( 0);
return 0;
}
I try to wrote this code without argc and argv but the debugger has a runtime error that i think its cause be argc.
If you run it by command line, you can call it as
DisplayImage "path_to_image"
If you want to run from VS, be sure to add the "path_to_image" in your command arguments:
PROJECT -> Properties -> Configuration Properties -> Debugging
Put the "path_to_image" in Command Arguments
Be sure that "path_to_image" is a valid path, check here for reference.
As already pointed out in comments, it's a very bad example!
OpenCV 3.0.0 has also CommandLineParser for more complex input arguments.
So I have played around in OpenCV a bunch before and never run into this problem. I am implementing a MeanShift algorithm and trying to do it on video devices, images, and videos. Devices and images work; however, no matter what I try, when I run VideoCapture on my filename (whether setting it in the Constructor or using the VideoCapture::open() method, and whether local or with a full path) I always get stuck in my error check.
Thoughts? Ideas? code below. running in Visual Studio 2012
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\video\video.hpp"
#include <string>
using cv::Mat;
using std::string;
enum Filetype{Image, Video};
int main(int argc, char* argv[])
{
string filename = "short_front.avi";// "C:\\Users\\Jonathan\\Videos\\short_front.mp4"; //"hallways.jpg";
Mat cv_image; //convert to unsigned char * with data
Mat filtImage_;
Mat segmImage_;
Mat whiteImage_;
cv::VideoCapture vid;
vid.open("C:/Users/Jonathan/Desktop/TestMeanShift/TestMeanShift/short_front.avi");
cv::waitKey(1000);
if ( !vid.isOpened() ){
throw "Error when reading vid";
cv::waitKey(0);
return -1;
}
// cv_image = cv::imread(filename);//, CV_LOAD_IMAGE_COLOR);
// if(! cv_image.data){
// std::cerr << "Image Failure: " << std::endl;
// system("pause");
// return -1;
// }
//Mat cv_image_gray;
//cv::cvtColor(cv_image,cv_image_gray,CV_RGB2GRAY);
for (;;)
{
vid >> cv_image;
if ( !cv_image.data)
continue;
cv::imshow("Input",cv_image); //add a normal window here to resizable
}
EDIT: This is a distinct problem from the one listed here because it deals with a specific corner case: VideoCapture and ImageCapture both work, only not VideoCapture with a file. When it doesn't work, the code runs properly, except that the "video" it creates is incomplete as it didn't open properly. Therefore, as the code above does not crash in compile time or run time, the only indicator is bad output (6KB video output file). If you are having issues not with the corner case I am describing but general issues with the above functions in OpenCV, the aforementioned link could help you.
I found that this question has been asked many times here, but I haven't found any solution or work-around to this problem. Here's my code (copied from here: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int _tmain(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", 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;
}
I compiled this using Visual Studio 2008 and 2010, and got different results (both don't work). The program compiled using VS 2008 has run-time error at imread(), and the other displays the message "Could not open or find the image".
Anybody can help me with this?
The problem here is your main() function._tmain does not exist in C++. main does.
_tmain is a Microsoft extension. Here is a nice explanation of these two methods.
Further more if you want to add default argument in Visual studio please follow these steps.
Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
Hope this solves your problem!
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
if( argc != 2)
{
cout <<"No Commandline Aurgument Found!: 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", 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;
}
set argv[1] to be a known image page "C:\test.jpg"
Ok, Ive read all the comments and I'm going to answer the main question along with the sub questions.
Why Doesn't my Code Work in VS2008?
The reason your code doesn't work in VS2008 is because you are using the compiled libraries for 2010, at least I think this is a pretty accurate assumption. If you want to be completely accurate then build the libraries, for the compiler you are using.
What is tmain & what is main
This stack overflow question answers the subject a lot better than I ever could, but effectively it is a windows specific main and does not actually exist in C++. It get removes by the compiler on compile time and converter to main.
Can you please try this: Use known-to-work image and absolute path until it works so you can be sure that there is no problem with image or relative path.
download http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png to C:\Lenna.png
rename your main function to something else and try this: if it does not work, please tell me the displayed name of the output window.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
Mat image;
image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.cols ) // Check for invalid input
{
cout << "Could not open or find the image. Press any key to exit." << std::endl ;
cv::waitKey(0)
return -1;
}
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Please try this and if it does not work, please tell me the displayed name of the output window.
I am trying to develop a stereoscopic vision system. I am receiving the messages below whenever I try to build my code:
***** Build of configuration Debug for project RicoCameraCpp ****
make all
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -I/home/ux/Downloads -I/usr/local/boost_1_52_0/boost -I/usr/local/boost_1_52_0 - I/home/ux/Downloads/opencv2 -include/home/ux/Downloads/opencv2/opencv_modules.hpp -include/usr/local/boost_1_52_0/boost/thread.hpp -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
In file included from /usr/local/boost_1_52_0/boost/thread/thread.hpp:22:0,
from /usr/local/boost_1_52_0/boost/thread.hpp:13,
from <command-line>:0:
/usr/local/boost_1_52_0/boost/thread/detail/thread.hpp: In member function ‘void boost::detail::thread_data<F>::run() [with F = int (*)(int, char**)]’:
../main.cpp:81:1: instantiated from here
/usr/local/boost_1_52_0/boost/thread/detail/thread.hpp:78:17: error: too few arguments to function
/usr/local/boost_1_52_0/boost/system/error_code.hpp: At global scope:
/usr/local/boost_1_52_0/boost/system/error_code.hpp:214:36: warning: ‘boost::system::posix_category’ defined but not used [-Wunused-variable]
/usr/local/boost_1_52_0/boost/system/error_code.hpp:215:36: warning: ‘boost::system::errno_ecat’ defined but not used [-Wunused-variable]
/usr/local/boost_1_52_0/boost/system/error_code.hpp:216:36: warning: ‘boost::system::native_ecat’ defined but not used [-Wunused-variable]
make: *** [main.o] Error 1
**** Build Finished *****
Here's my code:
#include "cstdlib"
#include "cmath"
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "boost/thread.hpp"
#include <iostream>
using namespace std;
using namespace boost;
using namespace cv;
int firstCam( int argc, char** argv )
{
//initilize first camera
CvCapture* captureRightCam = cvCaptureFromCAM(0);
//check if first camera is available
if(!captureRightCam)
{
cout << "No first camera to capture\n";
return(-1);
}
//create right window
cvNamedWindow( "Right Cam", CV_WINDOW_AUTOSIZE );
//display frames for every cvWaitKey duration
while ( 1 )
{
//get frames
IplImage* rightFrame = cvQueryFrame( captureRightCam );
//check if captured
if ( !rightFrame )
{
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
//show frames inside the windows
cvShowImage( "Right Cam", rightFrame );
cvWaitKey(150);
}
//release and destroy windows
cvReleaseCapture( &captureRightCam );
cvDestroyWindow( "Right Cam" );
return 0;
}
int secondCam( int argc, char** argv )
{
CvCapture* captureLeftCam = cvCaptureFromCAM(1);
if(!captureLeftCam)
{
cout << "No second camera to capture\n";
return(-1);
}
cvNamedWindow( "Left Cam", CV_WINDOW_AUTOSIZE );
while ( 1 )
{
IplImage* leftFrame = cvQueryFrame( captureLeftCam );
if ( !leftFrame )
{
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "Left Cam", leftFrame );
cvWaitKey(150);
}
cvReleaseCapture( &captureLeftCam );
cvDestroyWindow( "Left Cam" );
return 0;
}
int main()
{
boost::thread t1(firstCam);
boost::thread t2(secondCam);
return 0;
}
What am I doing wrong?
I think the error message is very descriptive, actually:
You are trying to make a thread out of your function firstCam. That function takes two arguments, but when you create your thread, you don't give it any arguments. It therefore can't figure out what arguments to pass to your function and therefore complains about "too few arguments".
In this case, it seems that you copied the function signatures from somewhere without giving it any thought. You never use argc and argv at all.
(As a side note:
using namespace std;
using namespace boost;
using namespace cv;
is a bad idea and will get you into trouble sooner or later. See GotW 53 for details.)