Open cv gives a blank window with my USB camera - opencv

I was using my acer crystal eye camera until now. It was giving me the exact output as I need but when I moved into using my Logitech camera it gives me just a black window.
Nothing is wrong with my Logitech camera I am using it for skyping and even I tried the onlinemirror as well.
Can anybody help me. there were lot of solutions for this issue but nothing helped me.
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv) {
CvCapture *video = cvCaptureFromCAM(1);
IplImage * img = NULL
if(!cvGrabFrame(video)){
printf("could not grab a frame\n");
exit(0);
}
cvNamedWindow("original_image",0);
while(1){
img = cvQueryFrame(video);
cvShowImage("original_image",img);
if (cvWaitKey(0)==27){
break;
}
cvReleaseImage(&img);
cvReleaseCapture(&video);
return (EXIT_SUCCESS);
}

Try to use if (cvWaitKey(30)==27) works instead of if (cvWaitKey(0)==27)
The highgui needs some time to update the frame.

Are you sure that your usb camera has index 1?
CvCapture *video = cvCaptureFromCAM(1);
You could try to disable your acer webcam in the Device Manager (if you are using windows) and then replace the above line with:
CvCapture *video = cvCaptureFromCAM(CV_CAP_ANY);
This way, you make sure you only have 1 camera enabled(the usb camera), and by using CV_CAP_ANY you make sure that that one cam is used. If this still gives you a black/blank screen, you know something else is wrong.

Related

Is it possible to have a square resolution with a webcam video stream using OpenCV?

I wrote a simple OpenCV program that recovers my webcam video stream and display it on a simple window. I wante to resize this window to the resolution 256x256 but it changed it to 320x240.
Here's my source code :
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int main(int argc, char** argv)
{
char key;
cvNamedWindow("Camera_Output", cv::WINDOW_NORMAL);
CvCapture *capture = cvCaptureFromCAM(CV_CAP_ANY);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 256);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 256);
while(1){
IplImage *frame = cvQueryFrame(capture);
cvShowImage("Camera_Output", frame);
key = cvWaitKey(10);
if (key == 27){
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("Camera_Output");
return 0;
}
The output resolution is 320x240 and I want a 256x256 resolution. I think it's not possible because the camera manages its output video stream buffer and it has to keep the same ratio (width/height). What do you think about this idea ?
Is there a function which can force the resolution as a square resolution using OpenCV ?
Thanks a lot in advance for your help.
Seems like you video source does not handle 256x256 resolution. If you want to display it as such, you will have to crop the image yourself before displaying it.
Simple, you can do this by:
VideoCapture cap;
cap.open(0); // open your web-camera
cap.set(CV_CAP_PROP_FRAME_WIDTH, 256);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 256);
If this doesn't work, you need to resize it manually by calling cv::resize().

Why would my OpenCV program on BeagleBone Black capture images like this?

Here is a link to the type of images my program produces: http://imgur.com/a/vibBx#0
I am trying out a simple capture test program that I have written. I am trying to capture images in a loop and save them onto the board properly numbered. The first capture sometimes is corrupted and the subsequent captures are a mix of two images. I have also observed that sometimes the upper half of the image is from the previous capture and the lower half is the capture from that cycle. I have given the details and the code below.
OpenCV 2.4.2 running on BeagleBone Black which has Ångström installed on it.
The camera which is plugged to the USB of BeagleBone Black is Logitech C920.
The camera is connected to BeagleBone Black before power up through the 5 V power supply and connecting BeagleBone Black to the laptop. Access is through PuTTY.
Code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cxcore.h>
#include "LaserWeed.h"
#include "SimpleGPIO.h"
using namespace cv;
int main(int argc, char* argv[])
{
int i=0, a;
string name;
int length;
char c, Filename[10000];
CvCapture* capture = NULL;
IplImage* img = NULL;
do
{
//I am not sure if this is necessary, but I tried anyway
//to see if it makes a difference since upper half of
//image was from previous cycle.
capture = NULL;
img = NULL;
//Converting Numbers to string to save with proper name
std::stringstream ss;
ss << i;
name = ss.str();
name = name + ".jpg";
length = name.size();
for(a=0; a<length; a++)
{
Filename[a] = name[a];
}
capture = cvCaptureFromCAM(-1);
if (!capture)
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
img = cvQueryFrame(capture);
if (!img)
{
fprintf(stderr, "ERROR: img is null...\n");
getchar();
return -1;
}
cvSaveImage(Filename,img);
cvReleaseCapture(&capture);
cvReleaseImage(&img);
i++;
c = getchar();
}
while (c!='e')
;
return 0;
}
Where might I be going wrong?
The Stack Overflow question BeagleBone, OpenCV and webcam issue somewhat has a similar problem. But reinstallation of the OS will be my last option.
This is weird, anyway try to take the capture out of the do-while loop, maybe opening and releasing the capture device each time won't give enough time for the camera to prepare it's image buffers.

