OpenCV: Background subtracting issue when one `VideoCapture` instance is used - opencv

Please have a look at the below code
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cv::VideoCapture cam1,cam2;
cam1.open(0);
//cam2.open(0);
Mat im,im2;
cam1>>im;
cam1>>im2;
while(true)
{
cam1>>im;
for(int i=0;i<15000;i++)
{
}
cam1>>im2;
Mat im3 = im2-im;
imshow("video",im3);
if(waitKey(30)>=0)
{
break;
}
}
waitKey(0);
}
I am trying to identify the difference (in other terms, motion) by subtracting the images. However what I get is a 100% blank screen. If I use 2 VideoCapture instances capture frames and load them to im and im2, then it works. But I must not use 2 VideoCapture instances, I must only use 1. what have I done wrong here?

If you compare im.data and im2.data you will find that they are pointing to the same buffer.
Change your code to this
Mat im,im2;
cam1>>im;
im = im.clone();
cam1>>im2;
When you read a frame from VideoCapture, it does not copy the data.
If you want to copy the data before it gets overwritten by the next frame you have to do it yourself.
If you have two different VideoCapture instances, you already have separate buffers so the problem does not occur.

Related

UART communication dsPIC33EP256MU810

I'm very much new to Microcontroller programming.
I'm using
MplabX v3.26 as IDE
XC16 compiler
PICKit 3
p33EP256MU810 (dspic)
for programing
I have written very simple program to blink LED and send few characters over UART, please refer to following source code:
#include <p33Exxxx.h>
#include <p33EP256MU810.h>
#include <libpic30.h>
#include <uart.h>
#include <stdlib.h>
#include <stdio.h>
#include <pps.h>
#include <xc.h>
#include <stdint.h>
// Configuration settings
_FOSC(FNOSC_FRCPLL);
_FWDT(FWDTEN_OFF);
int main()
{
//make all pins digital
PADCFG1 = 0xFFFF;
//set direction
TRISCbits.TRISC2 = 0;
//Initialze UART1
iPPSOutput(OUT_PIN_PPS_RP68, OUT_FN_PPS_U1TX);
//close UART
CloseUART1();
//open UART
OpenUART1( UART_EN & UART_IDLE_CON & UART_DIS_WAKE & UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_NO_PAR_8BIT & UART_1STOPBIT,
UART_TX_ENABLE & UART_INT_TX & UART_ADR_DETECT_DIS,
15);
while (1)
{
//turn on led
LATCbits.LATC2 = 1;
__delay32(3750000); // ~1 sec delay
//turn off led
LATCbits.LATC2 = 0;
__delay32(3750000); // ~1 sec delay
//Transmit data
while(BusyUART1()); //Wail till available
WriteUART1(0x55);
WriteUART1(0xaa);
while(BusyUART1()); //Wail till all bytes sent
}
return 0;
}
LED blinking part works perfectly, but I'M receiving garbage characters on other end of UART where I'm using serial monitor tool (X-CTU) to monitor data.
My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong.
Thank you
Clear ANSELx register to set pins digital, instead of writing to PADCFG1
Play with baud rate. Last parameter in procedure OpenUART1 specifies U1BRG value according to dsPic tool description page 147.
You can also setup baud rate manually adding U1BRG = value; right after executing OpenUART1.
What value pass to UxBRG depends on clock speed and desired baud rate, more details in manual for USART page 9. Notice, that if setup baud rate manually need also set or clear U1MODE.BRGH bit.
"My major issue is that I'm not able to calculate baudrate. Please let me know if I'm doing anything wrong."
I don't know what is desired baudrate but:
While making UxBRG calculation, please, take care to clock (oscillator) setup (since you are using _FOSCSEL(FNOSC_FRCPLL) with enabled PLL).
Issue that you've described sounds like a wrong baudrate (including oscillator) calculation, or it could be that port setup is wrong (databits, parity, stopbits).
Hope this helps...

Capturing through a single multi-head (stereo) camera using OpenCV

I have a single multi-head (stereo) usb camera that can be detected and can stream stereo videos using the "Video Capture Sources" filter in GraphEdit .
I'm trying to access both channels using OpenCV2.4.8 (on PC that has VS2010, Win7 X64) for further stereo image processing. However, I can only detect/stream single head(channel) of the camera and not both stereo heads of it. My code is set according to the related documentation notes of VideoCapture::grab/VideoCapture::retrieve and looks like the following:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat Lframe,Rframe;
namedWindow("Lframe",CV_WINDOW_AUTOSIZE);namedWindow("Rframe",CV_WINDOW_AUTOSIZE);
while(char(waitKey(1)) != 'q') {
if(cap.grab())
cap.retrieve(Lframe,0); cap.retrieve(Rframe,1);// get a new frame
imshow("Lframe",Lframe);imshow("Rframe",Rframe);
if(waitKey(30) >= 0) break;
}
return 0;
}
The problem is that the rendered channels (Lframe,Rframe) are identical no matter which Channel index is passed. Hence, only certain head is accessed & I can't get stereo streaming.
Is there a way to use "Video Capture Sources" filter directly with OpenCV?
Waiting for your assistance & Thank you in advance,

