Page 1 of 2

Displaying Image and Videos on my Parent Frame cMain

Posted: Thu Dec 05, 2019 9:00 am
by steven.pyae
HI, I am using OpenCV together with wxWidgets
From OpenCV, I have stored the Image in cv::Mat Test from the image read from the selectedfilepath. Conversions were done as well.
However, cv::imshow Opens another window to display.
Can I use the cv::Mat Test to show in wxwidgets? If it is a repeated thread, any guidance will be appreciated. The output looks like this now.
Capture.JPG
Capture.JPG (38.21 KiB) Viewed 820 times
The code is like this

Code: Select all

//Showing Video In a New Window
	std::string a = std::string(selectedfilepath.mb_str(wxConvUTF8));
	cv::Mat test = cv::imread(a, CV_LOAD_IMAGE_UNCHANGED);
	cv::imshow("TestPhoto", test);
	cv::waitKey();

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Thu Dec 05, 2019 2:14 pm
by doublemax
First you need a panel with a custom paint event handler that draws a bitmap. You can use this as a starting point:
https://wiki.wxwidgets.org/An_image_panel

Then you need to get RGB data from OpenCV. I know it's possible but i can't tell you exactly how and where.
From there you can either create a wxImage and convert it into a wxBitmap. This is slower, but less to code. Or you can write the RGB data directly into a wxBitmap using "raw bitmap access" . https://docs.wxwidgets.org/trunk/classw ... _data.html
Which should be faster.

However, all this still requires manual copying of the image data pixel-by-pixel. On modern hardware this will be fast enough for smaller images, but you probably won't be able to display full HD at 30 fps with this.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Thu Dec 05, 2019 2:30 pm
by Laurent Berger

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Fri Dec 06, 2019 7:55 am
by steven.pyae
Hi doublemax, Ultimately, I want to show the video on the Main Parent Frame.
I have written the following code. However, it is not playing my ".avi" file. I tried searching the documentations for supported media files for wxWidgets but I could not find any.
I have referred to the following document https://docs.wxwidgets.org/3.0/classwx_ ... 549bde6284
and Written the following codes to show a media file but it is not working

Code: Select all

void cMain::FilePathShowVideo()
{
	wxString Filter = "mp4|*.mp4|avi|*.avi";  //TO:DO Select Mode for Video
	wxFileDialog
		openFileDialog(this, ("Load Video File"), "", "", Filter , wxFD_OPEN | wxFD_FILE_MUST_EXIST); //Select Mode of Files
	if (openFileDialog.ShowModal() == wxID_CANCEL)
	{	m_list1->AppendString("User Cancelled");
		return;	
	}
	else
		selectedfilepath = openFileDialog.GetPath();
	//Code to Open File here and Display
	wxFileInputStream input_stream(openFileDialog.GetPath());
	m_list1->AppendString(selectedfilepath);

	if (!input_stream.IsOk())
	{
		wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
		return;
	}
	//Showing Video In the Same Parent Frame
	std::string a = std::string(selectedfilepath.mb_str(wxConvUTF8));
	video_panel = new wxMediaCtrl(this, wxID_ANY, a, wxPoint(10, 100), wxSize(400, 400));
	if (video_panel->GetState() != wxMEDIASTATE_PLAYING && video_panel->Stop() == FALSE) {
		video_panel->Play();
	}
}
The Media File will be loaded by me via an Event.
Any help?

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Fri Dec 06, 2019 8:42 am
by doublemax
You need to catch the EVT_MEDIA_LOADED event and play the video from there.

From the docs:
For general operation, all you need to do is call Load() to load the file you want to render, catch the EVT_MEDIA_LOADED event, and then call Play() to show the video/audio of the media in that event.
And just to be clear: While wxMediaCtrl can play videos, you won't have any access to the frame data. That's why i didn't mentioned it as i assumed you'd want to perform some filtering/processing on the video frames.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Fri Dec 06, 2019 8:44 am
by steven.pyae
An Update is that I got the video to load in my frame however, I need to use the controls to play the video.
Is there a way to do it so that it automatically plays the video?
My code currently looks like this

Code: Select all

