您好我使用opencv从我的默认相机获取网络摄像头,我希望将其显示在Windows窗体的图片框中。我的网络摄像头出现了,但出于某种原因,我的图片框上从未显示过该Feed。因为我现在被困在这里,请有人帮助指出/解决问题。
提前谢谢。
在myform.h中,我有这个代码将picturebox发送到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&
- allocator 0x0000000000000000 cv :: MatAllocator *
cols 640 int
-
数据0x0000026AC2146F80 unsigned char *
*(* cvImage).data 0'' unsigned char
- dataend 0x0000026AC2227F80 unsigned char *
- datalimit 0x0000026AC2227F80 unsigned char *
- datastart 0x0000026AC2146F80 unsigned char *
dims 2 int
标志1124024336 int
rows 480 int
-
size {p = 0x000000DBDFF0DE80} cv :: MatSize
-
p 0x000000DBDFF0DE80 int *
*(* cvImage).size.p 0 int
- step {p = 0x000000DBDFF0DEC8 buf = {Length = 2}} cv :: MatStep
- buf {Length = 2} unsigned __int64 []
- p 0x000000DBDFF0DEC8 unsigned __int64 *
- u 0x0000026AA4814780 cv :: UMatData *
- RED>>>>>>> bmpImage 0x0000026aa62a1fd0 {defaultTransparentColor =} System :: Drawing :: Bitmap ^
- RED>>>>>>> System :: Drawing :: Image ^ 0x0000026aa62a1fd0 {nativeImage = 2657511292512 rawData = userData =} System :: Drawing :: Image ^
defaultTransparentColor 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 false bool
BorderStyle System :: Windows :: Forms :: BorderStyle :: None System :: Windows :: Forms :: BorderStyle
CausesValidation true bool
- CreateParams 0x0000026aa62a15d8 {className = caption = style = 1442840576 ...} System :: Windows :: Forms :: CreateParams ^
DefaultImeMode System :: Windows :: Forms :: ImeMode :: Disable System :: Windows :: Forms :: ImeMode
- DefaultSize {Width = 100 Height = 50} System :: Drawing :: Size
EVENT_SIZEMODECHANGED 0x0000026aa629af50 System :: Object ^
- ErrorImage 0x0000026aa629b110 {nativeImage = 2657511192608 rawData = userData =} System :: Drawing :: Image ^
- 字体0x0000026aa6280148 {}系统::绘图::字体^
- ForeColor {RGB = 0x0} System :: Drawing :: Color
- Image 0x0000026aa62a1fa0 {nativeImage = 2657511290992 rawData = userData =} System :: Drawing :: Image ^
- [System :: Drawing :: Bitmap ^] 0x0000026aa62a1fa0 {defaultTransparentColor =} System :: Drawing :: Bitmap ^
- System :: MarshalByRefObject ^ 0x0000026aa62a1fa0 {__ identity =} System :: MarshalByRefObject ^
标志2 int
- FrameDimensionsList {Length = 1} array ^
高度480 int
HorizontalResolution 96.000000 float
- Palette 0x0000026aa62a25e8 {flags = 1129621568 entries = {Length = 0}} System :: Drawing :: Imaging :: ColorPalette ^
- PhysicalDimension {width = 640.00000 height = 480.00000} System :: Drawing :: SizeF
PixelFormat系统::绘图::成像:: PixelFormat :: Format32bppArgb系统::绘图::成像:: PixelFormat
PropertyIdList {Length = 0} array ^
PropertyItems {Length = 0} array ^
- RawFormat 0x0000026aa62a3488 {...} System :: Drawing :: Imaging :: ImageFormat ^
- 尺寸{宽度= 640高度= 480}系统::绘图::尺寸
标签系统::对象^
VerticalResolution 96.000000浮动
宽度640 int
nativeImage 2657511290992 __int64
rawData数组^
userData System :: Object ^
ImageLocation System :: String ^
- ImageRectangle {X = 0 Y = 0 Width = 640 Height = 480} System :: Drawing :: Rectangle
ImeMode System :: Windows :: Forms :: ImeMode :: Disable System :: Windows :: Forms :: ImeMode
- InitialImage 0x0000026aa629c568 {nativeImage = 2657511196256 rawData = userData =} System :: Drawing :: Image ^
PICTUREBOXSTATE_asyncOperationInProgress 1 int
PICTUREBOXSTATE_cancellationPending 2 int
PICTUREBOXSTATE_inInitialization 64 int
PICTUREBOXSTATE_needToLoadImageLocation 32 int
PICTUREBOXSTATE_useDefaultErrorImage 8 int
PICTUREBOXSTATE_useDefaultInitialImage 4 int
PICTUREBOXSTATE_waitOnLoad 16 int
RightToLeft系统:: Windows :: Forms :: RightToLeft :: No System :: Windows :: Forms :: RightToLeft
SizeMode System :: Windows :: Forms :: PictureBoxSizeMode :: Normal System :: Windows :: Forms :: PictureBoxSizeMode
TabIndex 1 int
TabStop假布尔
文字""系统:字符串^
WaitOnLoad假布尔
borderStyle System :: Windows :: Forms :: BorderStyle :: None System :: Windows :: Forms :: BorderStyle
contentLength 0 int
currentAsyncLoadOperation System :: ComponentModel :: AsyncOperation ^
目前正在动画假布尔
- 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 bool
- image 0x0000026aa62a1fa0 {nativeImage = 2657511290992 rawData = userData =} System :: Drawing :: Image ^
imageInstallationType ImageInstallationType :: DirectlySpecified ImageInstallationType
imageLocation 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} array ^
高度16 int
HorizontalResolution 96.000000 float
- RED>>>>>>>>调色板0x0000026aa629ea60 {flags = 951278840 entries = {Length = 0}} System :: Drawing :: Imaging :: ColorPalette ^
- PhysicalDimension {width = 14.000000 height = 16.000000} System :: Drawing :: SizeF
PixelFormat系统::绘图::成像:: PixelFormat :: Format24bppRgb系统::绘图::成像:: PixelFormat
PropertyIdList {Length = 0} array ^
PropertyItems {Length = 0} array ^
- RED>>>>>>> RawFormat 0x0000026aa629f900 {...} System :: Drawing :: Imaging :: ImageFormat ^
- 尺寸{宽度= 14高度= 16}系统::绘图::尺寸
标签系统::对象^
VerticalResolution 96.000000浮动
宽度14 int
nativeImage 2657511196256 __int64
rawData数组^
userData System :: Object ^
RED>>>>>>>> internalSyncObject 0x0000026aa629d900 System :: Object ^
loadCompletedDelegate System :: Threading :: SendOrPostCallback ^
loadCompletedKey 0x0000026aa629af20 System :: Object ^
loadProgressChangedKey 0x0000026aa629af38 System :: Object ^
loadProgressDelegate System :: Threading :: SendOrPostCallback ^
localImageStreamReader System :: IO :: StreamReader ^
- pictureBoxState {data = 12} System :: Collections :: Specialized :: BitVector32
readBlockSize 4096 int
readBuffer数组^
- savedSize {Width = 640 Height = 480} System :: Drawing :: Size
sizeMode System :: Windows :: Forms :: PictureBoxSizeMode :: Normal System :: Windows :: Forms :: PictureBoxSizeMode
tempDownloadStream System :: IO :: MemoryStream ^
totalBytesRead 0 int
uriImageStream System :: IO :: Stream ^
2 个答案:
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个
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);
}
}