Qt with XComposite problem - linux

I'm trying to write a simple program, which redirects all the windows to the backbuffer( as the composite manager does ), then write them to pixmap and save to disk. But I got this error:
(.text.startup+0x5e):-1: error: undefined reference to `XCompositeRedirectSubwindows'
(.text.startup+0x171):-1: error: undefined reference to `XCompositeNameWindowPixmap'
:-1: error: collect2: ld returned 1 exit status
Here is the code :
#include <QApplication>
#include <QDebug>
#include <X11/Xlib.h>
#include <QPaintDevice>
#include <QX11Info>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xdamage.h>
#include <QPixmap>
#include <QWidget>
int main( int argc, char *argv[] )
{
QApplication app( argc, argv );
app.setGraphicsSystem("native");
Picture frontBuffer;
XRenderPictFormat *format;
Window rootWindow;
int depth;
Display *dpy = XOpenDisplay( getenv("DISPLAY") );
rootWindow = XRootWindow( dpy, XDefaultScreen( dpy ) );
depth = DefaultDepth( dpy, DefaultScreen(dpy) );
// Redirect all the windows
XCompositeRedirectSubwindows( dpy, rootWindow, CompositeRedirectManual );
// Get the format
format = XRenderFindVisualFormat( dpy, DefaultVisual( dpy, DefaultScreen(dpy) ) );
XRenderPictureAttributes pa;
pa.subwindow_mode = IncludeInferiors;
// Creating front buffer
frontBuffer = XRenderCreatePicture( dpy, rootWindow, format, CPSubwindowMode, &pa );
uint nwindows;
Window root_return, parent_return, *windows;
XQueryTree( dpy, rootWindow, &root_return,
&parent_return, &windows, &nwindows );
for ( uint i = 0; i < nwindows; i++ ) {
XWindowAttributes attr;
if ( !XGetWindowAttributes( dpy, windows[i], &attr ) )
continue;
Pixmap pix = XCompositeNameWindowPixmap( dpy, windows[i] );
Picture pic = XRenderCreatePicture( dpy, pix, format, 0, 0 );
QPixmap pixmap(540, 900);
XRenderComposite( dpy, PictOpSrc, pic, None, pixmap.x11PictureHandle(),
0, 0, 0, 0, 0 , 0, 540, 900 );
pixmap.save( QString::number( i )+".png", "PNG" );
}
}
XFree( windows );
return app.exec();
}

Did you link your program with libXcomposite? That's the library which defines those functions.

Compile with -lXcomposite or with pkg-config --libs xcomposite.

Related

Memory leak in msxml6, MRE

MRE:
#include <Windows.h>
#include <MsXml2.h>
#include <atlcomcli.h>
#include <fstream>
int main() {
std::string strFileName( "xml.xml" );
std::ofstream outFile( strFileName );
outFile << "<root><n1><n2><n3><n4><n5/></n4></n3></n2></n1></root>";
outFile.close();
while ( CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ) == S_OK ) {
{
CComPtr<IXMLDOMDocument2> spIXMLDOMDocument2;
HRESULT hr = spIXMLDOMDocument2.CoCreateInstance( L"Msxml2.DOMDocument.6.0", nullptr, CLSCTX_INPROC_SERVER );
if ( hr == S_OK ) {
VARIANT_BOOL vbSuccess = VARIANT_FALSE;
CComVariant ccvarXmlSource( strFileName.c_str() );
hr = spIXMLDOMDocument2->load( ccvarXmlSource, &vbSuccess );
} else exit( 0 );
}
CoUninitialize();
Sleep( 0 );
}
}
The code is just an infinite loop of CoInitializeEx CoCreateInstance load Release and CoUninitialize, (with the help of CComPtr)
Release Build, x64, Visual Studio 2017, Windows 10 (1809).
On my laptop, this leaks several tens of KB per minute.
Process Explorer showed me private bytes at 2,5Mo at the start, and at 19Mo 3 hours later.
C:\Windows\System32\msxml6.dll version: 6.30.17763.437
UPDATE: Threaded MRE
#include <Windows.h>
#include <MsXml2.h>
#include <atlcomcli.h>
#include <fstream>
#include <thread>
void ThreadFunction( ) {
if ( CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ) != S_OK ) exit ( 0 );
{
CComPtr<IXMLDOMDocument2> spIXMLDOMDocument2;
HRESULT hr = spIXMLDOMDocument2.CoCreateInstance( L"Msxml2.DOMDocument.6.0", nullptr, CLSCTX_INPROC_SERVER );
if ( hr == S_OK ) {
VARIANT_BOOL vbSuccess = VARIANT_FALSE;
CComVariant ccvarXmlSource( "xml.xml" );
hr = spIXMLDOMDocument2->load( ccvarXmlSource, &vbSuccess );
} else exit( 0 );
}
CoUninitialize();
}
int main() {
std::ofstream outFile( "xml.xml" );
outFile << "<root><n1><n2><n3><n4><n5/></n4></n3></n2></n1></root>";
outFile.close();
while ( true ) {
std::thread oneThread( ThreadFunction );
oneThread.join();
Sleep( 0 );
}
}
Can you reproduce?

