OpenCV - closing the image display window - c++

I am doing on a project for searching through an image database, and when I find the results to some query - 5 database images, I would like to display the results visually. I do not keep all the images in memory, so I have do load the image first in order to display it.
I had something simple in mind, in pseudocode:
for image 1..5
load images
display image in a window
wait for any keypress
close the window
Here's a snippet of my code in C++ using OpenCV for this purpose:
IplImage *img;
for (int i=0; i < 5; ++i){
img = cvLoadImage(images[i].name.c_str(),1);
cvShowImage(("Match" + images[i].name).c_str(), img);
cvWaitKey(0);
cvDestroyWindow(("Match" + images[i].name).c_str());
// sleep(1);
cvReleaseImage(&img);
}
The images array used here does not as such exist in my code, but for the sake of the question, it contains the File Names of the images relative to the current program running point if its name member. I store the image names a bit differently in my project.
The code above almost works: I can iterate through 4/5 images OK, but when last image is displayed and a key is pressed, the image goes gray and I can not close the image window withouth crashing the rest of my application.
My first idea was that becouse of compile-time optimizations, cvReleaseImage releases the image before cvDestroyWindow is finished, and that somehow makes it freeze. But, I've tried adding some waiting time (hence the commented out sleep(1) line of my code) and it didn't help.
I am calling this display functionality from my console application, and when the image freezes, the control returns back to my application and I can keep using it (but the image window is still frozen in the background).
Can you give me any suggestions on how to fix this?
EDIT
I have talked to some people dealing with computer vision and OpenCV on a regular basis since asking the question, and still no ideas.
I have also found a similar stackoverflow question, but there is still no accepted answer. Googleing just gives similar questions as a result, but no answers.
Any ideas on what to try (even if they are not the complete solution) are very much appreciated.

For testing purposes, the application below does exactly what you stated in the question: it loads 7 images through the command line, one by one, and creates a new window for each image to be display.
It works flawlessly with OpenCV 2.3.1 on Linux.
#include <cv.h>
#include <highgui.h>
#define NUM_IMGS 7
int main(int argc, char* argv[])
{
if (argc < 8)
{
printf("Usage: %s <img1> <img2> <img3> <img4> <img5> <img6> <img7>\n", argv[0]);
return -1;
}
// Array to store pointers for the images
IplImage* images[NUM_IMGS] = { 0 };
for (int i = 0; i < NUM_IMGS; i++)
{
// load image
images[i] = cvLoadImage(argv[i+1], CV_LOAD_IMAGE_UNCHANGED);
if (!images[i])
{
printf("!!! failed to load: %s\n", argv[i+1]);
continue;
}
// display image in a window
cvNamedWindow(argv[i+1], CV_WINDOW_AUTOSIZE); // creating a new window each time
cvShowImage(argv[i+1], images[i]);
// wait for keypress
cvWaitKey(0);
// close the window
cvDestroyWindow(argv[i+1]);
cvReleaseImage(&images[i]);
}
return 0;
}

cvDestroyWindow() usually only starts pretty complicated procedure of window destruction. This procedure requires some interaction (event exchange) between windowing system and your application. Until this procedure finishes, the window cannot be completely destroyed. That is the reason why you see partially destroyed window while your application performs something not related to GUI.
Event exchange may be performed in system-dependent manner. In Windows this means (directly or indirectly) calling GetMessage or MsgWaitFor* functions and processing the result. For Unixes this means (directly or indirectly) calling XNextEvent and processing the result.
OpenCV allows to do this event exchange in system-independent way. There are two documented methods to do this. First one is cvWaitKey() (just call cvWaitKey(1) after you close the last image). Second one is to call cvStartWindowThread() at the start of your program to allow OpenCV updating its windows automatically.
Only one of these methods worked properly on my Linux box with libcv2.1: cvStartWindowThread().
Update (code snippet with cvStartWindowThread())
//gcc -std=c99 main.c -lcv -lcxcore -lhighgui
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_IMGS 2
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: %s <img1>\n", argv[0]);
return -1;
}
cvStartWindowThread();
// Array to store pointers for the images
IplImage* images[NUM_IMGS] = { 0 };
for (int i = 0; i < NUM_IMGS; i++)
{
// load image
images[i] = cvLoadImage(argv[i+1], CV_LOAD_IMAGE_UNCHANGED);
if (!images[i])
{
printf("!!! failed to load: %s\n", argv[i+1]);
continue;
}
// display image in a window
cvNamedWindow(argv[i+1], CV_WINDOW_AUTOSIZE); // creating a new window each time
cvShowImage(argv[i+1], images[i]);
// wait for keypress
cvWaitKey(0);
// close the window
cvDestroyWindow(argv[i+1]);
cvReleaseImage(&images[i]);
}
// cvWaitKey(1);
sleep(10);
return 0;
}