How can I use tesseract and opencv to extract the text from the camera

I'm using tesseract 3.02 and opencv to let the tesseract recognize the text from my camera realtime.
But the effect is quite bad. Results are unreadable and can't show image fluently. I think it's the problem of my code.
Can some one give me advice about how to modify it?
Thanks a lot!
#include "stdafx.h"
#include <string>
#include <opencv2/opencv.hpp>
#include <time.h>
using namespace std;
using namespace cv;
int main() {
// [1]
tesseract::TessBaseAPI *myOCR =
new tesseract::TessBaseAPI();
// [2]
printf("Tesseract-ocr version: %s\n",
myOCR->Version());
printf("Leptonica version: %s\n",
getLeptonicaVersion());
// [3]
if (myOCR->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
//声明IplImage指针
IplImage* pFrame = NULL;
//获取摄像头
CvCapture* pCapture = cvCreateCameraCapture(-1);
//创建窗口
cvNamedWindow("video", 1);
//显示视屏
time_t last_time = time(NULL);
while(1)
{
pFrame=cvQueryFrame( pCapture );
if(!pFrame) break;
cvShowImage("video",pFrame);
char c=cvWaitKey(33);
if(c==27)break;
time_t this_time = time(NULL);
if(this_time != last_time)
{
last_time = this_time;
myOCR->SetRectangle(0,0,pFrame->width,pFrame->height);
myOCR->SetImage((uchar*)pFrame->imageData,pFrame->width,pFrame- >height,pFrame->depth/8,pFrame->width*(pFrame->depth/8));
myOCR->Recognize(NULL);
const char* out = myOCR->GetUTF8Text();
printf("%s\n",out);
}
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("video");
cv::waitKey(-1);
return 0;
}
Tesseract was designed to process scanned books. It operates on white pages where there is only black text, clearly seen with minimal distortions. Images are mostly Black & White. Your image is grey level so Tesseract will perform very very poor.
It is not a problem of your code but of Tesseract.
If you point your camera towards a book, you will be able to get the text (assuming image is focused) but if you want to read general text (like street signs, logo on someones T-shirt than there is no way to do it. Sorry to disappoint you.
However, if you want to recognize a specific text, like credit card numbers or street signs,
that you can do it.
Start by grabbing many imgages of your text.
Do a bit of
pre-processing on the image, convert it to BW,
train Tesseract on many examples.
And then it will be able to accomplish your task.

Webcam is not being detected in opencv

I am using opencv 2.3.1a in ubuntu 11.o4.
I installed opencv using: http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/
Since I could not use webcam with that installation. I changed "cmake" option from WITH_V4L=OFF to WITH_V4L=ON. But it still does not work.
I tried the following code:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( 0);
if(!capture)
{
printf("no camera");
return -1;
}
}
And yeah the output is : no camera
I do not know where the problem is.
I followed the steps mentioned in:
http://www.ozbotz.org/opencv-installation/
This solved the problem.

Can't display an image (Win7 x64 Opencv 2.3)

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
int main()
{
cv::Mat im =cv::imread("C:/OpenCV2.3/opencv/samples/cpp/matching_to_many_images/query.png");
if(im.empty())
{
return -1;
}
cv::namedWindow("image", CV_WINDOW_AUTOSIZE);
cv::imshow("image" , im);
cv::waitKey();
return 0;
}
After executing this code sample , i have gray window. Whean i move a cursor on the window, it shows , that something is loading. What's the problem? I'am sure that the imagepath is correct.
I had the same error. Turned out my system missed the msvcp100d.dll and msvcr100d.dll files.

Resources