From 59bf2a34fa45a5d91076c724d3abc046fa85723b Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Sun, 30 Sep 2018 00:16:35 +0300 Subject: [PATCH] Update videoio --- modules/videoio/include/opencv2/videoio.hpp | 62 +++++++-------------- modules/videoio/src/cap.cpp | 57 ++++++++----------- samples/tapi/hog.cpp | 10 ++-- 3 files changed, 48 insertions(+), 81 deletions(-) diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index 6d1deed679..54f42fd9a8 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -617,14 +617,7 @@ public: CV_WRAP VideoCapture(); /** @overload - @brief Open video file or a capturing device or a IP video stream for video capturing - - Same as VideoCapture(const String& filename, int apiPreference) but using default Capture API backends - */ - CV_WRAP VideoCapture(const String& filename); - - /** @overload - @brief Open video file or a capturing device or a IP video stream for video capturing with API Preference + @brief Opens a video file or a capturing device or an IP video stream for video capturing with API Preference @param filename it can be: - name of video file (eg. `video.avi`) @@ -636,18 +629,19 @@ public: implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. @sa The list of supported API backends cv::VideoCaptureAPIs */ - CV_WRAP VideoCapture(const String& filename, int apiPreference); + CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY); /** @overload - @brief Open a camera for video capturing + @brief Opens a camera for video capturing - @param index camera_id + domain_offset (CAP_*) id of the video capturing device to open. To open default camera using default backend just pass 0. - Use a `domain_offset` to enforce a specific reader implementation if multiple are available like cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. - e.g. to open Camera 1 using the MS Media Foundation API use `index = 1 + cv::CAP_MSMF` + @param index id of the video capturing device to open. To open default camera using default backend just pass 0. + (to backward compatibility usage of camera_id + domain_offset (CAP_*) is valid when apiPreference is CAP_ANY) + @param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader + implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L. @sa The list of supported API backends cv::VideoCaptureAPIs */ - CV_WRAP VideoCapture(int index); + CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY); /** @brief Default destructor @@ -655,37 +649,27 @@ public: */ virtual ~VideoCapture(); - /** @brief Open video file or a capturing device or a IP video stream for video capturing + /** @brief Opens a video file or a capturing device or an IP video stream for video capturing. @overload - Parameters are same as the constructor VideoCapture(const String& filename) + Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY) @return `true` if the file has been successfully opened The method first calls VideoCapture::release to close the already opened file or camera. */ - CV_WRAP virtual bool open(const String& filename); + CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY); - /** @brief Open a camera for video capturing + /** @brief Opens a camera for video capturing @overload - Parameters are same as the constructor VideoCapture(int index) + Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY) @return `true` if the camera has been successfully opened. The method first calls VideoCapture::release to close the already opened file or camera. */ - CV_WRAP virtual bool open(int index); - - /** @brief Open a camera for video capturing - - @overload - - Parameters are similar as the constructor VideoCapture(int index),except it takes an additional argument apiPreference. - Definitely, is same as open(int index) where `index=cameraNum + apiPreference` - @return `true` if the camera has been successfully opened. - */ - CV_WRAP bool open(int cameraNum, int apiPreference); + CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY); /** @brief Returns true if video capturing has been initialized already. @@ -798,17 +782,6 @@ public: */ CV_WRAP virtual double get(int propId) const; - /** @brief Open video file or a capturing device or a IP video stream for video capturing with API Preference - - @overload - - Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference) - @return `true` if the file has been successfully opened - - The method first calls VideoCapture::release to close the already opened file or camera. - */ - CV_WRAP virtual bool open(const String& filename, int apiPreference); - /** @brief Returns used backend API name @note Stream should be opened. @@ -915,6 +888,11 @@ public: */ virtual VideoWriter& operator << (const Mat& image); + /** @overload + @sa write + */ + virtual VideoWriter& operator << (const UMat& image); + /** @brief Writes the next video frame @param image The written frame. In general, color images are expected in BGR format. @@ -922,7 +900,7 @@ public: The function/method writes the specified image to video file. It must have the same size as has been specified when opening the video writer. */ - CV_WRAP virtual void write(const Mat& image); + CV_WRAP virtual void write(InputArray image); /** @brief Sets a property in the VideoWriter. diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index b77f22fd43..6b3e62ad96 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -59,16 +59,10 @@ VideoCapture::VideoCapture(const String& filename, int apiPreference) open(filename, apiPreference); } -VideoCapture::VideoCapture(const String& filename) +VideoCapture::VideoCapture(int index, int apiPreference) { CV_TRACE_FUNCTION(); - open(filename, CAP_ANY); -} - -VideoCapture::VideoCapture(int index) -{ - CV_TRACE_FUNCTION(); - open(index); + open(index, apiPreference); } VideoCapture::~VideoCapture() @@ -110,19 +104,23 @@ bool VideoCapture::open(const String& filename, int apiPreference) return false; } -bool VideoCapture::open(const String& filename) -{ - CV_TRACE_FUNCTION(); - - return open(filename, CAP_ANY); -} - bool VideoCapture::open(int cameraNum, int apiPreference) { CV_TRACE_FUNCTION(); if (isOpened()) release(); + if(apiPreference==CAP_ANY) + { + // interpret preferred interface (0 = autodetect) + int backendID = (cameraNum / 100) * 100; + if (backendID) + { + cameraNum %= 100; + apiPreference = backendID; + } + } + const std::vector backends = cv::videoio_registry::getAvailableBackends_CaptureByIndex(); for (size_t i = 0; i < backends.size(); i++) { @@ -148,20 +146,6 @@ bool VideoCapture::open(int cameraNum, int apiPreference) return false; } -bool VideoCapture::open(int index) -{ - CV_TRACE_FUNCTION(); - - // interpret preferred interface (0 = autodetect) - int backendID = (index / 100) * 100; - if (backendID) - { - index %= 100; - } - - return open(index, backendID); -} - bool VideoCapture::isOpened() const { if (!icap.empty()) @@ -268,7 +252,7 @@ VideoCapture& VideoCapture::operator >> (UMat& image) bool VideoCapture::set(int propId, double value) { - CV_CheckNE(propId, (int)CAP_PROP_BACKEND, "Can set read-only property"); + CV_CheckNE(propId, (int)CAP_PROP_BACKEND, "Can't set read-only property"); if (!icap.empty()) return icap->setProperty(propId, value); @@ -367,7 +351,7 @@ bool VideoWriter::isOpened() const bool VideoWriter::set(int propId, double value) { - CV_CheckNE(propId, (int)CAP_PROP_BACKEND, "Can set read-only property"); + CV_CheckNE(propId, (int)CAP_PROP_BACKEND, "Can't set read-only property"); if (!iwriter.empty()) return iwriter->setProperty(propId, value); @@ -403,7 +387,7 @@ String VideoWriter::getBackendName() const return cv::videoio_registry::getBackendName((VideoCaptureAPIs)api); } -void VideoWriter::write(const Mat& image) +void VideoWriter::write(InputArray image) { CV_INSTRUMENT_REGION(); @@ -411,7 +395,7 @@ void VideoWriter::write(const Mat& image) iwriter->write(image); else { - IplImage _img = cvIplImage(image); + IplImage _img = cvIplImage(image.getMat()); cvWriteFrame(writer, &_img); } } @@ -424,6 +408,13 @@ VideoWriter& VideoWriter::operator << (const Mat& image) return *this; } +VideoWriter& VideoWriter::operator << (const UMat& image) +{ + CV_INSTRUMENT_REGION(); + write(image); + return *this; +} + // FIXIT OpenCV 4.0: make inline int VideoWriter::fourcc(char c1, char c2, char c3, char c4) { diff --git a/samples/tapi/hog.cpp b/samples/tapi/hog.cpp index 4753c19312..657036cfde 100644 --- a/samples/tapi/hog.cpp +++ b/samples/tapi/hog.cpp @@ -64,7 +64,7 @@ int main(int argc, char** argv) "{ v video | ../data/vtest.avi | use video as input }" "{ g gray | | convert image to gray one or not}" "{ s scale | 1.0 | resize the image before detect}" - "{ o output | | specify output path when input is images}"; + "{ o output | output.avi | specify output path when input is images}"; CommandLineParser cmd(argc, argv, keys); if (cmd.has("help")) { @@ -175,8 +175,7 @@ void App::run() throw runtime_error(string("can't open image file: " + img_source)); } - UMat img_aux, img; - Mat img_to_show; + UMat img_aux, img, img_to_show; // Iterate over all frames while (running && !frame.empty()) @@ -209,8 +208,7 @@ void App::run() // Draw positive classified windows for (size_t i = 0; i < found.size(); i++) { - Rect r = found[i]; - rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3); + rectangle(img_to_show, found[i], Scalar(0, 255, 0), 3); } putText(img_to_show, ocl::useOpenCL() ? "Mode: OpenCL" : "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2); @@ -241,7 +239,7 @@ void App::run() if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR); else cvtColor(img_to_show, img, COLOR_BGRA2BGR); - video_writer << img.getMat(ACCESS_READ); + video_writer << img; } }