There is no need to destroy the window on each frame, you can simply call cvShowImage() with the same window name and it will replace the current image.
You only need to call destroy window at shutdown. You can use cvCreateWindow() to create the window at startup but it will be created automatically on the first showWindow() call.

In your code, I've seen no calls of cvNamedWindow() to create any of the windows you use to show images on (and that you destroy). You should probably put one of those calls into the loop, before you cvShowImage() (as karlphillip has shown in his answer).
If you create the named windows before the loop: Have you made sure that none of the images have duplicate names? To make sure you don't assign an image to a destroyed window, and to make sure you don't destroy a window that has already been destroyed?
Would omitting all calls of cvDestroyWindow() and using a single call of cvDestroyAllWindows() instead help to avoid your problem?

Did you test that your 5th image is correctly loaded? What about this?
for(...)
{
if(!img)
break;
// display it
}
The problem you encounter sounds like a null pointer to cvShowImage();

I love openCV but it is not the only way to display the search result from your test query. You could write a html file from your c++-code in the root of your image folder and open it in a browser.
If you are running a webserver you could write a simple file list and publish it with a simple php-script or similar.
The resulting html-code would be something like:
<!DOCTYPE html>
<html>
<head>
<style>img {display:block}</style>
<meta http-equiv="refresh" content="5">
</head>
<body>
<img src="subfolderA/img1.png" />
<img src="subfolderB/img2.png" />
<img src="subfolderC/img3.png" />
<img src="subfolderD/img4.png" />
<img src="subfolderE/img5.png" />
</body>
</html>
On advantage of this approach is that it can run on a headless server.
For closing the image display window look at the documentation for opencv2.3:
waitKey (especially notes about events), destroyWindow and this code example based on the image.cpp sample in openCV2.3:
#include "cv.h" // include standard OpenCV headers, same as before
#include "highgui.h"
#include <stdio.h>
#include <iostream>
using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std;
void help(){
cout <<
"\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
"Call:\n"
"./image img1.png img2.png img3.png img4.png img5.png\n" << endl;
}
int main( int argc, char** argv ){
help();
namedWindow("Peephole", CV_WINDOW_AUTOSIZE);
int i=0;
while ((argc-1) > i){
i++;
const char* imagename = argv[i];
Ptr<IplImage> iplimg = cvLoadImage(imagename); // Ptr<T> is safe ref-conting pointer class
if(iplimg.empty()){
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
Mat img(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
// between the old and the new data structures (by default, only the header
// is converted, while the data is shared)
if( !img.data ) // check if the image has been loaded properly
return -1;
// it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
// step 1) - convert the headers, data will not be copied
// this is counterpart for cvNamedWindow
imshow("Peephole", img);
waitKey();
}
destroyAllWindows();
while (1) {
waitKey(10);
}
// all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
return 0;
}

If you want to close all OpenCV image display windows
use: destroyAllWindows();

What I was looking for is a solution to check wether a key has been pressed or if the window has been closed. That way python doesn't stall:
"""Check if window closed or key pressed"""
import cv2 as cv
img = cv.imread('image.jpg')
cv.imshow("MyWindow", img)
while cv.getWindowProperty("MyWindow", cv.WND_PROP_VISIBLE) > 0:
if cv.waitKey(1000) > 0:
break

Try using
cvDestroyWindow("Match");
// sleep(1);
cvReleaseImage(&img); // outside the for loop

Related

Can't grab frames from an ip camera (rtsp) with a C++ dll using OpenCv 3.4.5

I'm building a simple application in c++ (32-bits) that uses opencv to grab frames from a rtsp camera.
The function that grabs the camera frames run in a separated thread from the main program.
I've testes this application with a mp4 video, and it works fine. I was able to grab frames and process them.
However when I use the rtsp link, although I was able to open a connection with the camera, whenever I tried to read both grab() and read() functions returns False.
First, i thought it was an issue with the rtsp link, but I made a simple Python application to test it, and it worked as well. So it was not the link.
This is the code that I'm using to grab the frames:
#ifndef _IMAGE_BUFFER_H_
#define _IMAGE_BUFFER_H_
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
.
.
.
VideoCapture capture_;
string address_;
atomic<bool> keep_alive_;
thread thread_;
int fps_;
mutex mutex_;
list<FramePair> frames_;
int Run()
{
capture_.open(address_, cv::CAP_ANY);
if (!capture_.isOpened()) {
printf("Could not open Camera feed! \n");
return -1;
}
uint64_t period = uint64_t((float(1) / float(fps_)) * float(1000));
period = period - (period / 20);
uint64_t t0 = getCurrentTimeInMilli();
while (keep_alive_) {
uint64_t difftime = getCurrentTimeInMilli() - t0;
if (difftime < period) {
uint64_t sleep_time = period - difftime;
if (sleep_time < period) {
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
}
t0 = getCurrentTimeInMilli();
CaptureFrame();
}
return 0;
}
void CaptureFrame()
{
Mat frame;
bool ret = capture_.read(frame);
if (!ret) {
printf("Error in frame reading! \n");
}
vector<uint8_t> jpeg;
cv::imencode(".jpg", frame, jpeg, vector<int>());
mutex_.lock();
frames_.push_back(FramePair(getCurrentTimeInMilli(), jpeg));
if (frames_.size() > FRAME_QUEUE_SIZE)
frames_.pop_front();
mutex_.unlock();
}
The OpenCv version that I'm using is 3.4.5.
The link : rtsp://<user>:<pass>#<ip>:<port>/media
I appreciate any help on this matter.
Edit1:
What I have tried:
I`ve tried this this, but it didn't work
Also Tried with a pre-built opencv 3.4.0 version for 64 bits and still the same
Sorry for answering my own question.
But after a lot of trial and error, and reading various SO threads regarding the issue of using cv::VideoCapture in in Windows.
I found the issue,
I was trying to statically link OpenCv in my application, and due to license issues ffmpeg cannot be compiled statically along with the application.
I solved it by, copying the opencv_ffmpegxxx.dll, which can be found in /bin/ . And pasting it in my .exe folder as suggested in here.
There might be some workaround to embed ffmpeg dll in the application as suggested in here, but i haven't tried yet. I hope someone else can benefit from this issue.
Thanks for the help.

Request image from X11 compositing WM in C or C++

I need to request and retrieve an image of a window from the X server or the WM (I believe the WM does the actual compositing). I need to be able to get an image of the window even if it is obscured by another window or located on another workspace (but still mapped). I need to use C or C++ and would like to use XCB or Xlib. I think I also have Gtk+ development tools installed but it's been a while since I used them.
I have tried using xcb_composite_name_window_pixmap() with no success (XCB errors).
I tried using the render extension to render the window to a picture which produced no errors, but I still needed to get the image loaded into my program's memory.
I tried to use xcb_get_image to get the picture which, IIRC, failed (bad resource I believe).
I tried to copy the picture to a pixmap and use xcb_get_image on that. I was able to download what appeared to be an image, but it looked more like random regions of the screen than the actual window.
In short, I'm just making guesses at this point.
I'm having a difficult time finding organized and complete documentation. The most recent publications I can find on X11, XCB, or Xlib is from 1994 (The O'Reilly book on Xlib and/or X11R6) which I doubt has much, if anything, valid on these extensions. Most of the man pages and online documentation has a lot of "TODO: explain this" and/or function descriptions like "deliver a request to the X server". Any help that anyone can provide will be of use to me.
I'm currently running compiz for my WM and emerald for window decorations and nothing else standard in terms of a "desktop environment". I'm working on some utility applications for a custom desktop that I plan to release when they are ready. I would like whatever I make to work with some other WMs but, if it requires special codepaths for each, I can add the others down the line.
EDIT: I originally had added a non-working example here, and then a stripped down working example, which has all been moved to an answer as suggested in the comments.
I now have a working example that I will post here:
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/composite.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "usage: %s windowId\n", argv[0]);
return EXIT_FAILURE;
}
xcb_window_t req_win_id = strtoul(argv[1], NULL, 0);
xcb_connection_t *connection = xcb_connect(NULL, NULL);
xcb_generic_error_t *err = NULL, *err2 = NULL;
xcb_composite_query_version_cookie_t comp_ver_cookie = xcb_composite_query_version(connection, 0, 2);
xcb_composite_query_version_reply_t *comp_ver_reply = xcb_composite_query_version_reply(connection, comp_ver_cookie, &err);
if (comp_ver_reply)
{
if (comp_ver_reply->minor_version < 2) {
fprintf(stderr, "query composite failure: server returned v%d.%d\n", comp_ver_reply->major_version, comp_ver_reply->minor_version);
free(comp_ver_reply);
return EXIT_FAILURE;
}
free(comp_ver_reply);
}
else if (err)
{
fprintf(stderr, "xcb error: %d\n", err->error_code);
free(err);
return EXIT_FAILURE;
}
const xcb_setup_t *setup = xcb_get_setup(connection);
xcb_screen_iterator_t screen_iter = xcb_setup_roots_iterator(setup);
xcb_screen_t *screen = screen_iter.data;
// request redirection of window
xcb_composite_redirect_window(connection, req_win_id, XCB_COMPOSITE_REDIRECT_AUTOMATIC);
int win_h, win_w, win_d;
xcb_get_geometry_cookie_t gg_cookie = xcb_get_geometry(connection, req_win_id);
xcb_get_geometry_reply_t *gg_reply = xcb_get_geometry_reply(connection, gg_cookie, &err);
if (gg_reply) {
win_w = gg_reply->width;
win_h = gg_reply->height;
win_d = gg_reply->depth;
free(gg_reply);
} else {
if (err) {
fprintf(stderr, "get geometry: XCB error %d\n", err->error_code);
free(err);
}
return EXIT_FAILURE;
}
// create a pixmap
xcb_pixmap_t win_pixmap = xcb_generate_id(connection);
xcb_composite_name_window_pixmap(connection, req_win_id, win_pixmap);
// get the image
xcb_get_image_cookie_t gi_cookie = xcb_get_image(connection, XCB_IMAGE_FORMAT_Z_PIXMAP, win_pixmap, 0, 0, win_w, win_h, (uint32_t)(~0UL));
xcb_get_image_reply_t *gi_reply = xcb_get_image_reply(connection, gi_cookie, &err);
if (gi_reply) {
int data_len = xcb_get_image_data_length(gi_reply);
fprintf(stderr, "data_len = %d\n", data_len);
fprintf(stderr, "visual = %u\n", gi_reply->visual);
fprintf(stderr, "depth = %u\n", gi_reply->depth);
fprintf(stderr, "size = %dx%d\n", win_w, win_h);
uint8_t *data = xcb_get_image_data(gi_reply);
fwrite(data, data_len, 1, stdout);
free(gi_reply);
}
return EXIT_SUCCESS;
}
It was a case of overdoing it. I got the idea that I should use Xrender from somewhere and started pulling tons of data from that extension that I didn't really need. But the real problem I had was that the pixmap given to xcb_composite_name_window_pixmap should not be created first, but rather I just needed the new id for it. It turns out that it is really as simple as I expected it to be. All you need is the Window ID and the size, do some checks to make sure composite is there and a good version (>=0.2 in this case), and call xcb_composite_redirect_window, xcb_composite_name_window_pixmap (with a generated pixmap id), and xcb_get_image (+reply,data_len,data) in that order, and you have the image.

cannot convert 'cv::VideoCapture' to 'CvCapture*'

I have a simple program which takes a video and plays it (though it does some image processing on the video). The video can be retrieved from a Dialog Box result, or directly by giving the path of the file. When I use cv::CvCapture capture1, I get the properties like capture1.isOpen(), capture1.get(CV_CAP_PROP_FRAME_COUNT) etc. but when I use CvCapture* capture2 I get weird errors.
I want to use cv::CvCapture capture1 because of my functions are in accordance with capture1. Or is there any way to use both types with some kind of conversion between them like type casting or something else.
Actually I had two programs, the functions of the program1 was for cv::CvCapture and the functions of the program2 was for CvCapture*. I mean the two programs read the video file in different manners.
I then merged these two programs to use some functions from program1 and some functions from program2. But I can't convert cv::CvCapture to CvCapture*.
I am using OpenCv with Qt Creator.
My code is very long to post here but I have simplified my code to make it smaller and understandable. My code may not compile correctly because I modified it to make it simpler.
Any help would be appreciated. Thanks in advance :)
void MainWindow::on_pushButton_clicked()
{
std::string fileName = QFileDialog::getOpenFileName(this,tr("Open Video"), ".",tr("Video Files (*.mp4 *.avi)")).toStdString();
cv::VideoCapture capture1(fileName); // when I use the cv::VideoCapture capture it gives an error
//error: cannot convert 'cv::VideoCapture' to 'CvCapture*' for argument '1' to 'IplImage* cvQueryFrame(CvCapture*)
//CvCapture* capture2 = cvCaptureFromCAM(-1);
// but when i use the CvCapture* capture2, it does not recognize capture2.isOpend() and capture2.get(CV_CAP_PROP_FRAME_COUNT) etc. don't work.
// Is there any way to convert VideoCapture to CvCapture*?
if (!capture.isOpened())
{
QMessageBox msgBox;
msgBox.exec(); // some messagebox message. not important actually
}
cvNamedWindow( name );
IplImage* Ximage = cvQueryFrame(capture);
if (!Ximage)
{
QMessageBox msgBox;
msgBox.exec();
}
double rate= capture.get(CV_CAP_PROP_FPS);
int frames=(int)capture.get(CV_CAP_PROP_FRAME_COUNT);
int frameno=(int)capture.get(CV_CAP_PROP_POS_FRAMES);
bool stop(false);
capture.read(imgsize);
cv::Mat out(imgsize.rows,imgsize.cols,CV_8SC1);
cv::Mat out2(imgsize.rows,imgsize.cols,CV_8SC1);
//I print the frame numbers and the total frames on a label.
ui->label_3->setText(QString::number(frameno/1000)+" / "+QString::number(frames/1000));
ui->label->setScaledContents(true);
ui->label->setPixmap(QPixmap::fromImage(img1)); // here I show the frames on a label.
}
cv::VideoCapture is from the C++ interface of OpenCV, and can be used to capture from a camera device and from a file on the disk
cv::VideoCapture capture1(fileName);
if (!capture.isOpened())
{
// failed, print error message
}
and cvCaptureFromCAM() is a function from the C interface of OpenCV that is used only to capture from a camera device:
CvCapture* capture2 = cvCaptureFromCAM(-1);
if (!capture2)
{
// failed, print error message
}
Don't mix/merge this interfaces together, pick one and stick with it.
If you want to use the C interface to capture from a video file, use cvCaptureFromFile() instead:
CvCapture* capture = cvCaptureFromFile(fileName);
if (!capture)
{
// print error, quit application
}
Check these examples:
Camera capture using the C interface
Camera capture using the C++ interface

doesn't save any image.. when saving frames of a video [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cant save image using OpenCV
I have tried following code,please see this and tell me what did i wrong.but there is not compile error.after i run the program it doesn't save any image.
#include"stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<cxcore.h>
int main(int argc, char* argv[]) {
int c=1;
IplImage* img=0;
char buffer[1000];
CvCapture* cv_cap=cvCaptureFromCAM(-1);
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
sprintf(buffer,"D:/image%u.jpg",c);
cvSaveImage(buffer,img);
c++;
if (cvWaitKey(100)== 27) break;
}
cvDestroyWindow("Video");
return 0;
}
can you tell me how to save a image .above program doesn't save any images.please give me your suggestions.thank you.
IplImage *destination = cvCreateImage(cvSize( img->width, img->height ), IPL_DEPTH_8U,1);
cvCvtColor( img, destination, CV_RGB2GRAY );
cvSaveImage( "C:/Users/SP/Desktop/sample.jpg", destination );
It converts to grey image; use it as desired.
Hope this works.
Probably a permissions problem, only admin can write to the top level folder
Try making a sub-directory and writing to eg sprintf(buffer,"D:/data/image%u.jpg",c);
I'll say this for the 100th time in these OpenCV posts: code safely by checking the return of the calls. cvSaveImage() returns 0 when it fails to save the image:
if (!(cvSaveImage(buffer,img))
{
std::cout << "!!! Failed to save image" << std::endl;
}
If you see the error message being displayed, you should check if the destination directory exists and if your user has the authorization to create files inside it.

How to check if an opencv window is closed

How do you check if an opencv window has been closed?
I would like to do:
cvNamedWindow("main", 1);
while(!cvWindowIsClosed("main"))
{
cvShowImage("main", myImage);
}
but these is no such cvWindowIsClosed(...) function!
What you are trying to do can be achieved with cvGetWindowHandle():
The function cvGetWindowHandle returns the native window handle (HWND in case of Win32 and GtkWidget in case of GTK+). [Qt Backend Only] qt-specific details: The function cvGetWindowHandle returns the native window handle inheriting from the Qt class QWidget.
The idea is to get the handle of the window and then use specific platform API functions to check if that handle is still valid.
EDIT:
Or you could use the tradicional cvWaitKey() approach:
char exit_key_press = 0;
while (exit_key_press != 'q') // or key != ESC
{
// retrieve frame
// display frame
exit_key_press = cvWaitKey(10);
}
Suppose you have only one image window open, then clicking the 'x' button at its corner causes the waitkey() function to return a -1 value.
Then check if the cvGetWindowHandle("name_of_the_window") function returns 0 or not. If it does return 0, then the window is actually closed.
I have tested it in OpenCV3.
But I am still not very clear on the reason why the waitkey() return -. I will much appreciate if anyone explains why this happens.
[I don't know if my answer to this question will be relevant or not after such a long time. But hopefully if anyone else gets stuck with the same issue (like me), this answer might help them out.]
Thanks.
You can use the cv::getWindowProperty method.
Do like that:
cv::namedWindow("main", WINDOW_AUTOSIZE);
while(1)
{
cv::imshow("main", myImage);
// add this IF.
if (cv::getWindowProperty("main", WND_PROP_AUTOSIZE) == -1)
break;
}
When the windows be closed the getWindowProperty will return -1.
This should do
#include <opencv2/opencv.hpp>
std::string mTitle = "title of my window";
while (cvGetWindowHandle(mTitle.c_str()))
{
// ...
}
In Python OpenCV version 3.4.2, Ubuntu Bionic, cv2.getWindowProperty('Main', cv2.WND_PROP_VISIBLE) returns a floating 0.0 (zero) when the window is closed and 1.0 (one) when it's open, whether see-able or not. Yes, still a 1.0 when it's minimized or behind another window or on a different desktop.
Just before the end of the main(), put the following code:
int main(int, char**){
.
.
.
bool visible = true;
while(visible){
waitKey(1000);
visible = getWindowProperty("Main",WND_PROP_VISIBLE) > 0;
}
return 0;
}