Unhandled exception , access violation in opencv cvCaptureFromCAM using Visual c++

I am trying to build a program in Opencv that captures a video and saves it to a file. However, after repeated efforts I continue to get this error : Unhandled exception at 0x201e8efb in basic.exe: 0xC0000005 Access violation, where basic.cpp is my file name.
The same occurs when trying to capture an image.
I have already tried changing cvCaptureFromCAM parameters to CV_CAP_ANY/0/-1/-2/1/2..but none of these worked out.My webcam, works perfectly well for others applications and i have tested it online.
Any help would be greatly appreciated .Thanks in advance.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "C:\opencv\build\include\opencv2\core\core.hpp"
#include "C:\opencv\build\include\opencv2\highgui\highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void main( ){
CvCapture *capture = cvCaptureFromCAM( 0 );
int width = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
int height = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
CvVideoWriter *writer = cvCreateVideoWriter( "myCamCapture.avi", -1, 30, cvSize( width, height ) );
cvNamedWindow("camopen", CV_WINDOW_AUTOSIZE);
IplImage *frame = 0;
while( 1 )
{
frame = cvQueryFrame( capture );
cvShowImage("d",frame);
cvWriteFrame( writer, frame );
char c = cvWaitKey( 30 );
if( c == 27 ) break;
}
}
please, since you're a beginner, don't start with the deprecated c-api, use the c++ one instead.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
while( cap.isOpened() )
{
Mat frame;
if ( ! cap.read(frame) )
break;
imshow("lalala",frame);
int k = waitKey(10);
if ( k==27 )
break;
}
return 0;
}
regarding your error: please triple-check the libs you're linking (compiler version, debug/release, 32/64bit). you're not allowed to mix different settings there

Open CV : IplImage is undefined

