< >

Set a Region of Interest (ROI), while Displaying Live Video

Blogged by Stefan Geißler on April 23, 2007.

Often, programmers are required to use a region of interest (ROI) in a C++ application. The ROI should be changed depending upon the content of the live video. After speaking to several customers who are attempting something on these lines, I realized that generally, there is confusion in this area. I have, thus, created a little function that illustrates how to set ROI parameters. I hope that this helps relieve the confusion a little :-).

First of all, we perform a little sanity checking to ensure that the ROI Frame Filter is up and running:

if( m_pROIFilter.get() )  // Ensure the ROI filter has been loaded.
{
}

The dialog offers four input fields for the left, top, width and height parameters of the new ROI. These values are retrieved and saved in the local variables x, y, w and h respectively.

In order to avoid that the video output is too small, the contents of w and h are limited to a minimum value of 10:

int x,y,w,h;
bool bRestartVideo = false;

// Get the values from the edit fields
x = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIX,NULL,TRUE);
y = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIY,NULL,TRUE);
w = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIWIDTH,NULL,TRUE);
h = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIHEIGHT,NULL,TRUE);

// Avoid too small live video
if( w < 10 ) w = 10;
if( h < 10 ) h = 10;

Additionally, we need a boolean variable that is set to true, if the live video must be started after we change the ROI. The variable is called bRestartVideo.

In the next step, we need to stop the live video, if it is running. This is necessary, as the size of the output video format is going to be changed. This also affects the size of IC Imaging Control's images buffers.

// Check for a running live video. It must be stopped, before the new
// ROI can be set.
if( m_cGrabber.isDevValid() )
{
    if( m_cGrabber.isLive() )
    {
        bRestartVideo = true; // Set to true, so the live video will be restarted later.
        m_cGrabber.stopLive();
    }
}

Next, the four new values for the ROI are passed to the ROI Frame Filter:

m_pROIFilter->beginParamTransfer();
m_pROIFilter->setParameter("Left",x);
m_pROIFilter->setParameter("Top",y);
m_pROIFilter->setParameter("Width",w);
m_pROIFilter->setParameter("Height",h);
m_pROIFilter->endParamTransfer();

In the case that the live video was previously running, it is restarted in the final step:

// If a live video was running, restart it.
if( bRestartVideo )
{
    m_cGrabber.startLive();
}

Below is the source code in its entirety. It was implemented as a button handler function in the sample application:

//////////////////////////////////////////////////////////////////////////
// OnBnClickedButtonsetroi
//
// Set the ROI to the values entered in the fields on the dialog.
//
void CSampleDlg::OnBnClickedButtonsetroi()
{
    if( m_pROIFilter.get() ) // Ensure the ROI filter has been loaded.
    {
        int x,y,w,h;
        bool bRestartVideo = false;

        // Get the values from the edit fields
        x = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIX,NULL,TRUE);
        y = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIY,NULL,TRUE);
        w = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIWIDTH,NULL,TRUE);
        h = ::GetDlgItemInt(this->m_hWnd,IDC_EDITROIHEIGHT,NULL,TRUE);
        
        // Avoid too small live video
        if( w < 10 ) w = 10;
        if( h < 10 ) h = 10;

        // Check for a running live video. It must be stopped, before the new
        // ROI can be set.
        if( m_cGrabber.isDevValid() )
        {
            if( m_cGrabber.isLive() )
            {
                bRestartVideo = true;
                m_cGrabber.stopLive();
            }
        }

        m_pROIFilter->beginParamTransfer();
        m_pROIFilter->setParameter("Left",x);
        m_pROIFilter->setParameter("Top",y);
        m_pROIFilter->setParameter("Width",w);
        m_pROIFilter->setParameter("Height",h);
        m_pROIFilter->endParamTransfer();

        // If a live video was running, restart it.
        if( bRestartVideo )
        {
            m_cGrabber.startLive();
        }

    }
}

Tagged with C++, ROI, Region of Interest, Frame Filter, Class Library and Sample Application.

Previous:New LabVIEW Sub VI for Device Open and Restore
Next:Service Pack 1 for IC Imaging Control 3.0 Released