Pi Camera + openCV connection issues with V4l2 already installed? - c++

I am using openCV to access the camera- and for some reason it will not connect to the pi camera, Can anyone see any reason why that may be?
my code to test camera is as follows:
#include <stdio.h>
#include <pigpio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>
#include "move.h"
#include "init.h"
#include <iostream>
using namespace std;
#include "distance.h"
#include <linux/videodev2.h>
#include <../include/libv4l2.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp>
using namespace cv;
void TestCamera(){
Mat initframe,blurr,B_W,edges;
//Mat image;
VideoCapture cap;
cap.open(0);
cap.set(CAP_PROP_FRAME_WIDTH,500);
cap.set(CAP_PROP_FRAME_HEIGHT,500);
if (cap.isOpened()){
for(;;)
{
cap.read(initframe);
if (initframe.empty()){ printf("error frame empty/n"); break;}
imshow("Live Stream",initframe); //normal bgr output
cvtColor(initframe,B_W,CV_BGR2GRAY);//b&W stream
imshow("greyscale",B_W);
GaussianBlur(B_W,blurr,Size(9,9),1.5,1.5);//blur applied so edge detction is smoother (less hard edges)
imshow("blurr",blurr);
Canny(blurr,edges,0,30,3);//edge detection
imshow("edges",edges);
if(waitKey(30)>= 0) break;
}
}else printf("error! unable to use camera\n");
}
and the build command is:
g++ -Wall -I/usr/local/include $(pkg-config opencv --libs) $(pkg-config opencv --cflags) -o "SdCar" "SdCar.cpp" -lwiringPi -lpigpio -lv4l2 (in directory: /home/pi/selfdrivingcar-17)
I cant figure out why it builds but whenever the camera should run it says "error!unable to open camera" which is the error associated with cap.IsOpened() function.
any ideas?

Related

opencv build failed with xcode

I have a problem with a webcam capturing in opencv.
This can be built successfully:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main() {
// VideoCapture cap(0);
// while(true){
// Mat Webcam;
// cap.read(Webcam);
// imshow("Webcam", Webcam);
// }
}
However, this is not:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main() {
VideoCapture cap(0);
while(true){
Mat Webcam;
cap.read(Webcam);
imshow("Webcam", Webcam);
}
}
There is no error or warning message popped out, so I can't solve it by myself.
Any idea is appreciated!
Updates:
Error message
It seems something went wrong when I updated xcode.....
Error message 2
Your code is perfectly ok except following line after imshow:
waitKey(10);
It will provide ui thread to draw the frames. Without this delay ui thread can not be updated or get time slice from cpu.

OpenCV 3 SURF feature detection

I am trying to do a simple surf feature detection.
The problem is that during the execution I have two Microsoft Visual C++ Runtime Library Errors with only the use of the detect method :
Debug Assertion Failed ! Program:
C:\Windows\system32\MSVCP120D.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 240
Expression : vector iterators incompatible
And
Debug Assertion Failed ! Program:
C:\opencv\build\x64\vc12\bin\Debug\opencv_xfeatures2d300d.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 241
Expression : "Standard C++ Libraries Invalid Argument" && 0
This is the code :
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv/cv.h"
#include "opencv/highgui.h"
using namespace cv;
using namespace std;
using namespace xfeatures2d;
int main(int argc, const char** argv)
{
// Read image
Mat img1 = imread("box.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread("box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE);
if (img1.empty() || img2.empty())
{
printf("Images non lisibles \n");
return -1;
}
// detecting keypoints
int hessian = 800;
Ptr<SURF> surf = SURF::create(hessian);
vector<KeyPoint> keypoints1, keypoints2;
surf->detect(img1, keypoints1);
surf->detect(img2, keypoints2);
waitKey(0);
return 0;
}

OpenCV resize is not a member of cv (OpenCV Basics)

I successfully wrote a tool that converts an image's colors space from linear to sRGB, so opencv is working. Then i wanted to rescale the image with the cv::resize function to generate Thumbnails. However it didn't work, here is the reproduced code-snippet.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// Load images in the C++ format
cv::Mat img = cv::imread("something.jpg");
cv::Mat src = cv::imread("src.jpg");
// Resize src so that is has the same size as img
**cv::resize**(src, src, img.size());
return 0;
}
I am using OpenCV 2.4.8. What am i doing wrong?
you're lacking a header file:
#include "opencv2/imgproc/imgproc.hpp"
(ofc, you have to link opencv_imgproc, too)
#include "opencv2/opencv.hpp"
would have avoided the 1st error, but you still have to care for the correct libs

OpenCV SURF extractor.compute error

I use OpenCV 2.44 and Visual Studio C++ 2010
When I compile this
#include <opencv2/imgproc/imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;
void main()
{
Mat img1 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
// detecting keypoints
FastFeatureDetector detector(15);
vector<KeyPoint> keypoints1;
detector.detect(img1, keypoints1);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1;
extractor.compute(img1, keypoints1, descriptors1);
when I run the code I get Unhandled exception at 0x580f375b in prj.exe: 0xC0000005: Access violation reading location 0x001f7014.
the error is at extractor
I'm using this tutorial link
It's looks like that you forget to init non-free module. Try to call appropriate function before using SurfDescriptorExtractor:
#include <opencv2/nonfree/nonfree.hpp>
...
cv::initModule_nonfree();

OpenCV 2.4.0 C++ goodFeaturesToTrack corrupts heap?

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.