OpenCV cv::imshow() does not work but cvShowImage() does - c++

I'm having an issue that I've never had with OpenCV before. When I use cv::imshow() the image is shown perfectly, but there are weird ascii characters along the top bar instead of the window name and every few seconds a new window appears with a different name and the old freezes on whatever frame it was currently on. I can exit out of the windows that are frozen but more appear instantly after with different names. I have used OpenCV before and imshow() has always worked for me.
Here's my code:
#include "stdafx.h"
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
Mat frame;
namedWindow("Sample Program", CV_WINDOW_AUTOSIZE);
while (true)
{
cap >> frame;
imshow("Sample Program", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
If I replace imshow() with
cvShowImage("Sample Program", cvCloneImage(&(IplImage)frame));
It works perfectly.
I just want to know if theres a library issue or something that is causing this issue
I'm using OpenCV 2.4.9 in Visual Studio Ultimate 2012

using namespace std;
using namespace cv;
With that cv it works for me.

Related

problem on displaying live video in qt c++ application

I want to show a live stream of the camera connected to raspberry in qt application (OS Linux). After googling it, I found out I must display the video inside QLabel. When displaying an image there's no problem and everything works fine, but when I want to display the live stream inside QLabel, the live stream window opens separately (not inside QLabel). would you tell me how to solve this problem? here's my code :
void Dialog::on_Preview_clicked()
{
command = "raspistill";
args<<"-o"<<"/home/pi/Pictures/Preview/"+Date1.currentDateTime().toString()+".jpg"<<"-t"<<QString::number(20000);
Pic.start(command,args,QIODevice::ReadOnly);
QPixmap pix("//home//pi//Pictures//Preview//test.jpg");
ui->label_2->setPixmap(pix);
ui->label_2->setScaledContents(true);
}
This code opens video capturing screen and captures an image after 20 seconds. the only problem is that the capture screen (which could be used as a live stream). isn't being displayed inside the "Lable_2". Is there anyway to do this without using OpenCV library? If not, tell me how to do it using OpenCV.
Thanks
It is pretty simple in opencv
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("edges", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
Stream the camera using OpenCV, and show it in QLabel is possible.
When QCamera not working, and also use OpenCV in the project, could use VideoCapture to stream the video instead of QCamera.
The problem can be decomposed into several steps. Basically, We need:
Create a QThread for streaming(Don't let the GUI thread blocked).
In the sub-thread, using cv::VideoCapture to capture the frame into a cv::Mat.
Convert the cv::Mat to QImage(how to convert an opencv cv::Mat to qimage).
Pass QImage frame from sub-thread to the main GUI thread.
Paint the QImage on the QLabel.
I put the complete demo code in Github. it could paint the frame on the QLabel and QML VideoOutput.

Opencv 3.2 read mpeg files too slow

I am going through the book Learning OpenCV 3 and test out the video example 2.3. I could edit, compile and run it, but the problem is that it closed down immediately.
// DisplayPicture.cpp : Defines the entry point for the console application.
//
//#include "opencv2/opencv.hpp" // Include file for every supported OpenCV function
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <opencv2/videoio.hpp>
#include <stdio.h>
#include <string.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
namedWindow("video3", WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open( string(argv[1]));
int tell = 0;
Mat frame;
for (;;) {
cap >> frame;
//waitKey(30);
if (frame.empty())
{
break;
//end of film
}
imshow("video3", frame);
}
return 0;
}
I found that my computer processed the data too fast. It could not read the next frame fast enough. if (frame.empty()) became true the program reached the break statement and ended.
By adding a waitkey of 30 millisec before viewing the image frame, the video program works very well. At least I can view the video. Since this example is from the 'bible' it should work, but not with my computer.
I am running a MSI gt72 2PE computer with nvidia gtx880m. Not sure if that matters.
I assume that adding a waitKey(30) is not appropriate, so I am seeking suggestions as to what could be done differently.

image appears green after capture OpenCV

I have this code which captures image from Webcam using OpenCV:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main( )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat meter_image;
cap >> meter_image;
imwrite("/boneCV-master/img.jpg", meter_image);
return 0;
}
I get following image as output.
Previously it was working fine. I don't know what is happening. I tried simplest of all codes upon googling but nothing worked. Please let me know what could be wrong with it.
thanks in advance.
EDIT
One thing I forgot to mention is that i am working on beagleBone Black. this same piece of codes works great with my mac.
Maybe adding frame checking will be helpful.
Mat meter_image;
while(meter_image.empty()){
cap >> meter_image;
}
But there's a risk of infinite loop.

opencv imshow show gray in Opencv 3.1

Opencv imshow showing gray image.
#include "stdafx.h"
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap;
Mat frame;
cap.open(0);
while (true)
{
cap >> frame;
imshow("test", frame);
waitKey(30);
}
return 0;
}
I recently upgraded to windows insider preview 14316 and it stopped working after that i think.
I checked if the camera work in skype, and it did. I also tried the code on another computer and it worked.
Any ideas?

Capture image with OpenCV without GUI(on console linux)

I am using console linux and I have a camera capture application. I need to capture an image without GUI(The camera should start and capture some images, save it to disk and close). The following code works well on my laptop but doesn't start on console. Any suggestions?
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
Mat frame;
namedWindow("feed",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("feed", frame);
imwrite("/home/zaif/output.png", frame);
if(waitKey(1) >= 0) break;
}
return 0;
}
After the release of OpenCV 2.4.6 there were bug fixes for video capture on Linux. Go straight to 2.4.6.2 and you should get the fixes. Specifically, this revision is probably the relevant fix for you, although there were a number of other revisions pertaining to video capture on android that might effect Linux compilation too.