OpenCV Capture from Camera Results in Noisy-like Image - opencv

I am using OpenCV on an embedded target board (FriendlyARM mini6410, processor arm 1176 running linux kernel 2.6.38).
I compiled OpenCV 2.4.4 library using toolchain provided for the board, found in the ftp (pls see the website of FriendlyARM). I disabled GTK, ffmpeg and enable v4l. The library is compiled successfully.
Then I write code:
#include <opencv.hpp>
#include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main()
{
int i;
cout << "initialise" << endl;
IplImage* img=0;
cout << "capturing ..." << endl;
CvCapture* capture = cvCaptureFromCAM(2);
cout << "get here" << endl;
if(!capture){
cout << "not capture" << endl;
return -1;
}
cout << "captured" << endl;
img=cvQueryFrame(capture);
IplImage* img1 = cvCreateImage(cvGetSize(img),8,3);
// cvCvtColor(img,img1,CV_RGB2GRAY);
cvCopy(img, img1);
cvSaveImage("cam_snap.jpg",img1);
cvReleaseImage( &img1 );
cvReleaseImage( &img );
cvReleaseCapture( &capture );
cout << "exit" << endl;
return 0;
}
The code is built successfully. I run the .elf executable in the target board, connected to camera (PS3 eye), but the resulting image looks like a broken television (noise-like):
While in my host, the resulting image is as expected (scene in front of camera). Can you provide me suggestion as to what went wrong or where should I start on debugging?

You should check your depth and channels. It is probably a matter of alignement, moreover be careful your image is probably in BGR and not in RGB.
And you should use cv::Mat instead of IplImage in C++ and VideoCapture instead of CVCapture.
This sample of code should work. (Not tested on the same arch as your)
#include <opencv.hpp>
#include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture = cv::VideoCapture(0);
cout << "get here" << endl;
if(!capture.isOpened()) // check if we succeeded
return -1;
cout << "captured" << endl;
Mat img;
capture >> img;
imwrite("./test.png", img);
capture.release();
cout << "exit" << endl;
return 0;
}
Hope it helped.

Okay, confirmed. mini 6410 runs USB 1.0, and ps3 eye needs USB 2.0. I tried the program using standard webcam (chinese product, itech pc camera), works wonderfully. Saved image is showing scene in front of camera

Related

How to convert Opencv Mat frame into a VP8 frame

My problem is this:
I need to get the webcam video, get each frame of the video as VP8 frames to packetize it on a RTP stream or to directly get/create an RTP stream to send through a WebRTC application.
I saw that I can get the camera via OpenCV this is an complete example:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void drawText(Mat & image);
int main()
{
cout << "Built with OpenCV " << CV_VERSION << endl;
Mat image;
VideoCapture capture;
capture.open(0); // webcam device
if(capture.isOpened())
{
cout << "Capture is opened" << endl;
for(;;)
{
capture >> image;
if(image.empty())
break;
drawText(image);
imshow("Sample", image);
if(waitKey(10) >= 0)
break;
}
}
else
{
cout << "No capture" << endl;
image = Mat::zeros(480, 640, CV_8UC1);
drawText(image);
imshow("Sample", image);
waitKey(0);
}
return 0;
}
void drawText(Mat & image)
{
putText(image, "Hello OpenCV",
Point(20, 50),
FONT_HERSHEY_COMPLEX, 1, // font face and scale
Scalar(255, 255, 255), // white
1, LINE_AA); // line thickness and type
}
I have "Mat image", so I can the the Mat (each video frame into a OpenCV Mat) on the line:
capture >> image;
is it possible to get the bytes from the Mat class and encode it into a VP8 frame?
I saw there is a VideoWriter class on OpenCV where I can setup the VP8 encoding, but id like not to save anything on disk since I only need the VP8 bytes to send over a stream.
Is it possible to get the bytes from the camera and encode it to a VP8 frame or even to get the whole camera recording and setup a RTP stream with OpenCV?

