wxwidgets_opencv_example/main.cpp

106 lines
3.0 KiB
C++
Raw Normal View History

2023-08-05 18:02:02 +00:00
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <iostream>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <algorithm>
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
using namespace std;
using namespace cv;
class ExampleApp: public wxApp
{
public:
virtual bool OnInit();
};
class OpenCVwx: public wxFrame
{
public:
OpenCVwx(const wxString& title, const wxPoint& pos, const wxSize& size);
wxImage wx_from_mat(Mat& img);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
wxBEGIN_EVENT_TABLE(OpenCVwx, wxFrame)
EVT_MENU(ID_Hello, OpenCVwx::OnHello)
EVT_MENU(wxID_EXIT, OpenCVwx::OnExit)
EVT_MENU(wxID_ABOUT, OpenCVwx::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(ExampleApp);
bool ExampleApp::OnInit()
{
OpenCVwx *frame = new OpenCVwx( "OpenCV+wxWidgets Example", wxPoint(50, 50), wxSize(450, 340) );
Mat image(100, 100, CV_8UC3, Scalar(0, 0, 0));
wxImage displayImage = frame->wx_from_mat(image);
wxBitmap bitmap(displayImage.Scale(100, 100, wxIMAGE_QUALITY_HIGH));
wxPaintDC dc(frame);
dc.DrawBitmap(bitmap, 0, 0);
frame->Show( true );
return true;
}
OpenCVwx::OpenCVwx(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( "Welcome to wxWidgets!" );
//ShowFullScreen(true, wxFULLSCREEN_ALL);
}
void OpenCVwx::OnExit(wxCommandEvent& event)
{
Close( true );
}
void OpenCVwx::OnAbout(wxCommandEvent& event)
{
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void OpenCVwx::OnHello(wxCommandEvent& event)
{
wxLogMessage("This app uses OpenCV");
}
//https://forums.wxwidgets.org/viewtopic.php?t=46670
wxImage OpenCVwx::wx_from_mat(Mat& img)
{
Mat im2;
if (img.channels() == 1) { cvtColor(img, im2, COLOR_GRAY2RGB); }
else if (img.channels() == 4) { cvtColor(img, im2, COLOR_BGRA2RGB); }
else { cvtColor(img, im2, COLOR_BGR2RGB); }
long imsize = im2.rows * im2.cols * im2.channels();
wxImage wx(im2.cols, im2.rows, (unsigned char*)malloc(imsize), false);
unsigned char* s = im2.data;
unsigned char* d = wx.GetData();
for (long i = 0; i < imsize; i++) { d[i] = s[i]; }
return wx;
return wxImage();
}