OpenCV 网络摄像头提要未在 PictureBox 中显示 visual studio 2015
OpenCV Webcam feed not displaying in PictureBox visual studio 2015
您好,我使用 opencv 从我的默认摄像头获取网络摄像头源,我希望将其显示在我的 windows 表单的图片框中。我的网络摄像头出现了,但由于某种原因,提要从未显示在我的图片框上。请有人帮助指出 out/solve 这个问题,因为我现在被困在这里。
提前致谢。
在 myform.h 中,我有将图片框发送到 myform.cpp 文件的代码:
System::Windows::Forms::PictureBox^ mypicbox1(void)
{
opencv_gui::MyForm aform;
return aform.pictureBox1;
}
绘制代码获取视频源并放入我在 myform.cpp 中的图片框是:
void opencv_gui::DrawCvImage(const cv::Mat& cvImage)
{
System::Windows::Forms::PictureBox^ pictureBox = mypicbox1();
// only color images are supported
assert(cvImage.type() == CV_8UC3);
if ((pictureBox->Image == nullptr) || (pictureBox->Width != cvImage.cols) || (pictureBox->Height != cvImage.rows))
{
pictureBox->Width = cvImage.cols;
pictureBox->Height = cvImage.rows;
pictureBox->Image = gcnew System::Drawing::Bitmap(cvImage.cols, cvImage.rows);
}
// Create System::Drawing::Bitmap from cv::Mat
System::Drawing::Bitmap^ bmpImage = gcnew System::Drawing::Bitmap(
cvImage.cols, cvImage.rows, cvImage.step,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,
System::IntPtr(cvImage.data)
);
// Draw Bitmap over a PictureBox
System::Drawing::Graphics^ g = System::Drawing::Graphics::FromImage(pictureBox->Image);
g->DrawImage(bmpImage, 0, 0, cvImage.cols, cvImage.rows);
pictureBox->Refresh();
delete g;
}
//camera feed
int opencv_gui::video_cap(void)
{
VideoCapture cap;
if (!cap.open(0)) // open the default camera (camera 0), use something different from 0 otherwise;
return 0;
for (;;)
{
Mat frame;
cap >> frame;
if (frame.empty()) break; // end of video stream
DrawCvImage(frame);
if (waitKey(10) == 27) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
这是我的调试日志:我使用像“>>>>>>>”这样的箭头来显示重要参数,用 "RED >>>>>>>" 来显示错误参数。
- cvImage { flags=1124024336 dims=2 rows=480 ...} cv::Mat&
- 分配器 0x0000000000000000 cv::Mat分配器*
cols 640 int
data 0x0000026AC2146F80 unsigned char*
*(*cvImage).data 0 '' unsigned char
- 数据结束 0x0000026AC2227F80 无符号字符*
- 数据限制 0x0000026AC2227F80 无符号字符*
- datastart 0x0000026AC2146F80 无符号字符*
调暗 2 整数
标志 1124024336 int
第 480 行整数
size { p=0x000000DBDFF0DE80 } cv::MatSize
p 0x000000DBDFF0DE80 int*
*(*cvImage).size.p 0 int
- step { p=0x000000DBDFF0DEC8 buf={Length=2} } cv::MatStep
- buf {Length=2} 无符号 __int64[]
- p 0x000000DBDFF0DEC8 无符号 __int64*
- u 0x0000026AA4814780 cv::UMatData*
- RED >>>>>>> bmpImage 0x0000026aa62a1fd0 { defaultTransparentColor= } System::Drawing::Bitmap^
- RED >>>>>>> System::Drawing::Image^ 0x0000026aa62a1fd0 { nativeImage=2657511292512 rawData= userData= } System::Drawing::Image^
默认透明颜色 System::Drawing::Color
g System::Drawing::Graphics^
- RED >>>>>>> pictureBox 0x0000026aa62a1388 { borderStyle=System::Windows::Forms::BorderStyle::None image=0x0000026aa62a1fa0 sizeMode=System::Windows::Forms::PictureBoxSizeMode::Normal ...} System::Windows::Forms::PictureBox^
- RED >>>>>>> System::Windows::Forms::Control^ 0x0000026aa62a1388 { ControlKeyboardRouting= PaletteTracing= FocusTracing= ...} System::Windows::Forms::Control^
AllowDrop 假布尔
边框样式 System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle
CausesValidation 真布尔
- CreateParams 0x0000026aa62a15d8 { className= caption= style=1442840576 ...} System::Windows::Forms::CreateParams^
DefaultImeMode System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode
- 默认大小{宽度=100 高度=50}System::Drawing::Size
EVENT_SIZEMODECHANGED0x0000026aa629af50System::Object^
- ErrorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
- 字体 0x0000026aa6280148{} System::Drawing::Font^
- 前景色 {RGB=0x0} System::Drawing::Color
- 图片 0x0000026aa62a1fa0 { nativeImage=2657511290992 rawData= userData= } System::Drawing::Image^
- [System::Drawing::Bitmap^] 0x0000026aa62a1fa0 { defaultTransparentColor= } System::Drawing::Bitmap^
- System::MarshalByRefObject^ 0x0000026aa62a1fa0 { __identity= } System::MarshalByRefObject^
标志 2 整数
- FrameDimensionsList {Length=1} 数组 ^
身高 480 int
水平分辨率 96.000000 浮动
- 调色板 0x0000026aa62a25e8 { flags=1129621568 entries={Length=0} } System::Drawing::Imaging::ColorPalette^
- PhysicalDimension { width=640.00000 height=480.00000 } System::Drawing::SizeF
像素格式 System::Drawing::Imaging::PixelFormat::Format32bppArgb System::Drawing::Imaging::PixelFormat
PropertyIdList {Length=0} 数组 ^
PropertyItems {Length=0} 数组 ^
- 原始格式 0x0000026aa62a3488 { ...} System::Drawing::Imaging::ImageFormat^
- 尺寸{宽=640 高=480}System::Drawing::Size
标签 System::Object^
垂直分辨率 96.000000 浮动
宽度 640 英寸
nativeImage 2657511290992 __int64
原始数据数组 ^
用户数据 System::Object^
图片位置 System::String^
- ImageRectangle {X=0 Y=0 宽度=640 高度=480} System::Drawing::Rectangle
输入模式 System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode
- InitialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
PICTUREBOXSTATE_asyncOperationInProgress 1 个整数
PICTUREBOXSTATE_cancellationPending 2 整数
PICTUREBOXSTATE_inInitialization 64 整数
PICTUREBOXSTATE_needToLoadImageLocation 32 整数
PICTUREBOXSTATE_useDefaultErrorImage 8 整数
PICTUREBOXSTATE_useDefaultInitialImage 4 整数
PICTUREBOXSTATE_waitOnLoad 16 整数
从右到左 System::Windows::Forms::RightToLeft::No System::Windows::Forms::RightToLeft
SizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode
TabIndex 1 整数
TabStop 假布尔
文字“”System::String^
WaitOnLoad 假布尔
borderStyle System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle
内容长度 0 整数
currentAsyncLoadOperation System::ComponentModel::AsyncOperation^
currentlyAnimating false 布尔
- defaultErrorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
- defaultErrorImageForThread 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
defaultErrorImageKey 0x0000026aa629af08 System::Object^
- defaultInitialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
- defaultInitialImageForThread 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
defaultInitialImageKey 0x0000026aa629aef0 System::Object^
- errorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
handleValid false 布尔
- image 0x0000026aa62a1fa0 { nativeImage=2657511290992 rawData= userData= } System::Drawing::Image^
图像安装类型 ImageInstallationType::DirectlySpecified 图像安装类型
图片位置 System::String^
- initialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
- [System::Drawing::Bitmap^] 0x0000026aa629c568 { defaultTransparentColor= } System::Drawing::Bitmap^
- System::MarshalByRefObject^ 0x0000026aa629c568 { __identity= } System::MarshalByRefObject^
标志 73744 int
- FrameDimensionsList {Length=1} 数组 ^
高度 16 int
水平分辨率 96.000000 浮动
- RED >>>>>>>调色板 0x0000026aa629ea60 { flags=951278840 entries={Length=0} } System::Drawing::Imaging::ColorPalette^
- PhysicalDimension { width=14.000000 height=16.000000 } System::Drawing::SizeF
像素格式 System::Drawing::Imaging::PixelFormat::Format24bppRgb System::Drawing::Imaging::PixelFormat
PropertyIdList {Length=0} 数组 ^
PropertyItems {Length=0} 数组 ^
- RED >>>>>>> RawFormat 0x0000026aa629f900 { ...} System::Drawing::Imaging::ImageFormat^
- 尺寸{宽=14 高=16}System::Drawing::Size
标签 System::Object^
垂直分辨率 96.000000 浮动
宽度 14 整数
nativeImage 2657511196256 __int64
原始数据数组 ^
用户数据 System::Object^
红色 >>>>>>> 内部同步对象 0x0000026aa629d900 System::Object^
loadCompletedDelegate System::Threading::SendOrPostCallback^
loadCompletedKey 0x0000026aa629af20 System::Object^
loadProgressChangedKey 0x0000026aa629af38 System::Object^
loadProgressDelegate System::Threading::SendOrPostCallback^
localImageStreamReader System::IO::StreamReader^
- pictureBoxState { 数据=12 } System::Collections::Specialized::BitVector32
readBlockSize 4096 整数
readBuffer 数组 ^
- savedSize {宽度=640 高度=480} System::Drawing::Size
sizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode
tempDownloadStream System::IO::MemoryStream^
totalBytesRead 0 整数
uriImageStream System::IO::Stream^
不得不稍微编辑一下我的代码,但我使用此处提供的答案几乎解决了我的问题:
试试这个
private: System::Void button2_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
if (button2->Text == "Stop")
{
button2->Text = "Start";
}
else if (button2->Text == "Start")
{
button2->Text = "Stop";
}
VideoCapture capture(0);
Mat frame;
while (button2->Text == "Stop")
{
capture.read(frame);
System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
System::IntPtr ptr2(frame.ptr());
System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols,
frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
System::Drawing::RectangleF rect2(0, 0, pictureBox1->Width, pictureBox1->Height);
graphics2->DrawImage(b2, rect2);
}
}
您好,我使用 opencv 从我的默认摄像头获取网络摄像头源,我希望将其显示在我的 windows 表单的图片框中。我的网络摄像头出现了,但由于某种原因,提要从未显示在我的图片框上。请有人帮助指出 out/solve 这个问题,因为我现在被困在这里。 提前致谢。
在 myform.h 中,我有将图片框发送到 myform.cpp 文件的代码:
System::Windows::Forms::PictureBox^ mypicbox1(void)
{
opencv_gui::MyForm aform;
return aform.pictureBox1;
}
绘制代码获取视频源并放入我在 myform.cpp 中的图片框是:
void opencv_gui::DrawCvImage(const cv::Mat& cvImage)
{
System::Windows::Forms::PictureBox^ pictureBox = mypicbox1();
// only color images are supported
assert(cvImage.type() == CV_8UC3);
if ((pictureBox->Image == nullptr) || (pictureBox->Width != cvImage.cols) || (pictureBox->Height != cvImage.rows))
{
pictureBox->Width = cvImage.cols;
pictureBox->Height = cvImage.rows;
pictureBox->Image = gcnew System::Drawing::Bitmap(cvImage.cols, cvImage.rows);
}
// Create System::Drawing::Bitmap from cv::Mat
System::Drawing::Bitmap^ bmpImage = gcnew System::Drawing::Bitmap(
cvImage.cols, cvImage.rows, cvImage.step,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,
System::IntPtr(cvImage.data)
);
// Draw Bitmap over a PictureBox
System::Drawing::Graphics^ g = System::Drawing::Graphics::FromImage(pictureBox->Image);
g->DrawImage(bmpImage, 0, 0, cvImage.cols, cvImage.rows);
pictureBox->Refresh();
delete g;
}
//camera feed
int opencv_gui::video_cap(void)
{
VideoCapture cap;
if (!cap.open(0)) // open the default camera (camera 0), use something different from 0 otherwise;
return 0;
for (;;)
{
Mat frame;
cap >> frame;
if (frame.empty()) break; // end of video stream
DrawCvImage(frame);
if (waitKey(10) == 27) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
这是我的调试日志:我使用像“>>>>>>>”这样的箭头来显示重要参数,用 "RED >>>>>>>" 来显示错误参数。
- cvImage { flags=1124024336 dims=2 rows=480 ...} cv::Mat&
- 分配器 0x0000000000000000 cv::Mat分配器*
cols 640 int
data 0x0000026AC2146F80 unsigned char* *(*cvImage).data 0 '' unsigned char
- 数据结束 0x0000026AC2227F80 无符号字符*
- 数据限制 0x0000026AC2227F80 无符号字符*
- datastart 0x0000026AC2146F80 无符号字符* 调暗 2 整数 标志 1124024336 int 第 480 行整数
size { p=0x000000DBDFF0DE80 } cv::MatSize
p 0x000000DBDFF0DE80 int* *(*cvImage).size.p 0 int
- step { p=0x000000DBDFF0DEC8 buf={Length=2} } cv::MatStep
- buf {Length=2} 无符号 __int64[]
- p 0x000000DBDFF0DEC8 无符号 __int64*
- u 0x0000026AA4814780 cv::UMatData*
- RED >>>>>>> bmpImage 0x0000026aa62a1fd0 { defaultTransparentColor= } System::Drawing::Bitmap^
- RED >>>>>>> System::Drawing::Image^ 0x0000026aa62a1fd0 { nativeImage=2657511292512 rawData= userData= } System::Drawing::Image^ 默认透明颜色 System::Drawing::Color g System::Drawing::Graphics^
- RED >>>>>>> pictureBox 0x0000026aa62a1388 { borderStyle=System::Windows::Forms::BorderStyle::None image=0x0000026aa62a1fa0 sizeMode=System::Windows::Forms::PictureBoxSizeMode::Normal ...} System::Windows::Forms::PictureBox^
- RED >>>>>>> System::Windows::Forms::Control^ 0x0000026aa62a1388 { ControlKeyboardRouting= PaletteTracing= FocusTracing= ...} System::Windows::Forms::Control^ AllowDrop 假布尔 边框样式 System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle CausesValidation 真布尔
- CreateParams 0x0000026aa62a15d8 { className= caption= style=1442840576 ...} System::Windows::Forms::CreateParams^ DefaultImeMode System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode
- 默认大小{宽度=100 高度=50}System::Drawing::Size EVENT_SIZEMODECHANGED0x0000026aa629af50System::Object^
- ErrorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
- 字体 0x0000026aa6280148{} System::Drawing::Font^
- 前景色 {RGB=0x0} System::Drawing::Color
- 图片 0x0000026aa62a1fa0 { nativeImage=2657511290992 rawData= userData= } System::Drawing::Image^
- [System::Drawing::Bitmap^] 0x0000026aa62a1fa0 { defaultTransparentColor= } System::Drawing::Bitmap^
- System::MarshalByRefObject^ 0x0000026aa62a1fa0 { __identity= } System::MarshalByRefObject^ 标志 2 整数
- FrameDimensionsList {Length=1} 数组 ^ 身高 480 int 水平分辨率 96.000000 浮动
- 调色板 0x0000026aa62a25e8 { flags=1129621568 entries={Length=0} } System::Drawing::Imaging::ColorPalette^
- PhysicalDimension { width=640.00000 height=480.00000 } System::Drawing::SizeF 像素格式 System::Drawing::Imaging::PixelFormat::Format32bppArgb System::Drawing::Imaging::PixelFormat PropertyIdList {Length=0} 数组 ^ PropertyItems {Length=0} 数组 ^
- 原始格式 0x0000026aa62a3488 { ...} System::Drawing::Imaging::ImageFormat^
- 尺寸{宽=640 高=480}System::Drawing::Size 标签 System::Object^ 垂直分辨率 96.000000 浮动 宽度 640 英寸 nativeImage 2657511290992 __int64 原始数据数组 ^ 用户数据 System::Object^ 图片位置 System::String^
- ImageRectangle {X=0 Y=0 宽度=640 高度=480} System::Drawing::Rectangle 输入模式 System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode
- InitialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^ PICTUREBOXSTATE_asyncOperationInProgress 1 个整数 PICTUREBOXSTATE_cancellationPending 2 整数 PICTUREBOXSTATE_inInitialization 64 整数 PICTUREBOXSTATE_needToLoadImageLocation 32 整数 PICTUREBOXSTATE_useDefaultErrorImage 8 整数 PICTUREBOXSTATE_useDefaultInitialImage 4 整数 PICTUREBOXSTATE_waitOnLoad 16 整数 从右到左 System::Windows::Forms::RightToLeft::No System::Windows::Forms::RightToLeft SizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode TabIndex 1 整数 TabStop 假布尔 文字“”System::String^ WaitOnLoad 假布尔 borderStyle System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle 内容长度 0 整数 currentAsyncLoadOperation System::ComponentModel::AsyncOperation^ currentlyAnimating false 布尔
- defaultErrorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^
- defaultErrorImageForThread 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^ defaultErrorImageKey 0x0000026aa629af08 System::Object^
- defaultInitialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
- defaultInitialImageForThread 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^ defaultInitialImageKey 0x0000026aa629aef0 System::Object^
- errorImage 0x0000026aa629b110 { nativeImage=2657511192608 rawData= userData= } System::Drawing::Image^ handleValid false 布尔
- image 0x0000026aa62a1fa0 { nativeImage=2657511290992 rawData= userData= } System::Drawing::Image^ 图像安装类型 ImageInstallationType::DirectlySpecified 图像安装类型 图片位置 System::String^
- initialImage 0x0000026aa629c568 { nativeImage=2657511196256 rawData= userData= } System::Drawing::Image^
- [System::Drawing::Bitmap^] 0x0000026aa629c568 { defaultTransparentColor= } System::Drawing::Bitmap^
- System::MarshalByRefObject^ 0x0000026aa629c568 { __identity= } System::MarshalByRefObject^ 标志 73744 int
- FrameDimensionsList {Length=1} 数组 ^ 高度 16 int 水平分辨率 96.000000 浮动
- RED >>>>>>>调色板 0x0000026aa629ea60 { flags=951278840 entries={Length=0} } System::Drawing::Imaging::ColorPalette^
- PhysicalDimension { width=14.000000 height=16.000000 } System::Drawing::SizeF 像素格式 System::Drawing::Imaging::PixelFormat::Format24bppRgb System::Drawing::Imaging::PixelFormat PropertyIdList {Length=0} 数组 ^ PropertyItems {Length=0} 数组 ^
- RED >>>>>>> RawFormat 0x0000026aa629f900 { ...} System::Drawing::Imaging::ImageFormat^
- 尺寸{宽=14 高=16}System::Drawing::Size 标签 System::Object^ 垂直分辨率 96.000000 浮动 宽度 14 整数 nativeImage 2657511196256 __int64 原始数据数组 ^ 用户数据 System::Object^ 红色 >>>>>>> 内部同步对象 0x0000026aa629d900 System::Object^ loadCompletedDelegate System::Threading::SendOrPostCallback^ loadCompletedKey 0x0000026aa629af20 System::Object^ loadProgressChangedKey 0x0000026aa629af38 System::Object^ loadProgressDelegate System::Threading::SendOrPostCallback^ localImageStreamReader System::IO::StreamReader^
- pictureBoxState { 数据=12 } System::Collections::Specialized::BitVector32 readBlockSize 4096 整数 readBuffer 数组 ^
- savedSize {宽度=640 高度=480} System::Drawing::Size sizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode tempDownloadStream System::IO::MemoryStream^ totalBytesRead 0 整数 uriImageStream System::IO::Stream^
不得不稍微编辑一下我的代码,但我使用此处提供的答案几乎解决了我的问题:
试试这个
private: System::Void button2_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
if (button2->Text == "Stop")
{
button2->Text = "Start";
}
else if (button2->Text == "Start")
{
button2->Text = "Stop";
}
VideoCapture capture(0);
Mat frame;
while (button2->Text == "Stop")
{
capture.read(frame);
System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
System::IntPtr ptr2(frame.ptr());
System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols,
frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
System::Drawing::RectangleF rect2(0, 0, pictureBox1->Width, pictureBox1->Height);
graphics2->DrawImage(b2, rect2);
}
}