# Failed to open camera when using CAP\_MSMF

**URL:** <https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227>\
**Category:** C++\
**Tags:** videoio\
**Created:** [January 22, 2022, 5:26am UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227 "2022-01-22T05:26:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![westwood](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@westwood](https://forum.opencv.org/u/westwood)\
**Post date:** [January 22, 2022, 5:26am UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227/1 "2022-01-22T05:26:27Z")

</div>

Hi,  
I downloaded the official OpenCV 4.5.5 release for windows from [Releases - OpenCV](https://opencv.org/releases/).  
I wirte a very simple demo to open a usb camera, when using CAP\_ANY or CAP\_DSHOW, the camera can be opened, but when change the apiPreference to CAP\_MSMF, VideoCapture::open always return false. i have copied the opencv\_videoio\_msmf455\_64d.dll to the build dir.  
I can open the camera using the built-in Camera app in Windows or some sdk examples of microsoft media foundtaion.

below is the log when debugging the demo.

```auto
Opening camera 0, width: 1280, height: 720
[INFO:0@0.758] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (223) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[INFO:0@0.758] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (383) cv::impl::getPluginCandidates Found 2 plugin(s) for MSMF
[INFO:0@0.760] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\githubenterprise\MultiCamPrototype\out\build\x64-Debug\opencv_videoio_msmf455_64d.dll => OK
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (50) cv::impl::PluginBackend::initCaptureAPI Found entry: 'opencv_videoio_capture_plugin_init_v1'
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'Microsoft Media Foundation OpenCV Video I/O plugin': built with OpenCV 4.5 (ABI/API = 1/1), current OpenCV version is '4.5.5' (ABI/API = 1/1)
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (69) cv::impl::PluginBackend::initCaptureAPI Video I/O: plugin is ready to use 'Microsoft Media Foundation OpenCV Video I/O plugin'
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (84) cv::impl::PluginBackend::initWriterAPI Found entry: 'opencv_videoio_writer_plugin_init_v1'
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'Microsoft Media Foundation OpenCV Video I/O plugin': built with OpenCV 4.5 (ABI/API = 1/1), current OpenCV version is '4.5.5' (ABI/API = 1/1)
[INFO:0@0.760] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (103) cv::impl::PluginBackend::initWriterAPI Video I/O: plugin is ready to use 'Microsoft Media Foundation OpenCV Video I/O plugin'
Could not open camera 0

```

could someone help figure out what the reason is? thanks a lot.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 22, 2022, 11:58am UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227/2 "2022-01-22T11:58:44Z")

</div>

what can you tell about the camera?

---

<div class="post-metadata">

**Author:** ![westwood](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@westwood](https://forum.opencv.org/u/westwood)\
**Post date:** [January 22, 2022, 1:52pm UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227/3 "2022-01-22T13:52:40Z")

</div>

there are two cameras, one is the build-in camera of my notebook, another one is an external usb camera.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/bcd267e83d56a0942726a7ca1ba6f8b63983e59c.png)  
Both could be opened when using CAP\_ANY or CAP\_DSHOW. Both support 720p/30fps.  
Do you want to know other specific info? or maybe you can teach me how to fetch those info you are interested.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 22, 2022, 1:57pm UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227/4 "2022-01-22T13:57:36Z")

</div>

I would suggest that you `set OPENCV_VIDEOIO_DEBUG=1` (environment variable) and then run your program again with MSMF requested. OpenCV should be more talkative now.

if it _doesn’t_ get more talkative, verify that setting the environment variable was successful. check the value of the variable from within your program.

here’s some information on that variable: [OpenCV: Query I/O API backends registry](https://docs.opencv.org/4.x/de/db1/group __videoio__ registry.html)

check that you can open the other two cameras (built-in color and IR) using MSMF. if that works, the issue is specific to that external camera. if that doesn’t work either, that would indicate a larger issue with MSMF.

it’s likely that this is a bug in OpenCV.

it would be useful to know the _exact model_ of that camera. it’s a “hikvision” brand, which you should have known and stated already. I can guess from the screenshot of the device manager.

you say you can access the camera with DSHOW. that’s great. if you only care about using the camera, go with DSHOW then. I don’t see a reason to explicitly require MSMF.

---

<div class="post-metadata">

**Author:** ![westwood](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@westwood](https://forum.opencv.org/u/westwood)\
**Post date:** [January 22, 2022, 2:21pm UTC](https://forum.opencv.org/t/failed-to-open-camera-when-using-cap-msmf/7227/5 "2022-01-22T14:21:47Z")

</div>

> [@crackwitz](#):
>
> check that you can open the other two cameras (built-in color and IR) using MSMF

Both cameras can’t be opened when using MSMF.

> [@crackwitz](#):
>
> I would suggest that you `set OPENCV_VIDEOIO_DEBUG=1` (environment variable) and then run your program again with MSMF requested. OpenCV should be more talkative now.

After setting the env, the log contains two more lines compared with before, but doesn’t have much helpful infomation, i guess. Below is the new log.

```auto
Opening camera 0, width: 1280, height: 720
[INFO:0@1.173] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (223) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[WARN:0@1.174] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\cap.cpp (246) cv::VideoCapture::open VIDEOIO(MSMF): trying capture cameraNum=0 ...
[INFO:0@1.174] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (383) cv::impl::getPluginCandidates Found 2 plugin(s) for MSMF
[INFO:0@1.191] global c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\utils\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load D:\githubenterprise\MultiCamPrototype\out\build\x64-Debug\opencv_videoio_msmf455_64d.dll => OK
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (50) cv::impl::PluginBackend::initCaptureAPI Found entry: 'opencv_videoio_capture_plugin_init_v1'
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'Microsoft Media Foundation OpenCV Video I/O plugin': built with OpenCV 4.5 (ABI/API = 1/1), current OpenCV version is '4.5.5' (ABI/API = 1/1)
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (69) cv::impl::PluginBackend::initCaptureAPI Video I/O: plugin is ready to use 'Microsoft Media Foundation OpenCV Video I/O plugin'
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (84) cv::impl::PluginBackend::initWriterAPI Found entry: 'opencv_videoio_writer_plugin_init_v1'
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (169) cv::impl::PluginBackend::checkCompatibility Video I/O: initialized 'Microsoft Media Foundation OpenCV Video I/O plugin': built with OpenCV 4.5 (ABI/API = 1/1), current OpenCV version is '4.5.5' (ABI/API = 1/1)
[INFO:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (103) cv::impl::PluginBackend::initWriterAPI Video I/O: plugin is ready to use 'Microsoft Media Foundation OpenCV Video I/O plugin'
[WARN:0@1.191] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\cap.cpp (269) cv::VideoCapture::open VIDEOIO(MSMF): can't create capture
Could not open camera 0

```

> [@crackwitz](#):
>
> you say you can access the camera with DSHOW. that’s great. if you only care about using the camera, go with DSHOW then. I don’t see a reason to explicitly require MSMF.

The reason i want to try MSMF is because when using DSHOW, it can not set parameters when opening, it only supports set parameters one bye one after the camera is opened. Frome what i observed through the LED status of the camera and the long time used, the camera was opened several times during the process.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c56296f3dfb6247486a7d9085c10539c81a3c6b9.png)
