OpenCV HighGui module available for WinRT

clip_image002[12]MS Open Tech has just made a new contribution to the OpenCV open source project. It introduces support for Modern Windows of most of the API surface of the highgui module, which is used for quick UI prototyping. The only functionality that was not covered by this contribution was keyboard and mouse events tracking.

We are especially happy to share this code with the community since highgui is particularly valuable to OpenCV developers who are in the early phases of a new project, and want to experiment with different visual effects and image processing algorithms. Being able to leverage a cross-platform API like highgui that allows them to stand up functional, cross platform code quickly is a boon to productivity. That was noted in feedback to some of our previous announcements about OpenCV. As you can tell, we have been listening!!

 

Getting started

With the highgui module, you can use quick preview and interaction APIs likenamedWindow(),imshow(), createTrackbar() etc. from Windows Runtime 8.1+ applications. 

 

In the example below, we create a relatively small sample layout (MainPage.xaml) with a stack panel and a button:

 

<Page x:Class="FaceDetection.MainPage" 

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

      xmlns:local="using:FaceDetection" 

      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

      mc:Ignorable="d"> 

  

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

        <Button x:Name="ProcessBtn" 

                Width="218" 

                Height="67" 

                Margin="69,81,0,0" 

                HorizontalAlignment="Left" 

                VerticalAlignment="Top" 

                Click="processBtn_Click" 

                Content="Initialize" /> 

        <StackPanel x:Name="cvContainer" 

                    Width="800" 

                    Height="450" 

                    Margin="360,85,0,0" 

                    HorizontalAlignment="Left" 

                    VerticalAlignment="Top" /> 

  

    </Grid> 

</Page> 

 

The cvContainer stackpanel will be used as a component to hold the highgui-generated UI. This code shows an image from the Assets folder, and a couple of track bars:

 

void HighguiSample::MainPage::processBtn_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 

{ 

// creating name that can be passed to a slider callback 

static cv::String windowName("sample"); 

 

// Assigning a container that will be used to hold highgui windows: display image and trackbars 

cv::winrt_initContainer(this->cvContainer); 

 

// Creating a windows w/o displaying it. This step is actually not required but provided for reference 

cv::namedWindow(windowName); 

 

// Reading image from Assets 

cv::Mat image = cv::imread("Assets/sample.jpg"); 

 

// Converting image to default Window format so it can be properly displayed 

cv::Mat converted = cv::Mat(image.rows, image.cols, CV_8UC4); 

cvtColor(image, converted, CV_BGR2BGRA); 

 

// Showing the image in a window (that will be placed within a container referenced above) 

// It’ll create window if it hasn't been created before via namedWindow 

cv::imshow(windowName, converted);  

 

// Random but ultimate value :) 

int state = 42; 

 

// Creating callbacks to be used for trackbars 

cv::TrackbarCallback callback = [](int pos, void* userdata) 

{ 

    if (pos == 0) { 

  // This one will destroy selected window if we scroll it down to zero value 

  // We have a single window in this example and currently only one window can be displayed 

  // but you can have several windows pre-created and switch between displaying either of them 

        cv::destroyWindow(windowName); 

    } 

}; 

cv::TrackbarCallback callbackTwin = [](int pos, void* userdata) 

{ 

    if (pos >= 70) { 

  // This one will all windows if we scroll it higher up to provided value 

        cv::destroyAllWindows(); 

    } 

}; 

 

// Create two trackbars with callbacks created earlier. Trackbars are differentiated by names. 

cv::createTrackbar("Sample trackbar", windowName, &state, 100, callback); 

cv::createTrackbar("Twin brother", windowName, &state, 100, callbackTwin); 

} 

 

Working sample extended with face detection and the two trackbars is available here. 

clip_image003[4]

 

This work completes a series of contributions to OpenCV by Microsoft Open Technologies to enable first-class support of Modern Windows, including the upcoming Windows 10. We look forward to seeing community contributions that will further enhance this work!

 

Until then, we hope that you enjoy using OpenCV in Visual Studio. The Windows Store is waiting for your great apps! J Happy coding!

 

Adalberto Foresti
Principal Program Manager
Microsoft Open Technologies, Inc

Eric Mittelette
Senior Technical Evangelist
Microsoft Open Technologies, Inc