I am very new in open cv. I want to display a picture. Here is my code:
#include "stdafx.h"
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( "C:\Users\Cagin\Desktop\New.jpg" );
cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
cvShowImage( “Example1”, img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( “Example1” );
}
It's like doesn't recognize open cv lib. You can see below my solution window:
As I said before I am very new in open cv. Where is my mistake?
you are dealing with some legacy C code here, are you doing this on purpose? Under the latest builds, this would work for you:
using namespace cv;
int main(int argc, char** argv) {
Mat img = imread("C:\Users\Cagin\Desktop\New.jpg");
namedWindow("Example1", CV_WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
}
If this doesn't work, that means you haven't configured visual studio properly. Try following the instructions here: http://jepsonsblog.blogspot.com/2012/07/installation-guide-opencv-24-with.html

rendering shapes and text on the desktop (click through)

For Windows 7, what is the simplest way to render arbitrary shapes and text straight onto the desktop?
It must have the following properties:
1) Visible and always on top
3) Semi-transparent
2) Click through and type through, as if the objects are not there
Some notable examples range from the simple Fraps which renders framerate, to the complex Rainmeter which has tons of functionality.
EDIT0: I've looked at the Rainmeter sourcecode but I still have no idea how it renders objects...
EDIT1: Window Hud Behavior (Pass through clicks, can't be minimized) (Solutions such as this seem extremely restrictive, there must be a way to render stuff with as much freedom as Rainmeter?)
i am still working on it but here is part of it:
#include <algorithm>
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <conio.h>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <memory>
#include <cstdio>
#include <glut.h>
#include <io.h>
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace std;
#pragma comment(lib, "wininet.lib")
#pragma comment (lib, "Urlmon.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
HINSTANCE hInstance;
POINT Mouse;
HWND hwnd;
RECT rect;
HDC dc;
float Size = 100;
float angle = 0;
bool Dirty = false;
char TEX;
int posX = 0;
int posY = 0;
int storedDC;
void GetDesktopResolution(int& w, int& h){
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
w = desktop.right;
h = desktop.bottom;
}
void EX(){
delete hInstance;
delete hwnd;
exit(0);
}
void Keys(){
if (GetAsyncKeyState(VK_ESCAPE)){
exit(0);
}
if (GetAsyncKeyState(VK_LBUTTON) && GetAsyncKeyState(VK_CONTROL)){
}
}
void Draw(){
int h;
int w;
//Declair Desktop Size
GetDesktopResolution(w, h);
angle += 0.1f;
if (angle >= 2 * 3.141592f){
angle -= 2 * 3.141592f;
}
GetCursorPos(&Mouse);
if (Dirty == true){
rect = { 0, 0, w, h };
RedrawWindow(hwnd, &rect, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
posX = Mouse.x;
posY = Mouse.y;
RedrawWindow(hwnd, &rect, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
Dirty = false;
}
dc = GetDC(hwnd);
storedDC = SaveDC(dc);
//DEFAULT_CHARSET - ANSI_CHARSET
HFONT FMain = CreateFont(36, 20, -300, 0, FW_DONTCARE, FALSE, TRUE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Times New Roman"));
SetTextColor(dc, RGB(255, 255, 255));
SetBkColor(dc, RGB(0, 255, 0));
SelectObject(dc, FMain);
TextOut(dc, 15, 15, L"This is what the program does!", 30);
RedrawWindow(hwnd, &rect, NULL, RDW_NOERASE | RDW_INVALIDATE | RDW_UPDATENOW);
RestoreDC(dc, storedDC);
ReleaseDC(hwnd, dc);
}
int main(int argc, char **argv){
int h;
int w;
//Declair Desktop Size
GetDesktopResolution(w, h);
// find Program Manager
hwnd = FindWindowEx(GetDesktopWindow(), 0, L"Progman", L"Program Manager");
// find SHELLDLL_DefView
hwnd = FindWindowEx(hwnd, 0, L"SHELLDLL_DefView", 0);
// find Desktop Folder
hwnd = FindWindowEx(hwnd, 0, L"SysListView32", L"FolderView");
if (hwnd == NULL){
MessageBox(NULL, L"Could not initiate window!", L"ERROR!", MB_OK);
EX();
}
while (1){
Keys();
Draw();
}
//Remove the drawing
rect = { Mouse.x - 50, Mouse.y - 50, Mouse.x + 50, Mouse.y + 50 };
InvalidateRect(hwnd, &rect, true);
delete hInstance;
delete hwnd;
return 0;
}

after background subtraction how to detect segmented object?

I am trying for object detection using opencv 2.4.2. Here is the code for my moving foreground subtraction. Now I want to detect moving object in original frame and draw bounding box around it.
can anybody please help me? how to do that?
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "conio.h"
#include "time.h"
#include "opencv/cvaux.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
int key = 0;
CvSize imgSize;
CvCapture* capture = cvCaptureFromFile( "S:\\offline object detection database\\TwoEnterShop2cor.MPG" );
IplImage* frame = cvQueryFrame( capture );
imgSize = cvGetSize(frame);
IplImage* grayImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
IplImage* currframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
IplImage* destframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
if ( !capture )
{
fprintf( stderr, "Cannot open AVI!\n" );
return 1;
}
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );
while( key != 'y' )
{
frame = cvQueryFrame( capture );
currframe = cvCloneImage( frame );// copy frame to current
frame = cvQueryFrame( capture );// grab frame
cvAbsDiff(frame,currframe,destframe);
cvCvtColor(destframe,grayImage,CV_RGB2GRAY);
cvSmooth(grayImage,grayImage,CV_MEDIAN,3,3,0);
cvAdaptiveThreshold(grayImage,grayImage,230,CV_THRESH_BINARY,CV_ADAPTIVE_THRESH_GAUSSIAN_C,3,5);
cvDilate(grayImage, grayImage, 0,1);
cvErode(grayImage,grayImage, 0, 0);
if(key==27 )break;
cvShowImage( "fram",currframe);
cvShowImage( "dest",grayImage);
key = cvWaitKey( 100 );
}
cvDestroyWindow( "dest" );
cvReleaseCapture( &capture );
return 0;
}
Frame difference is the simplest method of background subtraction, but it's very sensitive to threshold you used and may not get good results.The better way is to estimate background model, compare each frame and background model to determine moving objects.
You can find further information from following links:
Background subtraction in OpenCV(C++)
Background Subtraction with OpenCV 2

Resources