Cannot capture image from webcam / imaging device

OK I’m tearing my hair out trying to get this to work, my knowledge is very basic so I’m sure the answers out there but I’m just not joining the dots.

I’ve installed opencv 4.9 on win 11 and I’m trying to use it on VS22 in C++

I’ve gone through some of the sample code and I can load image & video files so I presume opencv is working and installed OK.
But when I come to try and capture a video stream from either a webcam or an imaging device I get one of two errors depending on which device I’m trying to use.

Error 1 - With the webcam - When calling videoCapture cap(0) the debug output indicates an issue “Error: …......\private\impl\mft\mft-framework\AsyncMFTBase.cpp(852):AsyncMFTBase(416)::GetOutputCurrentType(StreamID=0): MF_E_TRANSFORM_TYPE_NOT_SET”

Error 2 - with the imaging device (machine vision camera using an MT9M001 mono sensor) - It gives a memory error at…) I presume indicating it can’t find anything to read.

Both devices work and can be tested in OBS / Camera app / etamcap so the devices work

play around with apiPreference. CAP_MSMF and CAP_DSHOW are available.

does it work with any of those?

Thanks @crackwitz I managed to get one camera (the webcam) working with

VideoCapture cam;, CAP_DSHOW);

but if I the CAP_DSHOW for CAP_MSMF I get the same error as before.

Still can’t get the other camera to work, it does have DS drivers so I know it can be opened and controlled using DS. I have two other cameras I need to get working, one a 1.3MP colour sensor and the other is a monochrome (real mono) sensor (the MT9M001) which is a 1.2MP sensor.

the webcam is just proof of concept so while good it’s working, it’s not where I need to be.

Side note, if you are importing a real mono image does the image matrix need to be defined as grayscale prior to importing the image into it?