cvShowImage for displaying directly to any window (Feature #1607)


Added by Jonatan Vargas about 13 years ago. Updated over 12 years ago.


Status:Done Start date:2012-02-18
Priority:Normal Due date:
Assignee:Alexander Reshetnikov % Done:

0%

Category:highgui-gui
Target version:2.4.0
Difficulty: Pull request:

Description

Hi everybody here,
I was developing one DLL involving in image processing for some Win32 applications. It was quite hard trying to display my images directly to a desired window or control. And I found a lot of people having the same troubles. Finally I decided to investigate a little bit through de Highgui code, and decided to write a modification for cvShowImage based on the basic one, but passing to the function the HWND handler of the control you want to display your images rescaled. If it's for your interest, here is the code:

 1CV_IMPL void cvShowImage2( HWND w_hWnd, const CvArr* arr )
 2{
 3    CV_FUNCNAME( "cvShowImage2" );
 4
 5    __BEGIN__;
 6
 7    SIZE size = { 0, 0 };
 8    int channels = 0;
 9    void* dst_ptr = 0;
10    const int channels0 = 3;
11    int origin = 0;
12    CvMat stub, dst, *image;
13    bool changed_size = false;
14    BITMAPINFO* tempbinfo = NULL;
15    HDC hdc = NULL;
16
17    // Ensure we have valid image & HWND
18    if( !arr )
19        return;
20    if( !w_hWnd )
21        return;
22
23    hdc = ::GetDC(w_hWnd);
24
25    if( CV_IS_IMAGE_HDR( arr ))
26        origin = ((IplImage*)arr)->origin;
27
28    CV_CALL( image = cvGetMat( arr, &stub ));
29
30    if ( hdc )
31    {
32        //GetBitmapData
33        BITMAP bmp;
34        GdiFlush();
35        HGDIOBJ h = GetCurrentObject( hdc, OBJ_BITMAP );
36
37        if (h == NULL)
38            return;
39        if (GetObject(h, sizeof(bmp), &bmp) == 0) //GetObject(): returns size of object, 0 if error 
40            return;
41
42        channels = bmp.bmBitsPixel/8;
43        dst_ptr = bmp.bmBits;
44    }
45
46    if( size.cx != image->width || size.cy != image->height || channels != channels0)
47    {
48        changed_size = true;
49
50        uchar buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
51        BITMAPINFO* binfo = (BITMAPINFO*)buffer;
52
53        BOOL bDeleteObj = DeleteObject(GetCurrentObject( hdc, OBJ_BITMAP));
54        assert( FALSE != bDeleteObj );
55
56        size.cx = image->width;
57        size.cy = image->height;
58        channels = channels0;
59
60        FillBitmapInfo( binfo, size.cx, size.cy, channels*8, 1 );
61        tempbinfo = binfo;
62
63        HGDIOBJ tempImage = SelectObject( hdc, CreateDIBSection( hdc, binfo, DIB_RGB_COLORS, &dst_ptr, 0, 0));
64    } 
65
66    cvInitMatHeader( &dst, size.cy, size.cx, CV_8UC3,dst_ptr, ( size.cx * channels + 3 ) & -4 );
67    cvConvertImage( image, &dst, origin == 0 ? CV_CVTIMG_FLIP : 0 );
68
69    // Image stretching to fit the window
70    RECT rect;
71    GetClientRect(w_hWnd, &rect);
72    int bSuccess = StretchDIBits( hdc,0, 0, rect.right, rect.bottom, 0, 0, image->width, image->height,
73                    dst_ptr, tempbinfo, DIB_RGB_COLORS, SRCCOPY );
74
75    InvalidateRect(w_hWnd, 0, 0);
76
77    __END__;
78}

Associated revisions

Revision f9fe1b9b
Added by Andrey Pavlenko over 11 years ago

Merge pull request #1607 from ilya-lavrenov:ocl_fft_cleanup

Revision e8451848
Added by Alexander Alekhin over 11 years ago

Merge remote-tracking branch 'upstream/2.4' into merge-2.4

  • #1538 from StevenPuttemans:bugfix_3283
  • #1545 from alalek:ocl_test_fix_rng
  • #1551 from alalek:cmake_install_win
  • #1570 from ilya-lavrenov:ipp_warn_fix
  • #1573 from alalek:perf_simple_strategy
  • #1574 from alalek:svm_workaround
  • #1576 from alalek:ocl_fix_cl_double
  • #1577 from ilya-lavrenov:ocl_setto_opencl12
  • #1578 from asmorkalov:android_fd_cp_fix
  • #1579 from ilya-lavrenov:ocl_norm
  • #1582 from sperrholz:ocl-arithm-additions
  • #1586 from ilya-lavrenov:ocl_setto_win_fix
  • #1589 from ilya-lavrenov:pr1582_fix
  • #1591 from alalek:ocl_remove_cl_hpp_h
  • #1592 from alalek:ocl_program_cache_update
  • #1593 from ilya-lavrenov:ocl_war_on_double
  • #1594 from ilya-lavrenov:ocl_perf
  • #1595 from alalek:cl_code_cleanup
  • #1596 from alalek:test_fix_run_py
  • #1597 from alalek:ocl_fix_cleanup
  • #1598 from alalek:ocl_fix_build_mac
  • #1599 from ilya-lavrenov:ocl_mac_kernel_warnings
  • #1601 from ilya-lavrenov:ocl_fix_tvl1_and_sparse
  • #1602 from alalek:ocl_test_dump_info
  • #1603 from ilya-lavrenov:ocl_disable_svm_noblas
  • #1605 from alalek:ocl_fixes
  • #1606 from ilya-lavrenov:ocl_imgproc
  • #1607 from ilya-lavrenov:ocl_fft_cleanup
  • #1608 from alalek:fix_warn_upd_haar
  • #1609 from ilya-lavrenov:ocl_some_optimization
  • #1610 from alalek:ocl_fix_perf_kalman
  • #1612 from alalek:ocl_fix_string_info
  • #1614 from ilya-lavrenov:ocl_svm_misprint
  • #1616 from ilya-lavrenov:ocl_cvtColor
  • #1617 from ilya-lavrenov:ocl_info
  • #1622 from a0byte:2.4
  • #1625 from ilya-lavrenov:to_string

Conflicts:
cmake/OpenCVConfig.cmake
cmake/OpenCVDetectPython.cmake
cmake/OpenCVGenConfig.cmake
modules/core/CMakeLists.txt
modules/nonfree/src/surf.ocl.cpp
modules/ocl/include/opencv2/ocl/ocl.hpp
modules/ocl/include/opencv2/ocl/private/util.hpp
modules/ocl/perf/main.cpp
modules/ocl/src/arithm.cpp
modules/ocl/src/cl_operations.cpp
modules/ocl/src/cl_programcache.cpp
modules/ocl/src/color.cpp
modules/ocl/src/fft.cpp
modules/ocl/src/filtering.cpp
modules/ocl/src/gemm.cpp
modules/ocl/src/haar.cpp
modules/ocl/src/imgproc.cpp
modules/ocl/src/matrix_operations.cpp
modules/ocl/src/pyrlk.cpp
modules/ocl/src/split_merge.cpp
modules/ocl/src/svm.cpp
modules/ocl/test/main.cpp
modules/ocl/test/test_fft.cpp
modules/ocl/test/test_moments.cpp
modules/ocl/test/test_objdetect.cpp
modules/ocl/test/test_optflow.cpp
modules/ocl/test/utility.hpp
modules/python/CMakeLists.txt
modules/ts/include/opencv2/ts.hpp
modules/ts/src/ts_perf.cpp
samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp

History

Updated by Alexander Reshetnikov about 13 years ago

  • Assignee set to Alexander Reshetnikov

Updated by Alexander Reshetnikov about 13 years ago

  • Status deleted (Open)

Updated by Alexander Reshetnikov about 13 years ago

Thank you for the report! It was added in r7353.

  • Status set to Done
  • Target version set to 2.4.0

Updated by Jonatan Vargas about 13 years ago

Hi Alex, thank you for including my contribution.
Just note that I didn't throw any errors in case of null nor invalid handles or input image, cause I had been taking care about it outside OpenCV. But it should be convenient not just return from the function but throw the convenient OPENCV_ERROR you decided.
Thank you.

Updated by Andrey Kamaev about 13 years ago

  • Description changed from Hi everybody here, I was developing one DLL involving in image processing for... to Hi everybody here, I was developing one DLL involving in image processing for... More

Updated by Andrey Kamaev over 12 years ago

  • Category changed from highgui-images to highgui-gui

Also available in: Atom PDF