void cMain::FilePathShowVideo()
{
	wxString Filter = "avi|*.avi|mp4|*.mp4";  //TO:DO Select Mode for Video
	wxFileDialog
		openFileDialog(this, ("Load Video File"), "", "", Filter , wxFD_OPEN | wxFD_FILE_MUST_EXIST); //Select Mode of Files
	if (openFileDialog.ShowModal() == wxID_CANCEL)
	{	m_list1->AppendString("User Cancelled");
		return;	
	}
	else
		selectedfilepath = openFileDialog.GetPath();
	//Code to Open File here and Display
	wxFileInputStream input_stream(openFileDialog.GetPath());
	m_list1->AppendString(selectedfilepath);

	if (!input_stream.IsOk())
	{
		wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
		return;
	}
	//Showing Video In the Same Parent Frame
	std::string a = std::string(selectedfilepath.mb_str(wxConvUTF8));


	video_panel = new wxMediaCtrl(this, wxID_ANY, a, wxPoint(10, 100), wxSize(400, 400), 0);
	video_panel->Load(a);
	video_panel->SetPlaybackRate(1.0);
	video_panel->ShowPlayerControls(wxMEDIACTRLPLAYERCONTROLS_DEFAULT);

//To DO:: To let the Video Play automatically after loading
}

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Fri Dec 06, 2019 8:47 am
by doublemax
Is there a way to do it so that it automatically plays the video?
See my previous post.

Code: Select all

std::string a = std::string(selectedfilepath.mb_str(wxConvUTF8));
This won't work if the path contains any non-ascii chars. Just pass the result from openFileDialog.GetPath() directly into wxMediaCtrl.

Code: Select all

wxFileInputStream input_stream(openFileDialog.GetPath());
This seems unnecessary.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Mon Dec 09, 2019 6:39 am
by steven.pyae
Hi DoubleMax,

Thank you so much for the input. I am considering Bitmapping option to process the video images frame by frame.
For a Start, I want to show a single image in the parent window
I followed the Image Panel example but I did not want to show in the New frame,
Is it possible?

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Mon Dec 09, 2019 7:06 am
by doublemax
The imagepanel sample is only written to display a single image. You'll need to add a method that replaces the bitmap and then calls Refresh() to force a visible update.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Tue Dec 10, 2019 2:36 am
by steven.pyae
Hi Doublemax,

So far I have used StaticBitMap to display a single image. Is there such a thing called dynamic bitmap?
or How can I display the video in my current output?
My code for displaying is this

Code: Select all

wxStaticBitmap* sbmp = new wxStaticBitmap(this, wxID_ANY, wxNullBitmap, wxPoint(20, 100), wxSize(400, 400));
	sbmp->SetBitmap(wx_img);
Currently I have this.
Appreciate your inputs and Cheers

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Tue Dec 10, 2019 6:13 am
by doublemax
Is there such a thing called dynamic bitmap?
or How can I display the video in my current output?
You have to load a video frame, convert it to wxBitmap, call sbmp->SetBitmap(...), and then sbmp ->Refresh().

You could do this in a timer event with the timer running at the frame rate of the video.

Maybe this helps: viewtopic.php?p=183279#p183279
(You'll only need the gl_bitmap_panel.h from there).

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Tue Dec 10, 2019 8:00 am
by steven.pyae
Hi Doublemax, I have another question.

Is it possible to make a child frame of the Main Parent Frame using OpenCV imshow?
This is to clean up the code by making another class to display the images.
If so, Any references?
Thanks a bunch

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Tue Dec 10, 2019 12:16 pm
by doublemax
Is it possible to make a child frame of the Main Parent Frame using OpenCV imshow?
This is more an OpenCV question. It would require that you can set a parent for the imshow through a native window handle, e.g. HWND under Windows. I looked through the OpenCV docu a bit, but couldn't find anything. But i'm not an OpenCV expert, maybe there is way somehow.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Tue Dec 10, 2019 6:05 pm
by ONEEYEMAN
Hi,
I think there was a try to combine OpenCV and wxWidgets.
I'd try Google...

Thank you.

Re: Displaying Image and Videos on my Parent Frame cMain

Posted: Wed Dec 11, 2019 3:07 am
by steven.pyae
Hi Doublemax and Oneeyeman,

I have found http://larryo.org/work/information/wxopencv/index.html to reference to OpenCV.
However, my Programming is not that strong and currently I have a lot of lines in my cMain.cpp
For example, I have created another class named cTest.cpp and cTest.h
wxMDIParentFrame is created in my cMain.cpp with this code

Code: Select all

cMain::cMain() : wxMDIParentFrame(nullptr, window::id::Main_ID, "Test Program", wxPoint(30, 30), wxSize(1200, 1200))
Is there a way to use another class cTest.cpp to create wxCheckBox or wxListBox or MenuEvents in the cMain parent frame?
This is in order to reduce lines in my cMain.cpp