how to reduce motion blur in picture?

I am trying to detect the QR data from a blurry image and have not been successful till now.
I have tried a couple of morphology operations on the image and still did not get the data embedded in it.
How could I improve the situation?
I have tried this so far:
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/barcode.hpp>
int main() {
cv::Mat imageMat = cv::imread("/Users/apple/Downloads/36.jpg");
if(imageMat.empty()) {
std::cout << "Image not present and can not be opened" << std::endl;
return 0;
}
cv::Mat imageGray, imageBlur, imageCanny, imageDilated, imageEroded, thresholdImage;
cv::cvtColor(imageMat, imageGray, cv::COLOR_BGR2GRAY);
cv::GaussianBlur(imageGray, imageBlur, cv::Size(3,3), 3, 0);
std::cout << "Gaussian blur done" << std::endl;
// cv::Canny(imageBlur, imageCanny, 25, 75);
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3,3));
cv::dilate(imageBlur, imageDilated, kernel);
cv::erode(imageDilated, imageEroded, kernel);
cv::threshold(imageEroded, thresholdImage, 5, 255, cv::THRESH_BINARY+cv::THRESH_OTSU);
std::cout << "Threshold done" << std::endl;
// Reads the barcode/dat
cv::QRCodeDetector qrDecoder;
std::string decodedData = qrDecoder.detectAndDecode(thresholdImage);
std::cout << "Decoded data = " << decodedData << std::endl;
This code is not decoding the data from the image which has blurry QR code. What would you guys prefer to unblur the image and get the data embedded in it? Increase the contrast? Increase the brightness?
Any document can be helpful too.
Thank you in advance.

Frames Lost while processing video with opencv