getting live webcam feed from opencv to Unity

I have a project in Unity using openCV 2.3 with Mat images, but Im stuck at finding a way to get a live feed from the webcam to a Unity 2dtexture. I've caught that you somewhat need to pass a array to the plugin dll file, which will then paste the image data into the array, but I don't know how to do that. Hope someone can help
You can extract the pointer to the data array from the Mat image and pass it to your other module:
VideoCapture cap;
Mat frame;
.....
cap >> frame;
uchar* imgData = (uchar*)(frame.data);
myBeautifulFunctionThatNeedsArray(imgData, frame.cols, frame.rows, ...);

Can I create a virtual webcam and stream data to it?

I am looking to stream a video from ffmpeg to OpenCV (a video manipulation library) and I am stumped. My idea is to create a virtual webcam device and then stream a video from ffmpeg to this device and the device will in turn stream like a regular webcam. My motivation is for OpenCV. OpenCV can read in a video stream from a webcam and go along its merry way.
But is this possible? I know there is software to create a virtual webcam, but can it accept a video stream (like from ffmpeg) and can it stream this video like a normal webcam? (I am working in a cygwin environment , if that is important)
You don't need to fool OpenCV into thinking the file is a webcam. You just need to add a delay between each frame. This code will do that:
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, const char * argv[]) {
VideoCapture cap;
cap.open("/Users/steve/Development/opencv2/opencv_extra/testdata/python/videos/bmp24.avi");
if (!cap.isOpened()) {
printf("Unable to open video file\n");
return -1;
}
Mat frame;
namedWindow("video", 1);
for(;;) {
cap >> frame;
if(!frame.data)
break;
imshow("video", frame);
if(waitKey(30) >= 0) //Show each frame for 30ms
break;
}
return 0;
}
Edit: trying to read from a file being created by ffmpeg:
for(;;) {
cap >> frame;
if(frame.data)
imshow("video", frame); //show frame if successfully loaded
if(waitKey(30) == 27) //Wait 30 ms. Quit if user presses escape
break;
}
I'm not sure how it will handle getting a partial frame at the end of the file while ffmpeg is still creating it.
Sounds like what you want is VideoCapture::Open, which can open both video devices and files.
If you're using C, the equivalents are cvCaptureFromFile and cvCaptureFromCAM.

Why does OpenCV give me a black screen?

I'm currently trying to use OpenCV (using the Processing library).
However, when I try to run any examples (either the Processing ones or the C ones included with OpenCV), I see nothing but black instead of input from the camera. The camera's LED indicator does turn on.. has anyone had the same problem? is my camera somehow incompatible with openCV? It's an Acer Crystal Eye...
Thanks,
OpenCV 2.1 still has a few problems with 64bits OS. You can read this topic on the subject.
If you're looking for working/compilable source code that shows how to use the webcam, check this out.
Let us know if it helped you.
I recently had the same problem. The OpenCV library on its own just gave me a blank screen, I had to include the videoInput library:
http://muonics.net/school/spring05/videoInput/
An example I followed was:
#include "stdafx.h"
#include "videoInput.h"
#include "cv.h"
#include "highgui.h"
int main()
{
videoInput VI;
int numDevices = VI.listDevices();
int device1= 0;
VI.setupDevice(device1);
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
IplImage* image= cvCreateImage(cvSize(width, height), 8, 3);
unsigned char* yourBuffer = new unsigned char[VI.getSize(device1)];
cvNamedWindow("test");
while(1)
{
VI.getPixels(device1, yourBuffer, false, false);
image->imageData = (char*)yourBuffer;
cvConvertImage(image, image, CV_CVTIMG_FLIP);
cvShowImage("test", image);
if(cvWaitKey(15)==27) break;
}
VI.stopDevice(device1);
cvDestroyWindow("test");
cvReleaseImage(&image);
return 0;
}
From this source: http://www.aishack.in/2010/03/capturing-images-with-directx/
I had somewhat same problem on Ubuntu. I downloaded a code from here:
http://www.rainsoft.de/projects/pwc.html
It does an extra step before starting to get frames(i think setting FPS). Worth a try, the code is easy to read and works with non-philips cams.
OpenCV only supports a limited number of types of cameras. Most likely your camera is not supported. You can look at either the source code or their web site to see which are supported.

Resources