gpt4 book ai didi

c# - 刷新两次picturebox显示错误图像

转载 作者:行者123 更新时间:2023-12-02 17:49:26 25 4
gpt4 key购买 nike

我编写了一个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#类从VideoBox继承
 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可以正常工作

您认为问题出在哪里?

最佳答案

首先,请注意:opencv(主要基于RAII)和托管c++不能一起使用。

您可以在捕获功能中分配本地Mat:

bool capture()
{
cv::Mat cur_frame;
...

一旦它超出范围(离开capture()时),并且您的ImageBox留下无效/悬空的指针,它将释放/使它的数据指针无效。

因此,您必须在整个程序运行期间保持cv::Mat cur_frame处于 Activity 状态,在此情况下,它变得非常难看:
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;
}

关于c# - 刷新两次picturebox显示错误图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com