How to operate an PS eye with openCV - opencv

I would like to use the external PS eye cam to save 30 frames a second if that is possible.
I don't know where to find guides or the code it self since I'm pretty sure it should be somewhere online.
Anyhelp would be appreciated. Thanks in advance!

So, getting the ps3eye up and running differs depending on which OS you're running. If you're running any flavor of Debian, the drivers are already there and the code I've got below should work fine.
If you're on Windows, you'll have to find a driver for it. CodeLibrary has a driver already made, but you'll have to pay $3 for it. Link's here.
I don't know for Mac, but a bit of digging found this, which might work.
Once you've got the drivers installed, you should be able to access it like any other camera.
Simple bit of code for that is below:
#include "stdafx.h"
#include <opencv/cxcore.h>
#include <opencv2\core\mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/videoio/videoio.hpp>
using namespace cv;
using namespace std;
int main() {
Mat image;
bool escnotpressed = true;
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
cap.set(CV_CAP_PROP_FPS, 30); //sets framerate
String capturePath = "C:/this/is/a/path.avi";
Size frameSize = Size(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter savedCapture;
savedCapture.open(capturePath, VideoWriter::fourcc('M','J','P','G'), 30.0, frameSize, true);
if (!savedCapture.isOpened()) {
return -2;
}
while(escnotpressed) { //loops
cap >> image;
if (image.empty()) {
cout << "camera feed got interrupted" << endl;
return 5; //dies if camera feed is interrupted for some reason
}
imshow("Image", image);
savedCapture << image;
int c = waitKey(10);
if( (char)c == 27 ) { escnotpressed = false;}
}
savedCapture.release();
cout << "Done!" << endl;
}
EDIT: If you've got multiple webcams on your computer, you might need to change that VideoCapture cap(0) to VideoCapture cap(x), where x gives you the right camera.

Related

Image panorama stitching black spots

Maybe someone knows what can cause that problem.
I have a simple code for stitching 12 images in one panorama image, but faced with strange behaviour.
Here the result of stitching on my MacBook Pro:
https://drive.google.com/open?id=1f_7i_rb8pnbHEBxlowzFa13MAHvxN07u
And result of stitching on my Desktop Mac:
https://drive.google.com/open?id=19CoyPzz6QgDjqG1lUZbphGPYvCAg41hi
The code i used is the same and images same too. On my desktop mac i can build and run it without problems, but on my MacBook it not working at all, it shows me the next error:
OpenCV Error: Assertion failed (imgs.size() == imgs_.size())
in composePanorama, file /tmp/opencv-20171031-87373-6u1izq/opencv-3.3.1/modules/stitching/src/stitcher.cpp, line 168
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20171031-87373-6u1izq/opencv-3.3.1/modules/stitching/src/stitcher.cpp:168:
error: (-215) imgs.size() == imgs_.size() in function composePanorama
This is my code:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv ) {
vector<Mat> images_arr;
const string images[] = {
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/1.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/2.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/3.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/4.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/5.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/6.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/7.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/8.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/9.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/10.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/11.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/12.jpeg",
};
const char* outFile = "/Users/george/CLionProjects/OpenCV_Stitch/out.jpeg";
for(auto path : images) {
Mat image = imread(path, IMREAD_REDUCED_COLOR_2 | IMREAD_IGNORE_ORIENTATION);
images_arr.push_back(image);
}
Mat pano;
Stitcher stitcher = Stitcher::createDefault(false);
stitcher.setRegistrationResol(0.8);
stitcher.setSeamEstimationResol(0.1);
stitcher.setCompositingResol(1);
stitcher.setPanoConfidenceThresh(1);
stitcher.setWaveCorrection(true);
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
Stitcher::Status status1 = stitcher.estimateTransform(images_arr);
Stitcher::Status status = stitcher.composePanorama(images_arr, pano);
if (status != Stitcher::OK) {
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
imwrite(outFile, pano);
return 0;
}
My CmakeLists.txt configuration:
cmake_minimum_required(VERSION 3.8)
project(OpenCV_Stitch)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(OpenCV_Stitch ${SOURCE_FILES})
target_link_libraries(OpenCV_Stitch ${OpenCV_LIBS})
Can't even imagine what can cause that black spots on image. Did anyone come across this? And sometimes, when i use different images to stitch opencv returns error: Camera parameters adjusting failed
How can i prevent that? Maybe use some methods to estimate camera rotation or something like that?
Here the link to images what i try to stitch:
https://drive.google.com/open?id=1-DfSf8eaC7bi37-ZhBpaRt8jHVvO_Qwb
Thanks!
i get the same error message with your code. when i modified the part below it runs without error.
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(Stitcher::PANORAMA, false);
stitcher->setRegistrationResol(0.8);
stitcher->setSeamEstimationResol(0.1);
stitcher->setCompositingResol(1);
stitcher->setPanoConfidenceThresh(1);
stitcher->setWaveCorrection(true);
stitcher->setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
Stitcher::Status status = stitcher->stitch(images_arr, pano);

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.

MJPEG network stream to OpenCV 2

Can anyone explain to me why this code below does not work?
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
const std::string videoStreamAddress = "http://hg55.no-ip.org/mjpg/video.mjpg";
//Yes, this stream does work! Try to paste it into your browser...
//open the video stream and make sure it's opened
if(!vcap.open(videoStreamAddress)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if(cv::waitKey(1) >= 0) break;
}
}
This code does cannot open the stream...
The code is based on some code in this thread: OpenCV with Network Cameras
The OpenCV 1 code here works without any problem for me.
Thank you very much in advance
I tried to create a new project with Visual Studio 2010 and OpenCV 2.2, instead of OpenCV 2.3.1.
This solved all my problems!

Resources