Matlab Capture Screen Example Temporary

Source: Internet
Author: User

Absrtact: Combining how to implement MATLAB screen grab function with Window API, this paper introduces how to use the interface of MATLAB and C + + in MATLAB to invoke operating system API and extend MATLAB function.

Introduction

The MATLAB product family is an ideal integrated environment developed by the American MathWorks Company for conceptual design, algorithm development, modeling and simulation, real-time implementation. Due to its complete professional system and advanced design and development ideas, MATLAB in a variety of fields have a wide range of application space.

Although MATLAB is very widely used in scientific research and industrial technology development, it is not omnipotent. In some cases, MATLAB itself carries functions and their combinations do not fully meet the requirements of the user, but must be done by invoking the operating system's API functions. MATLAB has taken this into account at design time, providing us with a MEX command that can be used to compile C programs that invoke operating system API functions into DLL files, which are MEX files, making them an extended function of MATLAB. In this way, we can directly invoke the extension function when we program in MATLAB environment, to achieve the purpose of indirectly invoking the operating system API function. This article demonstrates the process by writing a screen capture function for MATLAB.

Mex interface

MEX is the abbreviation for MATLAB executable, that is, it can be executed in MATLAB. This is the interface of MATLAB and other major programming languages such as C/c++,fortran. The ordinary C + + or FORTRAN source program, just add a special interface function, can be compiled by the MEX command in MATLAB into a special dynamic link library function, and this function can be programmed in the MATLAB environment directly called, and MATLAB embedded functions. This special interface function is equivalent to the main function in the C program, which is the entry of the program, and the execution of the program begins with the entry function. Its prototype is

void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const mxarray *prhs[])

Where the parameters nlhs and NRHS are the number of output and input variables, the parameters PLHS and PRHS are pointers to the output and input variable pointers, PRHS is an array of pointers to the input variables of length NRHS, and PLHS is an array of pointers to the output variables of the NLHS length.

Using the window API to implement MATLAB screen fetching function

In essence, we use the C language to invoke window API functions to realize the screen fetching function, meanwhile, we must handle the problems of C and MATLAB interface and the transformation of array storage in C language and array storage in MATLAB.

The following is the C source program, with detailed comments.

Matlab Mex file to snap the screen
Compile to Use>> Mex screensnap.c User32.lib gdi32.lib
Usage:1, >>a=screensnap (0)%exclude the MATLAB window
>>imshow (a);
2, >>a=screensnap (1); %include the Matlab window
>> Imshow (a);
Designed by Darnshong [email protected]
2005,12,18
#include <windows.h>
#include <string.h>
#include "mex.h"
void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const mxarray *prhs[])
{
int cx,cy,recnum;
int dims[3],i,j,k;
Char *pchar,*mloc;
BOOL bshowmatlab;//whether to block MATLAB main window when grasping screen
HWND hwin,hactw;
HDC DC,MEMDC;
Rect rect;
Hbitmap Hbitm,hold;
Bitmapinfoheader Binfoh;
if (nrhs!=1)//Check the input parameters
Mexerrmsgtxt ("Need 1 argument!
");
if (!mxisdouble (Prhs[0]))
Mexerrmsgtxt ("the input argument must be a numeric!
");
if (* (double*) (Mxgetdata (prhs[0)) ==0)
Bshowmatlab=false;
Else
Bshowmatlab=true;
Hactw=getforegroundwindow ();//Get the window handle of MATLAB
Hwin=getdesktopwindow ();//Get Desktop Window handle
DC=GETWINDOWDC (Hwin);//Get Desktop window DC
GetWindowRect (hwin,&rect);//Get Desktop window size
Cx=rect.right-rect.left;
Cy=rect.bottom-rect.top;
mexprintf ("CX:%d cy:%d
! ", Cx,cy);
Memdc=createcompatibledc (DC);//Create a memory DC that is compatible with the desktop window DC
mexprintf ("Handles:%d%d%d
! ", HWIN,DC,MEMDC);
Hbitm=createcompatiblebitmap (dc,cx,cy);//Create an appropriate bitmap
if (hbitm==0)
Mexerrmsgtxt ("Fail to create a compatible bitmap!
");
if (! ( Hold=selectobject (MEMDC, HBITM))//Select the newly created bitmap into the memory DC
Mexerrmsgtxt ("Compatible Bitmap selection!
");
Hide the application window.
if (!bshowmatlab)
{
ShowWindow (HACTW, sw_hide); Block MATLAB main window
Sleep (100);//delay 100ms, because in the process of shielding MATLAB main window can not capture the screen
}
Copy color data for the entire display into a
Bitmap that's selected into a compatible DC.
if (! BitBlt (memdc,0,0,cx,cy,dc,0,0,srccopy))//Copy the Desktop window DC to the memory DC
Mexerrmsgtxt ("screen to Compat Blt Failed");
  
dims[0]=cy;//Note the order of Cx,cy
DIMS[1]=CX;
dims[2]=3;
Plhs[0]=mxcreatenumericarray (3,dims,mxuint8_class,mxreal);
Create output variable space to pass image data
Because it is a color image, it contains three components of RGB, so it is three-dimensional data
Pchar= (char*) mxgetdata (Plhs[0]);
Binfoh.bisize=sizeof (Bitmapinfoheader);
BINFOH.BIWIDTH=CX;
Binfoh.biheight=-cy;
Binfoh.biplanes=1;
binfoh.bibitcount=24;
Binfoh.bicompression=bi_rgb;
binfoh.bisizeimage=0;
binfoh.bixpelspermeter=0;
Binfoh.biypelspermeter = 0;
Binfoh.biclrused=0;
binfoh.biclrimportant=0;
Configuring the bitmap information header structure
Mloc= (char*) Mxmalloc (cx*cy*3);
Application space to store image data Recnum=getdibits (Memdc,hbitm,0,cy,mloc, (bitmapinfo*) &binfoh,dib_rgb_colors);
Copy the image data into the Mloc space
mexprintf ("Copyed%d lines%d
! ", RECNUM,HBITM);
for (k=0;k<3;k++)
for (j=0;j<cy;j++)
for (i=0;i<cx;i++)
{
pchar[i*cy+j+k*cx*cy]=mloc[(J*cx+i) *3+2-k];
Since data storage in MATLAB is followed by column, it is not the same as the first column in C program
Image data must be handled appropriately when copying to an output variable
}
Mxfree (Mloc);//release The requested memory space
SelectObject (Hbitm,hold);
DeleteDC (MEMDC);
ReleaseDC (HWIN,DC);
DeleteObject (HBITM);
Perform necessary processing to prevent memory leaks after completion
if (!bshowmatlab)
ShowWindow (HACTW, sw_show);
Restore the display of the MATLAB main window
}
The function that begins with Mx,mex in the above program is the function that MATLAB provides for the development of the convenient MEX file, there is a declaration in the mex.h file, the MATLAB Help system has the detailed explanation.


Run results

Save the above source program as a screensnap.c file and run it in the MATLAB command window:
Mex SCREENSNAP.C User32.lib Gdi32.lib

You can generate a Screensnap.dll dynamic link library file, and then the directory where the file resides in the search directory of MATLAB, run the following command in the MATLAB command window:

Dat=screensnap (0);% shielded Matlab window, if not shielded, available dat=screensnap (1);
Imshow (DAT);% display image
You can get the following results

  

Summary

Through the above example can be seen through the interface between MATLAB and C, we can take full advantage of the C language, according to their own needs, the use of C language Call operating system API programming, expand and enrich MATLAB functions, better play matlab function.

Matlab Capture Screen Example Temporary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.