I am capturing a video with 30fps, but when I process the video with openCV for AruCo marker detection I am loosing almost half of the frames. So for a 5 min video I expect 5x60x30 = 9000 frames but I am getting only about 4500 frames. I tried different resolution and fps while recording but the problem still persists. My code is as follows. I later want to sync the video with the audio recorded from the camera, so even if I could know the frames which are being lost can solve my problem. Any pointers or suggestions are welcome.
#include "opencv2\core.hpp"
#include "opencv2\imgcodecs.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\aruco.hpp"
#include "opencv2\calib3d.hpp"
#include <time.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include "stdafx.h"
using namespace std;
using namespace cv;
#define _CRT_SECURE_NO_WARNINGS 1
int startWebcamMonitoring() //(const Mat& cameraMatrix, const Mat&
distanceCoefficients, float arucoSquareDimensions)
{
Mat frame4;
Scalar_<double> borderColor, borderColor2, borderColor3;
vector<int> markerIds;
vector < vector<Point2f>> markerCorners, rejectedCandidates ;
aruco::DetectorParameters parameters;
Ptr<aruco::Dictionary> makerDiktionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
VideoCapture cap("sample.mp4");
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frames per second : " << fps << endl;
while (true)
{
cap >> sample;
if (!cap.read(frame4))
break;
aruco::detectMarkers(frame4, makerDiktionary, markerCorners, markerIds);
aruco::drawDetectedMarkers(frame4, markerCorners, markerIds, borderColor);
aruco::estimatePoseSingleMarkers(markerCorners, arucoSquareDimension,
cameraMatrix, distanceCoefficients, rotationVectors, translationVectors);
Mat rotationMatrix
for (int i = 0; i < markerIds.size(); i++)
{
aruco::drawAxis(frame4, cameraMatrix, distanceCoefficients, rotationVectors[i], translationVectors[i], 0.01f);
Rodrigues(rotationVectors[i], rotationMatrix);
time_t current = time(0);
cout << " Translation " << translationVectors[i] << " ID" << markerIds[i] << " Euler angles " << 180 / 3.1415*rotationMatrixToEulerAngles(rotationMatrix) << "current time " << ctime(&current) << endl;
}
freopen("output_sample", "a", stdout);
imshow("recording", frame4);
if (waitKey(30) >= 0) break;
}
The problem is:
cap >> sample;
if (!cap.read(frame4))
break;
the program is reading a frame from source twice in every iteration.
You should remove the cap >> sample; line and it will be fine.

Grabbing Pixel data from a video strem

I am having trouble if understanding certain coding i am sorry if this comes off as stupid but i have a code to capture a video from my webcam i want to get the RGB valuee from the frame, if this is impossible would have to to save a frame as a picture and then get values from it?
const char window_name[]="Webcam";
int main(int argc, char* argv[])
{
/* attempt to capture from any connected device */
CvCapture *capture=cvCaptureFromCAM(CV_CAP_ANY);
if(!capture)
{
printf("Failed to initialise webcam\n");
return -1;
}
/* create the output window */
cvNamedWindow(window_name, CV_WINDOW_NORMAL);
do
{
/* attempt to grab a frame */
IplImage *frame=cvQueryFrame(capture);
if(!frame)
{
printf("Failed to get frame\n");
break;
}
COLORREF myColAbsolute = GetPixel(frame, 10,10);//error in saying frame is not compatible with HDC.
cout << "Red - " << (int)GetRValue(myColAbsolute) << endl;
cout << "Green - " << (int)GetGValue(myColAbsolute) << endl;
cout << "Blue - " << (int)GetBValue(myColAbsolute) << endl;
/* show the frame */
cvShowImage(window_name, frame);
ha ! ( obviously caught with a copy & paste bummer )
GetPixel() is a windows function, not an opencv one. same for GetRValue() and sisters.
you'd use them in the native win32 api, to get a pixel from an HDC, but it won't work with opencv/highgui, since neither HDC, nor HWND are exposed.
since you're obviously a beginner(nothing wrong with that, again!) let me try to talk you out of using the old, 1.0 opencv api(IplImages, cv*Functions) as well,
you should be using the new one(cv::Mat, namespace cv::Functions) instead.
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat frame;
namedWindow("video", 1);
VideoCapture cap(0);
while ( cap.isOpened() )
{
cap >> frame;
if(frame.empty()) break;
int x=3, y=5;
// Ladies and Gentlemen, the PIXEL!
Vec3b pixel = frame.at<Vec3b>(y,x); // row,col, not x,y!
cerr << "b:" << int(pixel[0]) << " g:" << int(pixel[1]) << " r:" << int(pixel[2]) << endl;
imshow("video", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}

How to initialize multiple OpenNI sensors with OpenCV

I'd like to use two Asus Xtion Pro sensors with OpenCV (2.4.4) and not sure how to initialize both devices.
I can initialize one like so:
VideoCapture capture;
capture.open(CV_CAP_OPENNI);
How can I initialize two VideoCapture instances for two separate sensors ?
Turns out it's as simple as this:
VideoCapture sensor1;sensor1.open(CV_CAP_OPENNI_ASUS);
VideoCapture sensor2;sensor2.open(CV_CAP_OPENNI_ASUS+1);
A very basic runnable example is:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(){
cout << "opening device(s)" << endl;
VideoCapture sensor1;sensor1.open(CV_CAP_OPENNI_ASUS);
VideoCapture sensor2;sensor2.open(CV_CAP_OPENNI_ASUS+1);
if( !sensor1.isOpened() ){
cout << "Can not open capture object 1." << endl;
return -1;
}
for(;;){
Mat depth1,depth2;
if( !sensor1.grab() ){
cout << "Sensor1 can not grab images." << endl;
return -1;
}else if( sensor1.retrieve( depth1, CV_CAP_OPENNI_DEPTH_MAP ) ) imshow("depth1",depth1);
if( !sensor2.grab() ){
cout << "Sensor2 can not grab images." << endl;
return -1;
}else if( sensor2.retrieve( depth2, CV_CAP_OPENNI_DEPTH_MAP ) ) imshow("depth2",depth2);
if( waitKey( 30 ) == 27 ) break;
}
}

Resources