Skip to content

VideoCapture open camera slow#26601

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
dai-xin:4.x
Jan 22, 2025
Merged

VideoCapture open camera slow#26601
asmorkalov merged 1 commit intoopencv:4.xfrom
dai-xin:4.x

Conversation

@dai-xin
Copy link
Copy Markdown
Contributor

@dai-xin dai-xin commented Dec 10, 2024

Resolve the issue of slow camera opening when using dshow as the backend for VideoCapture.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work : Camera is very slow to open when using the MSMF VideoCapture backend #17687 , about DSHOW slow.
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
    dshow-code.zip
    DSHOW_test.docx

@dai-xin
Copy link
Copy Markdown
Contributor Author

dai-xin commented Dec 10, 2024

image
I believe the key issue lies here: it first creates and opens the device, and then sets the properties. Setting the properties causes the device to close and reopen, which takes a considerable amount of time. My approach is to pass the parameters directly, similar to what is done in the setProperty method.

@asmorkalov asmorkalov self-assigned this Dec 10, 2024
@asmorkalov asmorkalov added this to the 4.11.0 milestone Dec 10, 2024
@asmorkalov asmorkalov requested a review from mshabunin December 16, 2024 10:50
@asmorkalov
Copy link
Copy Markdown
Contributor

@mshabunin could you take a look?

@mshabunin
Copy link
Copy Markdown
Contributor

I'm observing difference in actual fourcc set to a stream with this patch:

Without patch:

Open time: 3323.28 ms
FPS: 0 w:1600 h:896
Actual   : 0x47504a4d
Requested: 0x47504a4d
Frame: [1600 x 896] x 3 in 1000.76 ms
Total: 4324.04 ms

With patch:

Open time: 887.593 ms
FPS: 0 w:1600 h:896
Actual   : 0xe436eb7d
Requested: 0x47504a4d
Frame: [1600 x 896] x 3 in 999.927 ms
Total: 1887.52 ms

dshow.cpp.txt

@asmorkalov asmorkalov removed this from the 4.11.0 milestone Dec 19, 2024
@asmorkalov asmorkalov added this to the 4.12.0 milestone Jan 22, 2025
@asmorkalov asmorkalov merged commit 4a4031d into opencv:4.x Jan 22, 2025
@sturkmen72
Copy link
Copy Markdown
Contributor

I was not aware that the camera could be opened with parameters. I may update code samples related to VideoCapture accordingly.

        std::vector<int> params;
        params.push_back(cv::CAP_PROP_FRAME_WIDTH);
        params.push_back(size.width);
        params.push_back(cv::CAP_PROP_FRAME_HEIGHT);
        params.push_back(size.height);
        params.push_back(cv::CAP_PROP_FOURCC);
        params.push_back(fourcc);
        cap.open(0, cv::CAP_DSHOW, params);

@asmorkalov
Copy link
Copy Markdown
Contributor

Great idea! Thanks a lot!

@asmorkalov asmorkalov mentioned this pull request Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants