我写了一个VideoBox类,显示使用opencv从视频文件或网络摄像头捕获的图像这是我在c ++中的代码,我需要一个固定大小的框,所以我覆盖最小和最大
public ref class VideoBox : public System::Windows::Forms::PictureBox
{
public:
VideoBox();
~VideoBox();
bool capture()
{
cv::Mat cur_frame;
bool r = maintracker.capture(cur_frame);
if(!r) return r;
imshow("debug window", cur_frame);
this->Image = gcnew System::Drawing::Bitmap(cur_frame.cols, cur_frame.rows, cur_frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, (System::IntPtr)cur_frame.ptr());
this->Refresh();
return true;
}
....
virtual property System::Drawing::Size MinimumSize {
System::Drawing::Size get() override { return m_desiredSize; }
void set(System::Drawing::Size) override { }
}
virtual property System::Drawing::Size MaximumSize {
System::Drawing::Size get() override { return m_desiredSize; }
void set(System::Drawing::Size) override { }
}
protected:
/* virtual void OnPaint(System::Windows::Forms::PaintEventArgs^ pe) override
{
PictureBox::OnPaint(pe);
}*/
private:
System::Drawing::Size m_desiredSize;
};
这是我从视频盒继承的c#类
public class Video : VideoBox
{
public enum DrawMode { DRAW_H_LINE, DRAW_V_LINE, DRAW_RECT, NONE };
public enum ShowMode { SHOW_USER_RECTS, SHOW_USER_LINES, HIDE_ALL, SHOW_ALL };
public Video()
{
b_drawrect = b_drawhline = b_drawvline = false;
lstBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Green, Color.Transparent);
cTimer = new Timer();
cTimer.Interval = 100; // msec for change frame in video mode
cTimer.Tick += new EventHandler(cTimer_Tick);
// if you uncomment this you can see your image on design mode :D
//setCaptureVideo();
m_drMode = DrawMode.DRAW_RECT;
m_userRects = new List<Rectangle>();
}
public void setCaptureVideo()
{
...
}
public void startVideo() { cTimer.Start(); }
public void stopVideo() { cTimer.Stop(); }
private void cTimer_Tick(object sender, EventArgs e)
{
capture();
}
//protected override void OnPaint(PaintEventArgs e)
//{
// base.OnPaint(e);
// Graphics g = e.Graphics;
// if (m_insertRect.Width > 0 && b_drawrect)
// {
// Pen mP = new Pen(Color.Green);
// g.DrawRectangle(mP, m_insertRect);
// }
// if (m_shMode == ShowMode.SHOW_USER_RECTS || m_shMode == ShowMode.SHOW_ALL)
// {
// Font drawFont = new Font("Arial",8 );
// SolidBrush drawBrush = new SolidBrush(Color.Red);
// // Set format of string.
// StringFormat drawFormat = new StringFormat();
// if(m_userRects.Count > 0)
// {
// int c=1;
// foreach(Rectangle rect in m_userRects)
// {
// g.FillRectangle(lstBrush, rect);
// g.DrawString(c.ToString(), drawFont, drawBrush, rect.Location, drawFormat);
// c++;
// }
// }
// }
//}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (m_drMode == DrawMode.DRAW_RECT && !m_firstp.IsEmpty)
{
b_drawrect = true;
float x = m_firstp.X < e.X ? m_firstp.X : e.X;
float y = m_firstp.Y < e.Y ? m_firstp.Y : e.Y;
float w = Math.Abs(m_firstp.X - e.X);
float h = Math.Abs(m_firstp.Y - e.Y);
m_insertRect = new Rectangle((int)x, (int)y, (int)w, (int)h);
if (!cTimer.Enabled)
this.Refresh();
}
}
if(m_drMode == DrawMode.DRAW_H_LINE && !m_firstp.IsEmpty)
{
m_hlinep = e.Location;
b_drawhline = true;
if(!cTimer.Enabled)
this.Refresh();
}
else if (m_drMode == DrawMode.DRAW_V_LINE && !m_firstp.IsEmpty)
{
m_vlinep = e.Location;
b_drawvline = true;
if (!cTimer.Enabled)
this.Refresh();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (b_drawvline)
{
b_drawvline = false;
m_vlinep = e.Location;
m_drMode = DrawMode.NONE;
}
else if (b_drawhline)
{
b_drawhline = false;
m_hlinep = e.Location;
m_drMode = DrawMode.NONE;
}
else
m_firstp = e.Location;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (m_drMode == DrawMode.DRAW_RECT)
{
if (b_drawrect)
{
m_userRects.Add(m_insertRect);
b_drawrect = false;
if (!cTimer.Enabled)
this.Refresh();
}
}
}
// private members
private List<Rectangle> m_userRects;
private Timer cTimer;
private PointF m_firstp;
private PointF m_hlinep, m_vlinep;
private Rectangle m_insertRect;
private bool b_drawrect, b_drawhline, b_drawvline;
private HatchBrush lstBrush;
private ShowMode m_shMode;
private DrawMode m_drMode;
}
如果我调整窗体大小这么多次VideoBox显示picturebox的ErrorImage
如果我在启用cTimer时刷新视频框,则会再次显示ErrorImage
imshow无问题地工作
你认为问题在哪里?
答案 0 :(得分:1)
首先,需要注意的一点是:opencv,主要是基于RAII,托管c ++并不是很好。
在捕获函数中分配本地 Mat:
bool capture()
{
cv::Mat cur_frame;
...
一旦它超出范围(离开capture()时),它将释放/使其数据指针无效,并且你的ImageBox留有无效/悬空指针。
所以,你必须在程序的整个过程中保持cv :: Mat cur_frame活着,这里变得非常难看:
public:
cv::Mat *cur_frame; // yep, a bloody *pointer*, as stupid managed c++ can't do RAII...
VideoBox()
{
cur_frame = new Mat();
}
~VideoBox()
{
delete cur_frame;
}
bool capture()
{
bool r = maintracker.capture( *cur_frame ); // deref
if(!r) return r;
imshow("debug window", *cur_frame);
this->Image = gcnew System::Drawing::Bitmap(cur_frame->cols, cur_frame->rows, cur_frame->step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, (System::IntPtr)cur_frame->ptr());
this->Refresh();
return true;
}