diff --git a/modules/highgui/include/opencv2/highgui/highgui_c.h b/modules/highgui/include/opencv2/highgui/highgui_c.h index 35413139c7..5d20b9501e 100644 --- a/modules/highgui/include/opencv2/highgui/highgui_c.h +++ b/modules/highgui/include/opencv2/highgui/highgui_c.h @@ -44,12 +44,6 @@ #include "opencv2/core/core_c.h" #include "opencv2/imgproc/imgproc_c.h" -#ifdef HAVE_OPENCV_IMGCODECS -#include "opencv2/imgcodecs/imgcodecs_c.h" -#endif -#ifdef HAVE_OPENCV_VIDEOIO -#include "opencv2/videoio/videoio_c.h" -#endif #ifdef __cplusplus extern "C" { diff --git a/modules/highgui/src/precomp.hpp b/modules/highgui/src/precomp.hpp index 7c60064976..0f94bc7513 100644 --- a/modules/highgui/src/precomp.hpp +++ b/modules/highgui/src/precomp.hpp @@ -47,11 +47,11 @@ #include "opencv2/core/utility.hpp" #include "opencv2/core/private.hpp" +#include "opencv2/imgproc.hpp" #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/highgui/highgui_c.h" #include "opencv2/imgcodecs.hpp" -#include "opencv2/imgcodecs/imgcodecs_c.h" #include #include diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index 5c357765be..a3479a5050 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -2555,8 +2555,10 @@ void DefaultViewPort::updateImage(const CvArr* arr) } nbChannelOriginImage = cvGetElemType(mat); - - cvConvertImage(mat, image2Draw_mat, (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB); + CV_Assert(origin == 0); + cv::Mat src = cv::cvarrToMat(mat), dst = cv::cvarrToMat(image2Draw_mat); + cv::cvtColor(src, dst, cv::COLOR_BGR2RGB, dst.channels()); + CV_Assert(dst.data == image2Draw_mat->data.ptr); viewport()->update(); } @@ -3002,7 +3004,7 @@ void DefaultViewPort::drawStatusBar() if (nbChannelOriginImage==CV_8UC1) { - //all the channel have the same value (because of cvconvertimage), so only the r channel is dsplayed + //all the channel have the same value (because of cv::cvtColor(GRAY=>RGB)), so only the r channel is dsplayed centralWidget->myStatusBar_msg->setText(tr("(x=%1, y=%2) ~ ") .arg(mouseCoordinate.x()) .arg(mouseCoordinate.y())+ diff --git a/modules/highgui/src/window_cocoa.mm b/modules/highgui/src/window_cocoa.mm index 962abe09c1..3fe658a09a 100644 --- a/modules/highgui/src/window_cocoa.mm +++ b/modules/highgui/src/window_cocoa.mm @@ -41,6 +41,7 @@ // //M*/ #include "precomp.hpp" +#include "opencv2/imgproc.hpp" #import @@ -910,9 +911,8 @@ static NSSize constrainAspectRatio(NSSize base, NSSize constraint) { - (void)setImageData:(CvArr *)arr { //cout << "setImageData" << endl; NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init]; - CvMat *arrMat, dst, stub; - arrMat = cvGetMat(arr, &stub); + cv::Mat arrMat = cv::cvarrToMat(arr); /*CGColorSpaceRef colorspace = NULL; CGDataProviderRef provider = NULL; int width = cvimage->width; @@ -933,40 +933,35 @@ static NSSize constrainAspectRatio(NSSize base, NSSize constraint) { }*/ NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL - pixelsWide:arrMat->cols - pixelsHigh:arrMat->rows + pixelsWide:arrMat.cols + pixelsHigh:arrMat.rows bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat: kCGImageAlphaNone - bytesPerRow:((arrMat->cols * 3 + 3) & -4) + bytesPerRow:((arrMat.cols * 3 + 3) & -4) bitsPerPixel:24]; if (bitmap) { - cvInitMatHeader(&dst, arrMat->rows, arrMat->cols, CV_8UC3, [bitmap bitmapData], [bitmap bytesPerRow]); - cvConvertImage(arrMat, &dst, CV_CVTIMG_SWAP_RB); + cv::Mat dst(arrMat.rows, arrMat.cols, CV_8UC3, [bitmap bitmapData], [bitmap bytesPerRow]); + cv::cvtColor(arrMat, dst, cv::COLOR_BGR2RGB); } else { // It's not guaranteed to like the bitsPerPixel:24, but this is a lot slower so we'd rather not do it bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL - pixelsWide:arrMat->cols - pixelsHigh:arrMat->rows + pixelsWide:arrMat.cols + pixelsHigh:arrMat.rows bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace - bytesPerRow:(arrMat->cols * 4) + bytesPerRow:(arrMat.cols * 4) bitsPerPixel:32]; - uint8_t *data = [bitmap bitmapData]; - cvInitMatHeader(&dst, arrMat->rows, arrMat->cols, CV_8UC3, data, (arrMat->cols * 3)); - cvConvertImage(arrMat, &dst, CV_CVTIMG_SWAP_RB); - for (int i = (arrMat->rows * arrMat->cols) - 1; i >= 0; i--) { - memmove(data + i * 4, data + i * 3, 3); - data[i * 4 + 3] = 0; - } + cv::Mat dst(arrMat.rows, arrMat.cols, CV_8UC3, [bitmap bitmapData], [bitmap bytesPerRow]); + cv::cvtColor(arrMat, dst, cv::COLOR_BGR2RGBA); } if( image ) { diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index bb6397faee..77704734d3 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -140,8 +140,10 @@ void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr){ widget->original_image = cvCreateMat( mat->rows, mat->cols, CV_8UC3 ); gtk_widget_queue_resize( GTK_WIDGET( widget ) ); } - cvConvertImage( mat, widget->original_image, - (origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB ); + CV_Assert(origin == 0); + cv::Mat src = cv::cvarrToMat(arr), dst = cv::cvarrToMat(widget->original_image); + cv::cvtColor(src, dst, cv::COLOR_BGR2RGB, dst.channels()); + CV_Assert(dst.data == widget->original_image->data.ptr); if(widget->scaled_image){ cvResize( widget->original_image, widget->scaled_image, CV_INTER_AREA ); } diff --git a/modules/highgui/src/window_w32.cpp b/modules/highgui/src/window_w32.cpp index e52fcbbd2c..294f812c50 100644 --- a/modules/highgui/src/window_w32.cpp +++ b/modules/highgui/src/window_w32.cpp @@ -1155,7 +1155,7 @@ cvShowImage( const char* name, const CvArr* arr ) void* dst_ptr = 0; const int channels0 = 3; int origin = 0; - CvMat stub, dst, *image; + CvMat stub, *image; bool changed_size = false; // philipg if( !name ) @@ -1209,9 +1209,26 @@ cvShowImage( const char* name, const CvArr* arr ) DIB_RGB_COLORS, &dst_ptr, 0, 0)); } - cvInitMatHeader( &dst, size.cy, size.cx, CV_8UC3, - dst_ptr, (size.cx * channels + 3) & -4 ); - cvConvertImage( image, &dst, origin == 0 ? CV_CVTIMG_FLIP : 0 ); + { + cv::Mat src = cv::cvarrToMat(image); + cv::Mat dst(size.cy, size.cx, CV_8UC3, dst_ptr, (size.cx * channels + 3) & -4); + if (src.channels() == 1) + { + cv::cvtColor(src, dst, cv::COLOR_GRAY2BGR); + cv::flip(dst, dst, 0); + } + else if (src.channels() == 4) + { + cv::cvtColor(src, dst, cv::COLOR_BGRA2BGR); + cv::flip(dst, dst, 0); + } + else + { + CV_Assert(src.channels() == 3); + cv::flip(src, dst, 0); + } + CV_Assert(dst.data == (uchar*)dst_ptr); + } // ony resize window if needed if (changed_size) @@ -1223,86 +1240,6 @@ cvShowImage( const char* name, const CvArr* arr ) __END__; } -#if 0 -CV_IMPL void -cvShowImageHWND(HWND w_hWnd, const CvArr* arr) -{ - CV_FUNCNAME( "cvShowImageHWND" ); - - __BEGIN__; - - SIZE size = { 0, 0 }; - int channels = 0; - void* dst_ptr = 0; - const int channels0 = 3; - int origin = 0; - CvMat stub, dst, *image; - bool changed_size = false; - BITMAPINFO tempbinfo; - HDC hdc = NULL; - - if( !arr ) - EXIT; - if( !w_hWnd ) - EXIT; - - hdc = GetDC(w_hWnd); - - if( CV_IS_IMAGE_HDR( arr ) ) - origin = ((IplImage*)arr)->origin; - - CV_CALL( image = cvGetMat( arr, &stub ) ); - - if ( hdc ) - { - //GetBitmapData - BITMAP bmp; - GdiFlush(); - HGDIOBJ h = GetCurrentObject( hdc, OBJ_BITMAP ); - - if (h == NULL) - EXIT; - if (GetObject(h, sizeof(bmp), &bmp) == 0) //GetObject(): returns size of object, 0 if error - EXIT; - - channels = bmp.bmBitsPixel/8; - dst_ptr = bmp.bmBits; - } - - if( size.cx != image->width || size.cy != image->height || channels != channels0 ) - { - changed_size = true; - - uchar buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)]; - BITMAPINFO* binfo = (BITMAPINFO*)buffer; - - BOOL bDeleteObj = DeleteObject(GetCurrentObject(hdc, OBJ_BITMAP)); - CV_Assert( FALSE != bDeleteObj ); - - size.cx = image->width; - size.cy = image->height; - channels = channels0; - - FillBitmapInfo( binfo, size.cx, size.cy, channels*8, 1 ); - - SelectObject( hdc, CreateDIBSection( hdc, binfo, DIB_RGB_COLORS, &dst_ptr, 0, 0)); - } - - cvInitMatHeader( &dst, size.cy, size.cx, CV_8UC3, dst_ptr, (size.cx * channels + 3) & -4 ); - cvConvertImage( image, &dst, origin == 0 ? CV_CVTIMG_FLIP : 0 ); - - // Image stretching to fit the window - RECT rect; - GetClientRect(w_hWnd, &rect); - StretchDIBits( hdc, 0, 0, rect.right, rect.bottom, 0, 0, image->width, image->height, dst_ptr, &tempbinfo, DIB_RGB_COLORS, SRCCOPY ); - - // ony resize window if needed - InvalidateRect(w_hWnd, 0, 0); - - __END__; -} -#endif - CV_IMPL void cvResizeWindow(const char* name, int width, int height ) { CV_FUNCNAME( "cvResizeWindow" ); diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index a27d4be74e..d93d30eacc 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -53,6 +53,52 @@ @} */ +/* duplicate of "ImreadModes" enumeration for better compatibility with OpenCV 3.x */ +enum +{ +/* 8bit, color or not */ + CV_LOAD_IMAGE_UNCHANGED =-1, +/* 8bit, gray */ + CV_LOAD_IMAGE_GRAYSCALE =0, +/* ?, color */ + CV_LOAD_IMAGE_COLOR =1, +/* any depth, ? */ + CV_LOAD_IMAGE_ANYDEPTH =2, +/* ?, any color */ + CV_LOAD_IMAGE_ANYCOLOR =4, +/* ?, no rotate */ + CV_LOAD_IMAGE_IGNORE_ORIENTATION =128 +}; + +/* duplicate of "ImwriteFlags" enumeration for better compatibility with OpenCV 3.x */ +enum +{ + CV_IMWRITE_JPEG_QUALITY =1, + CV_IMWRITE_JPEG_PROGRESSIVE =2, + CV_IMWRITE_JPEG_OPTIMIZE =3, + CV_IMWRITE_JPEG_RST_INTERVAL =4, + CV_IMWRITE_JPEG_LUMA_QUALITY =5, + CV_IMWRITE_JPEG_CHROMA_QUALITY =6, + CV_IMWRITE_PNG_COMPRESSION =16, + CV_IMWRITE_PNG_STRATEGY =17, + CV_IMWRITE_PNG_BILEVEL =18, + CV_IMWRITE_PNG_STRATEGY_DEFAULT =0, + CV_IMWRITE_PNG_STRATEGY_FILTERED =1, + CV_IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2, + CV_IMWRITE_PNG_STRATEGY_RLE =3, + CV_IMWRITE_PNG_STRATEGY_FIXED =4, + CV_IMWRITE_PXM_BINARY =32, + CV_IMWRITE_EXR_TYPE = 48, + CV_IMWRITE_WEBP_QUALITY =64, + CV_IMWRITE_PAM_TUPLETYPE = 128, + CV_IMWRITE_PAM_FORMAT_NULL = 0, + CV_IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, + CV_IMWRITE_PAM_FORMAT_GRAYSCALE = 2, + CV_IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, + CV_IMWRITE_PAM_FORMAT_RGB = 4, + CV_IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, +}; + //////////////////////////////// image codec //////////////////////////////// namespace cv { @@ -250,6 +296,19 @@ CV_EXPORTS_W bool imencode( const String& ext, InputArray img, CV_OUT std::vector& buf, const std::vector& params = std::vector()); +/** @brief Returns true if the specified image can be decoded by OpenCV + +@param filename File name of the image +*/ +CV_EXPORTS_W bool haveImageReader( const String& filename ); + +/** @brief Returns true if an image with the specified filename can be encoded by OpenCV + + @param filename File name of the image + */ +CV_EXPORTS_W bool haveImageWriter( const String& filename ); + + //! @} imgcodecs } // cv diff --git a/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h b/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h deleted file mode 100644 index c36dac3343..0000000000 --- a/modules/imgcodecs/include/opencv2/imgcodecs/imgcodecs_c.h +++ /dev/null @@ -1,149 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// Intel License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000, Intel Corporation, all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of Intel Corporation may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef OPENCV_IMGCODECS_H -#define OPENCV_IMGCODECS_H - -#include "opencv2/core/core_c.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -/** @addtogroup imgcodecs_c - @{ - */ - -enum -{ -/* 8bit, color or not */ - CV_LOAD_IMAGE_UNCHANGED =-1, -/* 8bit, gray */ - CV_LOAD_IMAGE_GRAYSCALE =0, -/* ?, color */ - CV_LOAD_IMAGE_COLOR =1, -/* any depth, ? */ - CV_LOAD_IMAGE_ANYDEPTH =2, -/* ?, any color */ - CV_LOAD_IMAGE_ANYCOLOR =4, -/* ?, no rotate */ - CV_LOAD_IMAGE_IGNORE_ORIENTATION =128 -}; - -/* load image from file - iscolor can be a combination of above flags where CV_LOAD_IMAGE_UNCHANGED - overrides the other flags - using CV_LOAD_IMAGE_ANYCOLOR alone is equivalent to CV_LOAD_IMAGE_UNCHANGED - unless CV_LOAD_IMAGE_ANYDEPTH is specified images are converted to 8bit -*/ -CVAPI(IplImage*) cvLoadImage( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR)); -CVAPI(CvMat*) cvLoadImageM( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR)); - -enum -{ - CV_IMWRITE_JPEG_QUALITY =1, - CV_IMWRITE_JPEG_PROGRESSIVE =2, - CV_IMWRITE_JPEG_OPTIMIZE =3, - CV_IMWRITE_JPEG_RST_INTERVAL =4, - CV_IMWRITE_JPEG_LUMA_QUALITY =5, - CV_IMWRITE_JPEG_CHROMA_QUALITY =6, - CV_IMWRITE_PNG_COMPRESSION =16, - CV_IMWRITE_PNG_STRATEGY =17, - CV_IMWRITE_PNG_BILEVEL =18, - CV_IMWRITE_PNG_STRATEGY_DEFAULT =0, - CV_IMWRITE_PNG_STRATEGY_FILTERED =1, - CV_IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY =2, - CV_IMWRITE_PNG_STRATEGY_RLE =3, - CV_IMWRITE_PNG_STRATEGY_FIXED =4, - CV_IMWRITE_PXM_BINARY =32, - CV_IMWRITE_EXR_TYPE = 48, - CV_IMWRITE_WEBP_QUALITY =64, - CV_IMWRITE_PAM_TUPLETYPE = 128, - CV_IMWRITE_PAM_FORMAT_NULL = 0, - CV_IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, - CV_IMWRITE_PAM_FORMAT_GRAYSCALE = 2, - CV_IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, - CV_IMWRITE_PAM_FORMAT_RGB = 4, - CV_IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, -}; - - - -/* save image to file */ -CVAPI(int) cvSaveImage( const char* filename, const CvArr* image, - const int* params CV_DEFAULT(0) ); - -/* decode image stored in the buffer */ -CVAPI(IplImage*) cvDecodeImage( const CvMat* buf, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR)); -CVAPI(CvMat*) cvDecodeImageM( const CvMat* buf, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR)); - -/* encode image and store the result as a byte vector (single-row 8uC1 matrix) */ -CVAPI(CvMat*) cvEncodeImage( const char* ext, const CvArr* image, - const int* params CV_DEFAULT(0) ); - -enum -{ - CV_CVTIMG_FLIP =1, - CV_CVTIMG_SWAP_RB =2 -}; - -/* utility function: convert one image to another with optional vertical flip */ -CVAPI(void) cvConvertImage( const CvArr* src, CvArr* dst, int flags CV_DEFAULT(0)); - -CVAPI(int) cvHaveImageReader(const char* filename); -CVAPI(int) cvHaveImageWriter(const char* filename); - - -/****************************************************************************************\ -* Obsolete functions/synonyms * -\****************************************************************************************/ - -#define cvvLoadImage(name) cvLoadImage((name),1) -#define cvvSaveImage cvSaveImage -#define cvvConvertImage cvConvertImage - -/** @} imgcodecs_c */ - -#ifdef __cplusplus -} -#endif - -#endif // OPENCV_IMGCODECS_H diff --git a/modules/imgcodecs/src/grfmt_base.cpp b/modules/imgcodecs/src/grfmt_base.cpp index b7032c1723..574ce9e87a 100644 --- a/modules/imgcodecs/src/grfmt_base.cpp +++ b/modules/imgcodecs/src/grfmt_base.cpp @@ -142,7 +142,7 @@ void BaseImageEncoder::throwOnEror() const if(!m_last_error.empty()) { String msg = "Raw image encoder error: " + m_last_error; - CV_Error( CV_BadImageSize, msg.c_str() ); + CV_Error( Error::BadImageSize, msg.c_str() ); } } diff --git a/modules/imgcodecs/src/grfmt_bmp.cpp b/modules/imgcodecs/src/grfmt_bmp.cpp index 798b2d9446..f20f186660 100644 --- a/modules/imgcodecs/src/grfmt_bmp.cpp +++ b/modules/imgcodecs/src/grfmt_bmp.cpp @@ -55,7 +55,7 @@ BmpDecoder::BmpDecoder() m_signature = fmtSignBmp; m_offset = -1; m_buf_supported = true; - m_origin = 0; + m_origin = ORIGIN_TL; m_bpp = 0; m_rle_code = BMP_RGB; } @@ -179,7 +179,7 @@ bool BmpDecoder::readHeader() } // in 32 bit case alpha channel is used - so require CV_8UC4 type m_type = iscolor ? (m_bpp == 32 ? CV_8UC4 : CV_8UC3 ) : CV_8UC1; - m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL; + m_origin = m_height > 0 ? ORIGIN_BL : ORIGIN_TL; m_height = std::abs(m_height); if( !result ) @@ -206,7 +206,7 @@ bool BmpDecoder::readData( Mat& img ) if( m_offset < 0 || !m_strm.isOpened()) return false; - if( m_origin == IPL_ORIGIN_BL ) + if( m_origin == ORIGIN_BL ) { data += (m_height - 1)*(size_t)step; step = -step; @@ -238,7 +238,7 @@ bool BmpDecoder::readData( Mat& img ) m_strm.getBytes( src, src_pitch ); FillColorRow1( color ? data : bgr, src, m_width, m_palette ); if( !color ) - icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR2Gray_8u_C3C1R( bgr, 0, data, 0, Size(m_width,1) ); } result = true; break; @@ -441,9 +441,9 @@ decode_rle8_bad: ; { m_strm.getBytes( src, src_pitch ); if( !color ) - icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR5552Gray_8u_C2C1R( src, 0, data, 0, Size(m_width,1) ); else - icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR5552BGR_8u_C2C3R( src, 0, data, 0, Size(m_width,1) ); } result = true; break; @@ -453,9 +453,9 @@ decode_rle8_bad: ; { m_strm.getBytes( src, src_pitch ); if( !color ) - icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR5652Gray_8u_C2C1R( src, 0, data, 0, Size(m_width,1) ); else - icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR5652BGR_8u_C2C3R( src, 0, data, 0, Size(m_width,1) ); } result = true; break; @@ -465,7 +465,7 @@ decode_rle8_bad: ; { m_strm.getBytes( src, src_pitch ); if(!color) - icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, Size(m_width,1) ); else memcpy( data, src, m_width*3 ); } @@ -478,9 +478,9 @@ decode_rle8_bad: ; m_strm.getBytes( src, src_pitch ); if( !color ) - icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_BGRA2Gray_8u_C4C1R( src, 0, data, 0, Size(m_width,1) ); else if( img.channels() == 3 ) - icvCvt_BGRA2BGR_8u_C4C3R(src, 0, data, 0, cvSize(m_width, 1)); + icvCvt_BGRA2BGR_8u_C4C3R(src, 0, data, 0, Size(m_width, 1)); else if( img.channels() == 4 ) memcpy(data, src, m_width * 4); } diff --git a/modules/imgcodecs/src/grfmt_bmp.hpp b/modules/imgcodecs/src/grfmt_bmp.hpp index ccbaa9140d..e94b8ff8b4 100644 --- a/modules/imgcodecs/src/grfmt_bmp.hpp +++ b/modules/imgcodecs/src/grfmt_bmp.hpp @@ -73,9 +73,15 @@ public: protected: + enum Origin + { + ORIGIN_TL = 0, + ORIGIN_BL = 1 + }; + RLByteStream m_strm; PaletteEntry m_palette[256]; - int m_origin; + Origin m_origin; int m_bpp; int m_offset; BmpCompression m_rle_code; diff --git a/modules/imgcodecs/src/grfmt_jpeg.cpp b/modules/imgcodecs/src/grfmt_jpeg.cpp index 917c72eae3..76c3249da3 100644 --- a/modules/imgcodecs/src/grfmt_jpeg.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg.cpp @@ -453,16 +453,16 @@ bool JpegDecoder::readData( Mat& img ) if( color ) { if( cinfo->out_color_components == 3 ) - icvCvt_RGB2BGR_8u_C3R( buffer[0], 0, data, 0, cvSize(m_width,1) ); + icvCvt_RGB2BGR_8u_C3R( buffer[0], 0, data, 0, Size(m_width,1) ); else - icvCvt_CMYK2BGR_8u_C4C3R( buffer[0], 0, data, 0, cvSize(m_width,1) ); + icvCvt_CMYK2BGR_8u_C4C3R( buffer[0], 0, data, 0, Size(m_width,1) ); } else { if( cinfo->out_color_components == 1 ) memcpy( data, buffer[0], m_width ); else - icvCvt_CMYK2Gray_8u_C4C1R( buffer[0], 0, data, 0, cvSize(m_width,1) ); + icvCvt_CMYK2Gray_8u_C4C1R( buffer[0], 0, data, 0, Size(m_width,1) ); } } @@ -689,12 +689,12 @@ bool JpegEncoder::write( const Mat& img, const std::vector& params ) if( _channels == 3 ) { - icvCvt_BGR2RGB_8u_C3R( data, 0, buffer, 0, cvSize(width,1) ); + icvCvt_BGR2RGB_8u_C3R( data, 0, buffer, 0, Size(width,1) ); ptr = buffer; } else if( _channels == 4 ) { - icvCvt_BGRA2BGR_8u_C4C3R( data, 0, buffer, 0, cvSize(width,1), 2 ); + icvCvt_BGRA2BGR_8u_C4C3R( data, 0, buffer, 0, Size(width,1), 2 ); ptr = buffer; } diff --git a/modules/imgcodecs/src/grfmt_pam.cpp b/modules/imgcodecs/src/grfmt_pam.cpp index d1a2161733..a0dc171f23 100644 --- a/modules/imgcodecs/src/grfmt_pam.cpp +++ b/modules/imgcodecs/src/grfmt_pam.cpp @@ -132,12 +132,12 @@ rgb_convert (void *src, void *target, int width, int target_channels, int target switch (target_depth) { case CV_8U: icvCvt_RGB2BGR_8u_C3R( (uchar*) src, 0, (uchar*) target, 0, - cvSize(width,1) ); + Size(width,1) ); ret = true; break; case CV_16U: icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)target, 0, - cvSize(width,1) ); + Size(width,1) ); ret = true; break; default: @@ -147,12 +147,12 @@ rgb_convert (void *src, void *target, int width, int target_channels, int target switch (target_depth) { case CV_8U: icvCvt_BGR2Gray_8u_C3C1R( (uchar*) src, 0, (uchar*) target, 0, - cvSize(width,1), 2 ); + Size(width,1), 2 ); ret = true; break; case CV_16U: icvCvt_BGRA2Gray_16u_CnC1R( (ushort *)src, 0, (ushort *)target, 0, - cvSize(width,1), 3, 2 ); + Size(width,1), 3, 2 ); ret = true; break; default: diff --git a/modules/imgcodecs/src/grfmt_pxm.cpp b/modules/imgcodecs/src/grfmt_pxm.cpp index b41fd95edc..1289ee45cb 100644 --- a/modules/imgcodecs/src/grfmt_pxm.cpp +++ b/modules/imgcodecs/src/grfmt_pxm.cpp @@ -342,14 +342,14 @@ bool PxMDecoder::readData( Mat& img ) if( color ) { if( img.depth() == CV_8U ) - icvCvt_RGB2BGR_8u_C3R( src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_RGB2BGR_8u_C3R( src, 0, data, 0, Size(m_width,1) ); else - icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1) ); + icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)data, 0, Size(m_width,1) ); } else if( img.depth() == CV_8U ) - icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1), 2 ); + icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, Size(m_width,1), 2 ); else - icvCvt_BGRA2Gray_16u_CnC1R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1), 3, 2 ); + icvCvt_BGRA2Gray_16u_CnC1R( (ushort *)src, 0, (ushort *)data, 0, Size(m_width,1), 3, 2 ); } } result = true; @@ -522,10 +522,10 @@ bool PxMEncoder::write(const Mat& img, const std::vector& params) { if( depth == 8 ) icvCvt_BGR2RGB_8u_C3R( (const uchar*)data, 0, - (uchar*)buffer, 0, cvSize(width,1) ); + (uchar*)buffer, 0, Size(width,1) ); else icvCvt_BGR2RGB_16u_C3R( (const ushort*)data, 0, - (ushort*)buffer, 0, cvSize(width,1) ); + (ushort*)buffer, 0, Size(width,1) ); } // swap endianness if necessary diff --git a/modules/imgcodecs/src/grfmt_sunras.cpp b/modules/imgcodecs/src/grfmt_sunras.cpp index 4865edaa26..2d342be82d 100644 --- a/modules/imgcodecs/src/grfmt_sunras.cpp +++ b/modules/imgcodecs/src/grfmt_sunras.cpp @@ -343,13 +343,13 @@ bad_decoding_end: if( color ) { if( m_type == RAS_FORMAT_RGB ) - icvCvt_RGB2BGR_8u_C3R(src, 0, data, 0, cvSize(m_width,1) ); + icvCvt_RGB2BGR_8u_C3R(src, 0, data, 0, Size(m_width,1) ); else memcpy(data, src, std::min(step, (size_t)src_pitch)); } else { - icvCvt_BGR2Gray_8u_C3C1R(src, 0, data, 0, cvSize(m_width,1), + icvCvt_BGR2Gray_8u_C3C1R(src, 0, data, 0, Size(m_width,1), m_type == RAS_FORMAT_RGB ? 2 : 0 ); } } @@ -364,10 +364,10 @@ bad_decoding_end: m_strm.getBytes( src + 3, src_pitch ); if( color ) - icvCvt_BGRA2BGR_8u_C4C3R( src + 4, 0, data, 0, cvSize(m_width,1), + icvCvt_BGRA2BGR_8u_C4C3R( src + 4, 0, data, 0, Size(m_width,1), m_type == RAS_FORMAT_RGB ? 2 : 0 ); else - icvCvt_BGRA2Gray_8u_C4C1R( src + 4, 0, data, 0, cvSize(m_width,1), + icvCvt_BGRA2Gray_8u_C4C1R( src + 4, 0, data, 0, Size(m_width,1), m_type == RAS_FORMAT_RGB ? 2 : 0 ); } result = true; diff --git a/modules/imgcodecs/src/grfmt_tiff.cpp b/modules/imgcodecs/src/grfmt_tiff.cpp index b83ab1068a..f4b360e27d 100644 --- a/modules/imgcodecs/src/grfmt_tiff.cpp +++ b/modules/imgcodecs/src/grfmt_tiff.cpp @@ -408,19 +408,19 @@ bool TiffDecoder::readData( Mat& img ) { icvCvt_BGRA2RGBA_8u_C4R( bstart + i*tile_width0*4, 0, data + x*4 + img.step*(tile_height - i - 1), 0, - cvSize(tile_width,1) ); + Size(tile_width,1) ); } else { icvCvt_BGRA2BGR_8u_C4C3R( bstart + i*tile_width0*4, 0, data + x*3 + img.step*(tile_height - i - 1), 0, - cvSize(tile_width,1), 2 ); + Size(tile_width,1), 2 ); } } else icvCvt_BGRA2Gray_8u_C4C1R( bstart + i*tile_width0*4, 0, data + x + img.step*(tile_height - i - 1), 0, - cvSize(tile_width,1), 2 ); + Size(tile_width,1), 2 ); break; } @@ -445,13 +445,13 @@ bool TiffDecoder::readData( Mat& img ) { icvCvt_Gray2BGR_16u_C1C3R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x*3, 0, - cvSize(tile_width,1) ); + Size(tile_width,1) ); } else if( ncn == 3 ) { icvCvt_RGB2BGR_16u_C3R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x*3, 0, - cvSize(tile_width,1) ); + Size(tile_width,1) ); } else if (ncn == 4) { @@ -459,20 +459,20 @@ bool TiffDecoder::readData( Mat& img ) { icvCvt_BGRA2RGBA_16u_C4R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x * 4, 0, - cvSize(tile_width, 1)); + Size(tile_width, 1)); } else { icvCvt_BGRA2BGR_16u_C4C3R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x * 3, 0, - cvSize(tile_width, 1), 2); + Size(tile_width, 1), 2); } } else { icvCvt_BGRA2BGR_16u_C4C3R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x*3, 0, - cvSize(tile_width,1), 2 ); + Size(tile_width,1), 2 ); } } else @@ -487,7 +487,7 @@ bool TiffDecoder::readData( Mat& img ) { icvCvt_BGRA2Gray_16u_CnC1R(buffer16 + i*tile_width0*ncn, 0, (ushort*)(data + img.step*i) + x, 0, - cvSize(tile_width,1), ncn, 2 ); + Size(tile_width,1), ncn, 2 ); } } } @@ -859,18 +859,18 @@ bool TiffEncoder::writeLibTiff( const std::vector& img_vec, const std::vect case 3: { if (depth == CV_8U) - icvCvt_BGR2RGB_8u_C3R( img.ptr(y), 0, buffer, 0, cvSize(width, 1)); + icvCvt_BGR2RGB_8u_C3R( img.ptr(y), 0, buffer, 0, Size(width, 1)); else - icvCvt_BGR2RGB_16u_C3R( img.ptr(y), 0, (ushort*)buffer, 0, cvSize(width, 1)); + icvCvt_BGR2RGB_16u_C3R( img.ptr(y), 0, (ushort*)buffer, 0, Size(width, 1)); break; } case 4: { if (depth == CV_8U) - icvCvt_BGRA2RGBA_8u_C4R( img.ptr(y), 0, buffer, 0, cvSize(width, 1)); + icvCvt_BGRA2RGBA_8u_C4R( img.ptr(y), 0, buffer, 0, Size(width, 1)); else - icvCvt_BGRA2RGBA_16u_C4R( img.ptr(y), 0, (ushort*)buffer, 0, cvSize(width, 1)); + icvCvt_BGRA2RGBA_16u_C4R( img.ptr(y), 0, (ushort*)buffer, 0, Size(width, 1)); break; } diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index 77b0ff124a..24ff0489f0 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -301,8 +301,6 @@ static ImageEncoder findEncoder( const String& _ext ) } -enum { LOAD_CVMAT=0, LOAD_IMAGE=1, LOAD_MAT=2 }; - static void ExifTransform(int orientation, Mat& img) { switch( orientation ) @@ -397,15 +395,9 @@ static void ApplyExifOrientation(const Mat& buf, Mat& img) * @param[in] scale_denom Scale value * */ -static void* -imread_( const String& filename, int flags, int hdrtype, Mat* mat=0 ) +static bool +imread_( const String& filename, int flags, Mat& mat ) { - CV_Assert(mat || hdrtype != LOAD_MAT); // mat is required in LOAD_MAT case - - IplImage* image = 0; - CvMat *matrix = 0; - Mat temp, *data = &temp; - /// Search for the relevant decoder to handle the imagery ImageDecoder decoder; @@ -476,30 +468,13 @@ imread_( const String& filename, int flags, int hdrtype, Mat* mat=0 ) type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1); } - if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT ) - { - if( hdrtype == LOAD_CVMAT ) - { - matrix = cvCreateMat( size.height, size.width, type ); - temp = cvarrToMat( matrix ); - } - else - { - mat->create( size.height, size.width, type ); - data = mat; - } - } - else - { - image = cvCreateImage(cvSize(size), cvIplDepth(type), CV_MAT_CN(type)); - temp = cvarrToMat( image ); - } + mat.create( size.height, size.width, type ); // read the image data bool success = false; CV_TRY { - if (decoder->readData(*data)) + if (decoder->readData(mat)) success = true; } CV_CATCH (cv::Exception, e) @@ -512,20 +487,16 @@ imread_( const String& filename, int flags, int hdrtype, Mat* mat=0 ) } if (!success) { - cvReleaseImage( &image ); - cvReleaseMat( &matrix ); - if( mat ) - mat->release(); - return 0; + mat.release(); + return false; } if( decoder->setScale( scale_denom ) > 1 ) // if decoder is JpegDecoder then decoder->setScale always returns 1 { - resize( *mat, *mat, Size( size.width / scale_denom, size.height / scale_denom ), 0, 0, INTER_LINEAR_EXACT); + resize( mat, mat, Size( size.width / scale_denom, size.height / scale_denom ), 0, 0, INTER_LINEAR_EXACT); } - return hdrtype == LOAD_CVMAT ? (void*)matrix : - hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat; + return true; } @@ -650,7 +621,7 @@ Mat imread( const String& filename, int flags ) Mat img; /// load the data - imread_( filename, flags, LOAD_MAT, &img ); + imread_( filename, flags, img ); /// optionally rotate the data if EXIF' orientation flag says so if( !img.empty() && (flags & IMREAD_IGNORE_ORIENTATION) == 0 && flags != IMREAD_UNCHANGED ) @@ -687,7 +658,7 @@ static bool imwrite_( const String& filename, const std::vector& img_vec, ImageEncoder encoder = findEncoder( filename ); if( !encoder ) - CV_Error( CV_StsError, "could not find a writer for the specified extension" ); + CV_Error( Error::StsError, "could not find a writer for the specified extension" ); for (size_t page = 0; page < img_vec.size(); page++) { @@ -748,13 +719,10 @@ bool imwrite( const String& filename, InputArray _img, return imwrite_(filename, img_vec, params, false); } -static void* -imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) +static bool +imdecode_( const Mat& buf, int flags, Mat& mat ) { CV_Assert(!buf.empty() && buf.isContinuous()); - IplImage* image = 0; - CvMat *matrix = 0; - Mat temp, *data = &temp; String filename; ImageDecoder decoder = findDecoder(buf); @@ -771,11 +739,11 @@ imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) if( fwrite( buf.ptr(), 1, bufSize, f ) != bufSize ) { fclose( f ); - CV_Error( CV_StsError, "failed to write image data to temporary file" ); + CV_Error( Error::StsError, "failed to write image data to temporary file" ); } if( fclose(f) != 0 ) { - CV_Error( CV_StsError, "failed to write image data to temporary file" ); + CV_Error( Error::StsError, "failed to write image data to temporary file" ); } decoder->setSource(filename); } @@ -823,29 +791,12 @@ imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1); } - if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT ) - { - if( hdrtype == LOAD_CVMAT ) - { - matrix = cvCreateMat( size.height, size.width, type ); - temp = cvarrToMat(matrix); - } - else - { - mat->create( size.height, size.width, type ); - data = mat; - } - } - else - { - image = cvCreateImage(cvSize(size), cvIplDepth(type), CV_MAT_CN(type)); - temp = cvarrToMat(image); - } + mat.create( size.height, size.width, type ); success = false; CV_TRY { - if (decoder->readData(*data)) + if (decoder->readData(mat)) success = true; } CV_CATCH (cv::Exception, e) @@ -867,15 +818,11 @@ imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) if (!success) { - cvReleaseImage( &image ); - cvReleaseMat( &matrix ); - if( mat ) - mat->release(); - return 0; + mat.release(); + return false; } - return hdrtype == LOAD_CVMAT ? (void*)matrix : - hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat; + return true; } @@ -884,7 +831,7 @@ Mat imdecode( InputArray _buf, int flags ) CV_TRACE_FUNCTION(); Mat buf = _buf.getMat(), img; - imdecode_( buf, flags, LOAD_MAT, &img ); + imdecode_( buf, flags, img ); /// optionally rotate the data if EXIF' orientation flag says so if( !img.empty() && (flags & IMREAD_IGNORE_ORIENTATION) == 0 && flags != IMREAD_UNCHANGED ) @@ -901,7 +848,7 @@ Mat imdecode( InputArray _buf, int flags, Mat* dst ) Mat buf = _buf.getMat(), img; dst = dst ? dst : &img; - imdecode_( buf, flags, LOAD_MAT, dst ); + imdecode_( buf, flags, *dst ); /// optionally rotate the data if EXIF' orientation flag says so if( !dst->empty() && (flags & IMREAD_IGNORE_ORIENTATION) == 0 && flags != IMREAD_UNCHANGED ) @@ -924,7 +871,7 @@ bool imencode( const String& ext, InputArray _image, ImageEncoder encoder = findEncoder( ext ); if( !encoder ) - CV_Error( CV_StsError, "could not find encoder for the specified extension" ); + CV_Error( Error::StsError, "could not find encoder for the specified extension" ); if( !encoder->isFormatSupported(image.depth()) ) { @@ -964,94 +911,18 @@ bool imencode( const String& ext, InputArray _image, return code; } -} - -/****************************************************************************************\ -* Imgcodecs loading & saving function implementation * -\****************************************************************************************/ - -CV_IMPL int -cvHaveImageReader( const char* filename ) +bool haveImageReader( const String& filename ) { - cv::ImageDecoder decoder = cv::findDecoder(filename); + ImageDecoder decoder = cv::findDecoder(filename); return !decoder.empty(); } -CV_IMPL int cvHaveImageWriter( const char* filename ) +bool haveImageWriter( const String& filename ) { cv::ImageEncoder encoder = cv::findEncoder(filename); return !encoder.empty(); } -CV_IMPL IplImage* -cvLoadImage( const char* filename, int iscolor ) -{ - return (IplImage*)cv::imread_(filename, iscolor, cv::LOAD_IMAGE ); -} - -CV_IMPL CvMat* -cvLoadImageM( const char* filename, int iscolor ) -{ - return (CvMat*)cv::imread_( filename, iscolor, cv::LOAD_CVMAT ); -} - -CV_IMPL int -cvSaveImage( const char* filename, const CvArr* arr, const int* _params ) -{ - int i = 0; - if( _params ) - { - for( ; _params[i] > 0; i += 2 ) - CV_Assert(i < CV_IO_MAX_IMAGE_PARAMS*2); // Limit number of params for security reasons - } - return cv::imwrite_(filename, cv::cvarrToMat(arr), - i > 0 ? std::vector(_params, _params+i) : std::vector(), - CV_IS_IMAGE(arr) && ((const IplImage*)arr)->origin == IPL_ORIGIN_BL ); -} - -/* decode image stored in the buffer */ -CV_IMPL IplImage* -cvDecodeImage( const CvMat* _buf, int iscolor ) -{ - CV_Assert( _buf && CV_IS_MAT_CONT(_buf->type) ); - cv::Mat buf(1, _buf->rows*_buf->cols*CV_ELEM_SIZE(_buf->type), CV_8U, _buf->data.ptr); - return (IplImage*)cv::imdecode_(buf, iscolor, cv::LOAD_IMAGE ); -} - -CV_IMPL CvMat* -cvDecodeImageM( const CvMat* _buf, int iscolor ) -{ - CV_Assert( _buf && CV_IS_MAT_CONT(_buf->type) ); - cv::Mat buf(1, _buf->rows*_buf->cols*CV_ELEM_SIZE(_buf->type), CV_8U, _buf->data.ptr); - return (CvMat*)cv::imdecode_(buf, iscolor, cv::LOAD_CVMAT ); -} - -CV_IMPL CvMat* -cvEncodeImage( const char* ext, const CvArr* arr, const int* _params ) -{ - int i = 0; - if( _params ) - { - for( ; _params[i] > 0; i += 2 ) - CV_Assert(i < CV_IO_MAX_IMAGE_PARAMS*2); // Limit number of params for security reasons - } - cv::Mat img = cv::cvarrToMat(arr); - if( CV_IS_IMAGE(arr) && ((const IplImage*)arr)->origin == IPL_ORIGIN_BL ) - { - cv::Mat temp; - cv::flip(img, temp, 0); - img = temp; - } - std::vector buf; - - bool code = cv::imencode(ext, img, buf, - i > 0 ? std::vector(_params, _params+i) : std::vector() ); - if( !code ) - return 0; - CvMat* _buf = cvCreateMat(1, (int)buf.size(), CV_8U); - memcpy( _buf->data.ptr, &buf[0], buf.size() ); - - return _buf; } /* End of file. */ diff --git a/modules/imgcodecs/src/precomp.hpp b/modules/imgcodecs/src/precomp.hpp index c280b08ce2..f79336b820 100644 --- a/modules/imgcodecs/src/precomp.hpp +++ b/modules/imgcodecs/src/precomp.hpp @@ -48,8 +48,6 @@ #include "opencv2/core/private.hpp" #include "opencv2/imgproc.hpp" -#include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/imgcodecs/imgcodecs_c.h" #include #include diff --git a/modules/imgcodecs/src/utils.cpp b/modules/imgcodecs/src/utils.cpp index 6aeb631060..4eea8b351f 100644 --- a/modules/imgcodecs/src/utils.cpp +++ b/modules/imgcodecs/src/utils.cpp @@ -42,6 +42,9 @@ #include "precomp.hpp" #include "utils.hpp" +namespace cv +{ + int validateToInt(size_t sz) { int valueInt = (int)sz; @@ -56,7 +59,7 @@ int validateToInt(size_t sz) void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step, uchar* gray, int gray_step, - CvSize size, int _swap_rb ) + Size size, int _swap_rb ) { int i; int swap_rb = _swap_rb ? 2 : 0; @@ -75,7 +78,7 @@ void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step, void icvCvt_BGRA2Gray_16u_CnC1R( const ushort* rgb, int rgb_step, ushort* gray, int gray_step, - CvSize size, int ncn, int _swap_rb ) + Size size, int ncn, int _swap_rb ) { int i; int swap_rb = _swap_rb ? 2 : 0; @@ -94,7 +97,7 @@ void icvCvt_BGRA2Gray_16u_CnC1R( const ushort* rgb, int rgb_step, void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step, uchar* gray, int gray_step, - CvSize size, int _swap_rb ) + Size size, int _swap_rb ) { int i; int swap_rb = _swap_rb ? 2 : 0; @@ -112,7 +115,7 @@ void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step, void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step, - uchar* bgr, int bgr_step, CvSize size ) + uchar* bgr, int bgr_step, Size size ) { int i; for( ; size.height--; gray += gray_step ) @@ -127,7 +130,7 @@ void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step, void icvCvt_Gray2BGR_16u_C1C3R( const ushort* gray, int gray_step, - ushort* bgr, int bgr_step, CvSize size ) + ushort* bgr, int bgr_step, Size size ) { int i; for( ; size.height--; gray += gray_step/sizeof(gray[0]) ) @@ -143,7 +146,7 @@ void icvCvt_Gray2BGR_16u_C1C3R( const ushort* gray, int gray_step, void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step, uchar* bgr, int bgr_step, - CvSize size, int _swap_rb ) + Size size, int _swap_rb ) { int i; int swap_rb = _swap_rb ? 2 : 0; @@ -163,7 +166,7 @@ void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step, void icvCvt_BGRA2BGR_16u_C4C3R( const ushort* bgra, int bgra_step, ushort* bgr, int bgr_step, - CvSize size, int _swap_rb ) + Size size, int _swap_rb ) { int i; int swap_rb = _swap_rb ? 2 : 0; @@ -182,7 +185,7 @@ void icvCvt_BGRA2BGR_16u_C4C3R( const ushort* bgra, int bgra_step, void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step, - uchar* rgba, int rgba_step, CvSize size ) + uchar* rgba, int rgba_step, Size size ) { int i; for( ; size.height--; ) @@ -200,7 +203,7 @@ void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step, } void icvCvt_BGRA2RGBA_16u_C4R( const ushort* bgra, int bgra_step, - ushort* rgba, int rgba_step, CvSize size ) + ushort* rgba, int rgba_step, Size size ) { int i; for( ; size.height--; ) @@ -220,7 +223,7 @@ void icvCvt_BGRA2RGBA_16u_C4R( const ushort* bgra, int bgra_step, void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step, - uchar* rgb, int rgb_step, CvSize size ) + uchar* rgb, int rgb_step, Size size ) { int i; for( ; size.height--; ) @@ -237,7 +240,7 @@ void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step, void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step, - ushort* rgb, int rgb_step, CvSize size ) + ushort* rgb, int rgb_step, Size size ) { int i; for( ; size.height--; ) @@ -256,7 +259,7 @@ void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step, typedef unsigned short ushort; void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step, - uchar* gray, int gray_step, CvSize size ) + uchar* gray, int gray_step, Size size ) { int i; for( ; size.height--; gray += gray_step, bgr555 += bgr555_step ) @@ -273,7 +276,7 @@ void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step, void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step, - uchar* gray, int gray_step, CvSize size ) + uchar* gray, int gray_step, Size size ) { int i; for( ; size.height--; gray += gray_step, bgr565 += bgr565_step ) @@ -290,7 +293,7 @@ void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step, void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step, - uchar* bgr, int bgr_step, CvSize size ) + uchar* bgr, int bgr_step, Size size ) { int i; for( ; size.height--; bgr555 += bgr555_step ) @@ -308,7 +311,7 @@ void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step, void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step, - uchar* bgr, int bgr_step, CvSize size ) + uchar* bgr, int bgr_step, Size size ) { int i; for( ; size.height--; bgr565 += bgr565_step ) @@ -326,7 +329,7 @@ void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step, void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step, - uchar* bgr, int bgr_step, CvSize size ) + uchar* bgr, int bgr_step, Size size ) { int i; for( ; size.height--; ) @@ -346,7 +349,7 @@ void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step, void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* cmyk, int cmyk_step, - uchar* gray, int gray_step, CvSize size ) + uchar* gray, int gray_step, Size size ) { int i; for( ; size.height--; ) @@ -371,7 +374,7 @@ void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entr int i; for( i = 0; i < entries; i++ ) { - icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, cvSize(1,1) ); + icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, Size(1,1) ); } } @@ -598,103 +601,4 @@ uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette ) return data; } - -CV_IMPL void -cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags ) -{ - CvMat* temp = 0; - - CV_FUNCNAME( "cvConvertImage" ); - - __BEGIN__; - - CvMat srcstub, *src; - CvMat dststub, *dst; - int src_cn, dst_cn, swap_rb = flags & CV_CVTIMG_SWAP_RB; - - CV_CALL( src = cvGetMat( srcarr, &srcstub )); - CV_CALL( dst = cvGetMat( dstarr, &dststub )); - - src_cn = CV_MAT_CN( src->type ); - dst_cn = CV_MAT_CN( dst->type ); - - if( src_cn != 1 && src_cn != 3 && src_cn != 4 ) - CV_ERROR( CV_BadNumChannels, "Source image must have 1, 3 or 4 channels" ); - - if( CV_MAT_DEPTH( dst->type ) != CV_8U ) - CV_ERROR( CV_BadDepth, "Destination image must be 8u" ); - - if( CV_MAT_CN(dst->type) != 1 && CV_MAT_CN(dst->type) != 3 ) - CV_ERROR( CV_BadNumChannels, "Destination image must have 1 or 3 channels" ); - - if( !CV_ARE_DEPTHS_EQ( src, dst )) - { - int src_depth = CV_MAT_DEPTH(src->type); - double scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255; - double shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0; - - if( !CV_ARE_CNS_EQ( src, dst )) - { - temp = cvCreateMat( src->height, src->width, - (src->type & CV_MAT_CN_MASK)|(dst->type & CV_MAT_DEPTH_MASK)); - cvConvertScale( src, temp, scale, shift ); - src = temp; - } - else - { - cvConvertScale( src, dst, scale, shift ); - src = dst; - } - } - - if( src_cn != dst_cn || (src_cn == 3 && swap_rb) ) - { - uchar *s = src->data.ptr, *d = dst->data.ptr; - int s_step = src->step, d_step = dst->step; - int code = src_cn*10 + dst_cn; - CvSize size = {src->cols, src->rows}; - - if( CV_IS_MAT_CONT(src->type & dst->type) ) - { - size.width *= size.height; - size.height = 1; - s_step = d_step = /*CV_STUB_STEP*/ (1 << 30); - } - - switch( code ) - { - case 13: - icvCvt_Gray2BGR_8u_C1C3R( s, s_step, d, d_step, size ); - break; - case 31: - icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb ); - break; - case 33: - CV_Assert(swap_rb); - icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size ); - break; - case 41: - icvCvt_BGRA2Gray_8u_C4C1R( s, s_step, d, d_step, size, swap_rb ); - break; - case 43: - icvCvt_BGRA2BGR_8u_C4C3R( s, s_step, d, d_step, size, swap_rb ); - break; - default: - CV_ERROR( CV_StsUnsupportedFormat, "Unsupported combination of input/output formats" ); - } - src = dst; - } - - if( flags & CV_CVTIMG_FLIP ) - { - CV_CALL( cvFlip( src, dst, 0 )); - } - else if( src != dst ) - { - CV_CALL( cvCopy( src, dst )); - } - - __END__; - - cvReleaseMat( &temp ); } diff --git a/modules/imgcodecs/src/utils.hpp b/modules/imgcodecs/src/utils.hpp index 6e0ec95826..70cbc448a0 100644 --- a/modules/imgcodecs/src/utils.hpp +++ b/modules/imgcodecs/src/utils.hpp @@ -42,6 +42,9 @@ #ifndef _UTILS_H_ #define _UTILS_H_ +namespace cv +{ + int validateToInt(size_t step); template static inline @@ -68,53 +71,53 @@ struct PaletteEntry void icvCvt_BGR2Gray_8u_C3C1R( const uchar* bgr, int bgr_step, uchar* gray, int gray_step, - CvSize size, int swap_rb=0 ); + Size size, int swap_rb=0 ); void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* bgra, int bgra_step, uchar* gray, int gray_step, - CvSize size, int swap_rb=0 ); + Size size, int swap_rb=0 ); void icvCvt_BGRA2Gray_16u_CnC1R( const ushort* bgra, int bgra_step, ushort* gray, int gray_step, - CvSize size, int ncn, int swap_rb=0 ); + Size size, int ncn, int swap_rb=0 ); void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step, - uchar* bgr, int bgr_step, CvSize size ); + uchar* bgr, int bgr_step, Size size ); void icvCvt_Gray2BGR_16u_C1C3R( const ushort* gray, int gray_step, - ushort* bgr, int bgr_step, CvSize size ); + ushort* bgr, int bgr_step, Size size ); void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step, uchar* bgr, int bgr_step, - CvSize size, int swap_rb=0 ); + Size size, int swap_rb=0 ); void icvCvt_BGRA2BGR_16u_C4C3R( const ushort* bgra, int bgra_step, ushort* bgr, int bgr_step, - CvSize size, int _swap_rb ); + Size size, int _swap_rb ); void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step, - uchar* rgb, int rgb_step, CvSize size ); + uchar* rgb, int rgb_step, Size size ); #define icvCvt_RGB2BGR_8u_C3R icvCvt_BGR2RGB_8u_C3R void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step, - ushort* rgb, int rgb_step, CvSize size ); + ushort* rgb, int rgb_step, Size size ); #define icvCvt_RGB2BGR_16u_C3R icvCvt_BGR2RGB_16u_C3R void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step, - uchar* rgba, int rgba_step, CvSize size ); + uchar* rgba, int rgba_step, Size size ); #define icvCvt_RGBA2BGRA_8u_C4R icvCvt_BGRA2RGBA_8u_C4R void icvCvt_BGRA2RGBA_16u_C4R( const ushort* bgra, int bgra_step, - ushort* rgba, int rgba_step, CvSize size ); + ushort* rgba, int rgba_step, Size size ); #define icvCvt_RGBA2BGRA_16u_C4R icvCvt_BGRA2RGBA_16u_C4R void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step, - uchar* gray, int gray_step, CvSize size ); + uchar* gray, int gray_step, Size size ); void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step, - uchar* gray, int gray_step, CvSize size ); + uchar* gray, int gray_step, Size size ); void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step, - uchar* bgr, int bgr_step, CvSize size ); + uchar* bgr, int bgr_step, Size size ); void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step, - uchar* bgr, int bgr_step, CvSize size ); + uchar* bgr, int bgr_step, Size size ); void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step, - uchar* bgr, int bgr_step, CvSize size ); + uchar* bgr, int bgr_step, Size size ); void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* ycck, int ycck_step, - uchar* gray, int gray_step, CvSize size ); + uchar* gray, int gray_step, Size size ); void FillGrayPalette( PaletteEntry* palette, int bpp, bool negative = false ); bool IsColorPalette( PaletteEntry* palette, int bpp ); @@ -136,4 +139,6 @@ CV_INLINE bool isBigEndian( void ) return (((const int*)"\0\x1\x2\x3\x4\x5\x6\x7")[0] & 255) != 0; } +} + #endif/*_UTILS_H_*/ diff --git a/modules/imgproc/src/color.cpp b/modules/imgproc/src/color.cpp index 38d35c014d..4370339bd4 100644 --- a/modules/imgproc/src/color.cpp +++ b/modules/imgproc/src/color.cpp @@ -191,7 +191,10 @@ void cvtColor( InputArray _src, OutputArray _dst, int code, int dcn ) { case COLOR_BGR2BGRA: case COLOR_RGB2BGRA: case COLOR_BGRA2BGR: case COLOR_RGBA2BGR: case COLOR_RGB2BGR: case COLOR_BGRA2RGBA: - cvtColorBGR2BGR(_src, _dst, dcn, swapBlue(code)); + if(_src.channels() == 1) + cvtColorGray2BGR(_src, _dst, dcn); + else + cvtColorBGR2BGR(_src, _dst, dcn, swapBlue(code)); break; case COLOR_BGR2BGR565: case COLOR_BGR2BGR555: case COLOR_BGRA2BGR565: case COLOR_BGRA2BGR555: diff --git a/modules/photo/include/opencv2/photo.hpp b/modules/photo/include/opencv2/photo.hpp index 083811971e..eac35fcad2 100644 --- a/modules/photo/include/opencv2/photo.hpp +++ b/modules/photo/include/opencv2/photo.hpp @@ -61,6 +61,12 @@ camera calibration with multiple exposures and exposure fusion. @} */ +enum InpaintingModes +{ + CV_INPAINT_NS =0, + CV_INPAINT_TELEA =1 +}; + namespace cv { diff --git a/modules/photo/include/opencv2/photo/photo_c.h b/modules/photo/include/opencv2/photo/photo_c.h deleted file mode 100644 index cd623c1913..0000000000 --- a/modules/photo/include/opencv2/photo/photo_c.h +++ /dev/null @@ -1,74 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef OPENCV_PHOTO_C_H -#define OPENCV_PHOTO_C_H - -#include "opencv2/core/core_c.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** @addtogroup photo_c - @{ - */ - -/* Inpainting algorithms */ -enum InpaintingModes -{ - CV_INPAINT_NS =0, - CV_INPAINT_TELEA =1 -}; - - -/* Inpaints the selected region in the image */ -CVAPI(void) cvInpaint( const CvArr* src, const CvArr* inpaint_mask, - CvArr* dst, double inpaintRange, int flags ); - -/** @} */ - -#ifdef __cplusplus -} //extern "C" -#endif - -#endif //OPENCV_PHOTO_C_H diff --git a/modules/photo/src/inpaint.cpp b/modules/photo/src/inpaint.cpp index 03011e0c13..cadeccef1b 100644 --- a/modules/photo/src/inpaint.cpp +++ b/modules/photo/src/inpaint.cpp @@ -47,7 +47,6 @@ #include "precomp.hpp" #include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/photo/photo_c.h" #undef CV_MAT_ELEM_PTR_FAST #define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size ) \ @@ -727,8 +726,8 @@ namespace cv { template<> struct DefaultDeleter{ void operator ()(IplConvKernel* obj) const { cvReleaseStructuringElement(&obj); } }; } -void -cvInpaint( const CvArr* _input_img, const CvArr* _inpaint_mask, CvArr* _output_img, +static void +icvInpaint( const CvArr* _input_img, const CvArr* _inpaint_mask, CvArr* _output_img, double inpaintRange, int flags ) { cv::Ptr mask, band, f, t, out; @@ -847,5 +846,5 @@ void cv::inpaint( InputArray _src, InputArray _mask, OutputArray _dst, _dst.create( src.size(), src.type() ); Mat dst = _dst.getMat(); CvMat c_src = cvMat(src), c_mask = cvMat(mask), c_dst = cvMat(dst); - cvInpaint( &c_src, &c_mask, &c_dst, inpaintRange, flags ); + icvInpaint( &c_src, &c_mask, &c_dst, inpaintRange, flags ); } diff --git a/modules/video/include/opencv2/video/tracking.hpp b/modules/video/include/opencv2/video/tracking.hpp index 228548924b..d0a53357b3 100644 --- a/modules/video/include/opencv2/video/tracking.hpp +++ b/modules/video/include/opencv2/video/tracking.hpp @@ -47,6 +47,14 @@ #include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" +enum +{ + CV_LKFLOW_PYR_A_READY = 1, + CV_LKFLOW_PYR_B_READY = 2, + CV_LKFLOW_INITIAL_GUESSES = 4, + CV_LKFLOW_GET_MIN_EIGENVALS = 8 +}; + namespace cv { diff --git a/modules/video/include/opencv2/video/tracking_c.h b/modules/video/include/opencv2/video/tracking_c.h deleted file mode 100644 index 740a9fae12..0000000000 --- a/modules/video/include/opencv2/video/tracking_c.h +++ /dev/null @@ -1,228 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef OPENCV_TRACKING_C_H -#define OPENCV_TRACKING_C_H - -#include "opencv2/imgproc/types_c.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** @addtogroup video_c - @{ -*/ - -/****************************************************************************************\ -* Motion Analysis * -\****************************************************************************************/ - -/************************************ optical flow ***************************************/ - -#define CV_LKFLOW_PYR_A_READY 1 -#define CV_LKFLOW_PYR_B_READY 2 -#define CV_LKFLOW_INITIAL_GUESSES 4 -#define CV_LKFLOW_GET_MIN_EIGENVALS 8 - -/* It is Lucas & Kanade method, modified to use pyramids. - Also it does several iterations to get optical flow for - every point at every pyramid level. - Calculates optical flow between two images for certain set of points (i.e. - it is a "sparse" optical flow, which is opposite to the previous 3 methods) */ -CVAPI(void) cvCalcOpticalFlowPyrLK( const CvArr* prev, const CvArr* curr, - CvArr* prev_pyr, CvArr* curr_pyr, - const CvPoint2D32f* prev_features, - CvPoint2D32f* curr_features, - int count, - CvSize win_size, - int level, - char* status, - float* track_error, - CvTermCriteria criteria, - int flags ); - - -/* Modification of a previous sparse optical flow algorithm to calculate - affine flow */ -CVAPI(void) cvCalcAffineFlowPyrLK( const CvArr* prev, const CvArr* curr, - CvArr* prev_pyr, CvArr* curr_pyr, - const CvPoint2D32f* prev_features, - CvPoint2D32f* curr_features, - float* matrices, int count, - CvSize win_size, int level, - char* status, float* track_error, - CvTermCriteria criteria, int flags ); - -/* Estimate optical flow for each pixel using the two-frame G. Farneback algorithm */ -CVAPI(void) cvCalcOpticalFlowFarneback( const CvArr* prev, const CvArr* next, - CvArr* flow, double pyr_scale, int levels, - int winsize, int iterations, int poly_n, - double poly_sigma, int flags ); - -/********************************* motion templates *************************************/ - -/****************************************************************************************\ -* All the motion template functions work only with single channel images. * -* Silhouette image must have depth IPL_DEPTH_8U or IPL_DEPTH_8S * -* Motion history image must have depth IPL_DEPTH_32F, * -* Gradient mask - IPL_DEPTH_8U or IPL_DEPTH_8S, * -* Motion orientation image - IPL_DEPTH_32F * -* Segmentation mask - IPL_DEPTH_32F * -* All the angles are in degrees, all the times are in milliseconds * -\****************************************************************************************/ - -/* Updates motion history image given motion silhouette */ -CVAPI(void) cvUpdateMotionHistory( const CvArr* silhouette, CvArr* mhi, - double timestamp, double duration ); - -/* Calculates gradient of the motion history image and fills - a mask indicating where the gradient is valid */ -CVAPI(void) cvCalcMotionGradient( const CvArr* mhi, CvArr* mask, CvArr* orientation, - double delta1, double delta2, - int aperture_size CV_DEFAULT(3)); - -/* Calculates average motion direction within a selected motion region - (region can be selected by setting ROIs and/or by composing a valid gradient mask - with the region mask) */ -CVAPI(double) cvCalcGlobalOrientation( const CvArr* orientation, const CvArr* mask, - const CvArr* mhi, double timestamp, - double duration ); - -/* Splits a motion history image into a few parts corresponding to separate independent motions - (e.g. left hand, right hand) */ -CVAPI(CvSeq*) cvSegmentMotion( const CvArr* mhi, CvArr* seg_mask, - CvMemStorage* storage, - double timestamp, double seg_thresh ); - -/****************************************************************************************\ -* Tracking * -\****************************************************************************************/ - -/* Implements CAMSHIFT algorithm - determines object position, size and orientation - from the object histogram back project (extension of meanshift) */ -CVAPI(int) cvCamShift( const CvArr* prob_image, CvRect window, - CvTermCriteria criteria, CvConnectedComp* comp, - CvBox2D* box CV_DEFAULT(NULL) ); - -/* Implements MeanShift algorithm - determines object position - from the object histogram back project */ -CVAPI(int) cvMeanShift( const CvArr* prob_image, CvRect window, - CvTermCriteria criteria, CvConnectedComp* comp ); - -/* -standard Kalman filter (in G. Welch' and G. Bishop's notation): - - x(k)=A*x(k-1)+B*u(k)+w(k) p(w)~N(0,Q) - z(k)=H*x(k)+v(k), p(v)~N(0,R) -*/ -typedef struct CvKalman -{ - int MP; /* number of measurement vector dimensions */ - int DP; /* number of state vector dimensions */ - int CP; /* number of control vector dimensions */ - - /* backward compatibility fields */ -#if 1 - float* PosterState; /* =state_pre->data.fl */ - float* PriorState; /* =state_post->data.fl */ - float* DynamMatr; /* =transition_matrix->data.fl */ - float* MeasurementMatr; /* =measurement_matrix->data.fl */ - float* MNCovariance; /* =measurement_noise_cov->data.fl */ - float* PNCovariance; /* =process_noise_cov->data.fl */ - float* KalmGainMatr; /* =gain->data.fl */ - float* PriorErrorCovariance;/* =error_cov_pre->data.fl */ - float* PosterErrorCovariance;/* =error_cov_post->data.fl */ - float* Temp1; /* temp1->data.fl */ - float* Temp2; /* temp2->data.fl */ -#endif - - CvMat* state_pre; /* predicted state (x'(k)): - x(k)=A*x(k-1)+B*u(k) */ - CvMat* state_post; /* corrected state (x(k)): - x(k)=x'(k)+K(k)*(z(k)-H*x'(k)) */ - CvMat* transition_matrix; /* state transition matrix (A) */ - CvMat* control_matrix; /* control matrix (B) - (it is not used if there is no control)*/ - CvMat* measurement_matrix; /* measurement matrix (H) */ - CvMat* process_noise_cov; /* process noise covariance matrix (Q) */ - CvMat* measurement_noise_cov; /* measurement noise covariance matrix (R) */ - CvMat* error_cov_pre; /* priori error estimate covariance matrix (P'(k)): - P'(k)=A*P(k-1)*At + Q)*/ - CvMat* gain; /* Kalman gain matrix (K(k)): - K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)*/ - CvMat* error_cov_post; /* posteriori error estimate covariance matrix (P(k)): - P(k)=(I-K(k)*H)*P'(k) */ - CvMat* temp1; /* temporary matrices */ - CvMat* temp2; - CvMat* temp3; - CvMat* temp4; - CvMat* temp5; -} CvKalman; - -/* Creates Kalman filter and sets A, B, Q, R and state to some initial values */ -CVAPI(CvKalman*) cvCreateKalman( int dynam_params, int measure_params, - int control_params CV_DEFAULT(0)); - -/* Releases Kalman filter state */ -CVAPI(void) cvReleaseKalman( CvKalman** kalman); - -/* Updates Kalman filter by time (predicts future state of the system) */ -CVAPI(const CvMat*) cvKalmanPredict( CvKalman* kalman, - const CvMat* control CV_DEFAULT(NULL)); - -/* Updates Kalman filter by measurement - (corrects state of the system and internal matrices) */ -CVAPI(const CvMat*) cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement ); - -#define cvKalmanUpdateByTime cvKalmanPredict -#define cvKalmanUpdateByMeasurement cvKalmanCorrect - -/** @} video_c */ - -#ifdef __cplusplus -} // extern "C" -#endif - - -#endif // OPENCV_TRACKING_C_H diff --git a/modules/video/src/compat_video.cpp b/modules/video/src/compat_video.cpp deleted file mode 100644 index da3996a012..0000000000 --- a/modules/video/src/compat_video.cpp +++ /dev/null @@ -1,301 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000, Intel Corporation, all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" -#include "opencv2/video/tracking_c.h" - - -/////////////////////////// Meanshift & CAMShift /////////////////////////// - -CV_IMPL int -cvMeanShift( const void* imgProb, CvRect windowIn, - CvTermCriteria criteria, CvConnectedComp* comp ) -{ - cv::Mat img = cv::cvarrToMat(imgProb); - cv::Rect window = windowIn; - int iters = cv::meanShift(img, window, criteria); - - if( comp ) - { - comp->rect = cvRect(window); - comp->area = cvRound(cv::sum(img(window))[0]); - } - - return iters; -} - - -CV_IMPL int -cvCamShift( const void* imgProb, CvRect windowIn, - CvTermCriteria criteria, - CvConnectedComp* comp, - CvBox2D* box ) -{ - cv::Mat img = cv::cvarrToMat(imgProb); - cv::Rect window = windowIn; - cv::RotatedRect rr = cv::CamShift(img, window, criteria); - - if( comp ) - { - comp->rect = cvRect(window); - cv::Rect roi = rr.boundingRect() & cv::Rect(0, 0, img.cols, img.rows); - comp->area = cvRound(cv::sum(img(roi))[0]); - } - - if( box ) - *box = cvBox2D(rr); - - return rr.size.width*rr.size.height > 0.f ? 1 : -1; -} - -///////////////////////////////// Kalman /////////////////////////////// - -CV_IMPL CvKalman* -cvCreateKalman( int DP, int MP, int CP ) -{ - CvKalman *kalman = 0; - - if( DP <= 0 || MP <= 0 ) - CV_Error( CV_StsOutOfRange, - "state and measurement vectors must have positive number of dimensions" ); - - if( CP < 0 ) - CP = DP; - - /* allocating memory for the structure */ - kalman = (CvKalman *)cvAlloc( sizeof( CvKalman )); - memset( kalman, 0, sizeof(*kalman)); - - kalman->DP = DP; - kalman->MP = MP; - kalman->CP = CP; - - kalman->state_pre = cvCreateMat( DP, 1, CV_32FC1 ); - cvZero( kalman->state_pre ); - - kalman->state_post = cvCreateMat( DP, 1, CV_32FC1 ); - cvZero( kalman->state_post ); - - kalman->transition_matrix = cvCreateMat( DP, DP, CV_32FC1 ); - cvSetIdentity( kalman->transition_matrix ); - - kalman->process_noise_cov = cvCreateMat( DP, DP, CV_32FC1 ); - cvSetIdentity( kalman->process_noise_cov ); - - kalman->measurement_matrix = cvCreateMat( MP, DP, CV_32FC1 ); - cvZero( kalman->measurement_matrix ); - - kalman->measurement_noise_cov = cvCreateMat( MP, MP, CV_32FC1 ); - cvSetIdentity( kalman->measurement_noise_cov ); - - kalman->error_cov_pre = cvCreateMat( DP, DP, CV_32FC1 ); - - kalman->error_cov_post = cvCreateMat( DP, DP, CV_32FC1 ); - cvZero( kalman->error_cov_post ); - - kalman->gain = cvCreateMat( DP, MP, CV_32FC1 ); - - if( CP > 0 ) - { - kalman->control_matrix = cvCreateMat( DP, CP, CV_32FC1 ); - cvZero( kalman->control_matrix ); - } - - kalman->temp1 = cvCreateMat( DP, DP, CV_32FC1 ); - kalman->temp2 = cvCreateMat( MP, DP, CV_32FC1 ); - kalman->temp3 = cvCreateMat( MP, MP, CV_32FC1 ); - kalman->temp4 = cvCreateMat( MP, DP, CV_32FC1 ); - kalman->temp5 = cvCreateMat( MP, 1, CV_32FC1 ); - -#if 1 - kalman->PosterState = kalman->state_pre->data.fl; - kalman->PriorState = kalman->state_post->data.fl; - kalman->DynamMatr = kalman->transition_matrix->data.fl; - kalman->MeasurementMatr = kalman->measurement_matrix->data.fl; - kalman->MNCovariance = kalman->measurement_noise_cov->data.fl; - kalman->PNCovariance = kalman->process_noise_cov->data.fl; - kalman->KalmGainMatr = kalman->gain->data.fl; - kalman->PriorErrorCovariance = kalman->error_cov_pre->data.fl; - kalman->PosterErrorCovariance = kalman->error_cov_post->data.fl; -#endif - - return kalman; -} - - -CV_IMPL void -cvReleaseKalman( CvKalman** _kalman ) -{ - CvKalman *kalman; - - if( !_kalman ) - CV_Error( CV_StsNullPtr, "" ); - - kalman = *_kalman; - if( !kalman ) - return; - - /* freeing the memory */ - cvReleaseMat( &kalman->state_pre ); - cvReleaseMat( &kalman->state_post ); - cvReleaseMat( &kalman->transition_matrix ); - cvReleaseMat( &kalman->control_matrix ); - cvReleaseMat( &kalman->measurement_matrix ); - cvReleaseMat( &kalman->process_noise_cov ); - cvReleaseMat( &kalman->measurement_noise_cov ); - cvReleaseMat( &kalman->error_cov_pre ); - cvReleaseMat( &kalman->gain ); - cvReleaseMat( &kalman->error_cov_post ); - cvReleaseMat( &kalman->temp1 ); - cvReleaseMat( &kalman->temp2 ); - cvReleaseMat( &kalman->temp3 ); - cvReleaseMat( &kalman->temp4 ); - cvReleaseMat( &kalman->temp5 ); - - memset( kalman, 0, sizeof(*kalman)); - - /* deallocating the structure */ - cvFree( _kalman ); -} - - -CV_IMPL const CvMat* -cvKalmanPredict( CvKalman* kalman, const CvMat* control ) -{ - if( !kalman ) - CV_Error( CV_StsNullPtr, "" ); - - /* update the state */ - /* x'(k) = A*x(k) */ - cvMatMulAdd( kalman->transition_matrix, kalman->state_post, 0, kalman->state_pre ); - - if( control && kalman->CP > 0 ) - /* x'(k) = x'(k) + B*u(k) */ - cvMatMulAdd( kalman->control_matrix, control, kalman->state_pre, kalman->state_pre ); - - /* update error covariance matrices */ - /* temp1 = A*P(k) */ - cvMatMulAdd( kalman->transition_matrix, kalman->error_cov_post, 0, kalman->temp1 ); - - /* P'(k) = temp1*At + Q */ - cvGEMM( kalman->temp1, kalman->transition_matrix, 1, kalman->process_noise_cov, 1, - kalman->error_cov_pre, CV_GEMM_B_T ); - - /* handle the case when there will be measurement before the next predict */ - cvCopy(kalman->state_pre, kalman->state_post); - - return kalman->state_pre; -} - - -CV_IMPL const CvMat* -cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement ) -{ - if( !kalman || !measurement ) - CV_Error( CV_StsNullPtr, "" ); - - /* temp2 = H*P'(k) */ - cvMatMulAdd( kalman->measurement_matrix, kalman->error_cov_pre, 0, kalman->temp2 ); - /* temp3 = temp2*Ht + R */ - cvGEMM( kalman->temp2, kalman->measurement_matrix, 1, - kalman->measurement_noise_cov, 1, kalman->temp3, CV_GEMM_B_T ); - - /* temp4 = inv(temp3)*temp2 = Kt(k) */ - cvSolve( kalman->temp3, kalman->temp2, kalman->temp4, CV_SVD ); - - /* K(k) */ - cvTranspose( kalman->temp4, kalman->gain ); - - /* temp5 = z(k) - H*x'(k) */ - cvGEMM( kalman->measurement_matrix, kalman->state_pre, -1, measurement, 1, kalman->temp5 ); - - /* x(k) = x'(k) + K(k)*temp5 */ - cvMatMulAdd( kalman->gain, kalman->temp5, kalman->state_pre, kalman->state_post ); - - /* P(k) = P'(k) - K(k)*temp2 */ - cvGEMM( kalman->gain, kalman->temp2, -1, kalman->error_cov_pre, 1, - kalman->error_cov_post, 0 ); - - return kalman->state_post; -} - -///////////////////////////////////// Optical Flow //////////////////////////////// - -CV_IMPL void -cvCalcOpticalFlowPyrLK( const void* arrA, const void* arrB, - void* /*pyrarrA*/, void* /*pyrarrB*/, - const CvPoint2D32f * featuresA, - CvPoint2D32f * featuresB, - int count, CvSize winSize, int level, - char *status, float *error, - CvTermCriteria criteria, int flags ) -{ - if( count <= 0 ) - return; - CV_Assert( featuresA && featuresB ); - cv::Mat A = cv::cvarrToMat(arrA), B = cv::cvarrToMat(arrB); - cv::Mat ptA(count, 1, CV_32FC2, (void*)featuresA); - cv::Mat ptB(count, 1, CV_32FC2, (void*)featuresB); - cv::Mat st, err; - - if( status ) - st = cv::Mat(count, 1, CV_8U, (void*)status); - if( error ) - err = cv::Mat(count, 1, CV_32F, (void*)error); - cv::calcOpticalFlowPyrLK( A, B, ptA, ptB, st, - error ? cv::_OutputArray(err) : (cv::_OutputArray)cv::noArray(), - winSize, level, criteria, flags); -} - - -CV_IMPL void cvCalcOpticalFlowFarneback( - const CvArr* _prev, const CvArr* _next, - CvArr* _flow, double pyr_scale, int levels, - int winsize, int iterations, int poly_n, - double poly_sigma, int flags ) -{ - cv::Mat prev = cv::cvarrToMat(_prev), next = cv::cvarrToMat(_next); - cv::Mat flow = cv::cvarrToMat(_flow); - CV_Assert( flow.size() == prev.size() && flow.type() == CV_32FC2 ); - cv::calcOpticalFlowFarneback( prev, next, flow, pyr_scale, levels, - winsize, iterations, poly_n, poly_sigma, flags ); -} diff --git a/modules/video/test/test_camshift.cpp b/modules/video/test/test_camshift.cpp index dc6275f408..24d2ec6c24 100644 --- a/modules/video/test/test_camshift.cpp +++ b/modules/video/test/test_camshift.cpp @@ -40,10 +40,12 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/video/tracking_c.h" +#include "opencv2/video/tracking.hpp" namespace opencv_test { namespace { +using namespace cv; + class CV_TrackBaseTest : public cvtest::BaseTest { public: @@ -59,10 +61,10 @@ protected: void generate_object(); int min_log_size, max_log_size; - CvMat* img; - CvBox2D box0; - CvSize img_size; - CvTermCriteria criteria; + Mat img; + RotatedRect box0; + Size img_size; + TermCriteria criteria; int img_type; }; @@ -84,7 +86,7 @@ CV_TrackBaseTest::~CV_TrackBaseTest() void CV_TrackBaseTest::clear() { - cvReleaseMat( &img ); + img.release(); cvtest::BaseTest::clear(); } @@ -103,8 +105,7 @@ int CV_TrackBaseTest::read_params( const cv::FileStorage& fs ) max_log_size = cvtest::clipInt( max_log_size, 1, 10 ); if( min_log_size > max_log_size ) { - int t; - CV_SWAP( min_log_size, max_log_size, t ); + std::swap( min_log_size, max_log_size ); } return 0; @@ -122,13 +123,12 @@ void CV_TrackBaseTest::generate_object() double a = sin(angle), b = -cos(angle); double inv_ww = 1./(width*width), inv_hh = 1./(height*height); - img = cvCreateMat( img_size.height, img_size.width, img_type ); - cvZero( img ); + img = Mat::zeros( img_size.height, img_size.width, img_type ); // use the straightforward algorithm: for every pixel check if it is inside the ellipse for( y = 0; y < img_size.height; y++ ) { - uchar* ptr = img->data.ptr + img->step*y; + uchar* ptr = img.ptr(y); float* fl = (float*)ptr; double x_ = (y - cy)*b, y_ = (y - cy)*a; @@ -163,8 +163,7 @@ int CV_TrackBaseTest::prepare_test_case( int test_case_idx ) if( box0.size.width > box0.size.height ) { - float t; - CV_SWAP( box0.size.width, box0.size.height, t ); + std::swap( box0.size.width, box0.size.height ); } m = MAX( box0.size.width, box0.size.height ); @@ -176,7 +175,7 @@ int CV_TrackBaseTest::prepare_test_case( int test_case_idx ) box0.center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - m)); box0.center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - m)); - criteria = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1 ); + criteria = TermCriteria( TermCriteria::EPS + TermCriteria::MAX_ITER, 10, 0.1 ); generate_object(); @@ -209,9 +208,8 @@ protected: int validate_test_results( int test_case_idx ); void generate_object(); - CvBox2D box; - CvRect init_rect; - CvConnectedComp comp; + RotatedRect box; + Rect init_rect; int area0; }; @@ -231,12 +229,10 @@ int CV_CamShiftTest::prepare_test_case( int test_case_idx ) if( code <= 0 ) return code; - area0 = cvCountNonZero(img); + area0 = countNonZero(img); for(i = 0; i < 100; i++) { - CvMat temp; - m = MAX(box0.size.width,box0.size.height)*0.8; init_rect.x = cvFloor(box0.center.x - m*(0.45 + cvtest::randReal(rng)*0.2)); init_rect.y = cvFloor(box0.center.y - m*(0.45 + cvtest::randReal(rng)*0.2)); @@ -248,8 +244,8 @@ int CV_CamShiftTest::prepare_test_case( int test_case_idx ) init_rect.y + init_rect.height >= img_size.height ) continue; - cvGetSubRect( img, &temp, init_rect ); - area = cvCountNonZero( &temp ); + Mat temp = img(init_rect); + area = countNonZero( temp ); if( area >= 0.1*area0 ) break; @@ -261,7 +257,7 @@ int CV_CamShiftTest::prepare_test_case( int test_case_idx ) void CV_CamShiftTest::run_func(void) { - cvCamShift( img, init_rect, criteria, &comp, &box ); + box = CamShift( img, init_rect, criteria ); } @@ -269,15 +265,14 @@ int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ ) { int code = cvtest::TS::OK; - double m = MAX(box0.size.width, box0.size.height), delta; + double m = MAX(box0.size.width, box0.size.height); double diff_angle; if( cvIsNaN(box.size.width) || cvIsInf(box.size.width) || box.size.width <= 0 || cvIsNaN(box.size.height) || cvIsInf(box.size.height) || box.size.height <= 0 || cvIsNaN(box.center.x) || cvIsInf(box.center.x) || cvIsNaN(box.center.y) || cvIsInf(box.center.y) || - cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 || - cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 ) + cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 ) { ts->printf( cvtest::TS::LOG, "Invalid CvBox2D or CvConnectedComp was returned by cvCamShift\n" ); code = cvtest::TS::FAIL_INVALID_OUTPUT; @@ -318,29 +313,6 @@ int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ ) goto _exit_; } - delta = m*0.7; - - if( comp.rect.x < box0.center.x - delta || - comp.rect.y < box0.center.y - delta || - comp.rect.x + comp.rect.width > box0.center.x + delta || - comp.rect.y + comp.rect.height > box0.center.y + delta ) - { - ts->printf( cvtest::TS::LOG, - "Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n", - comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height, - box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta ); - code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; - } - - if( fabs(comp.area - area0) > area0*0.15 ) - { - ts->printf( cvtest::TS::LOG, - "Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 ); - code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; - } - _exit_: if( code < 0 ) @@ -377,8 +349,7 @@ protected: int validate_test_results( int test_case_idx ); void generate_object(); - CvRect init_rect; - CvConnectedComp comp; + Rect init_rect, rect; int area0, area; }; @@ -398,12 +369,10 @@ int CV_MeanShiftTest::prepare_test_case( int test_case_idx ) if( code <= 0 ) return code; - area0 = cvCountNonZero(img); + area0 = countNonZero(img); for(i = 0; i < 100; i++) { - CvMat temp; - m = (box0.size.width + box0.size.height)*0.5; init_rect.x = cvFloor(box0.center.x - m*(0.4 + cvtest::randReal(rng)*0.2)); init_rect.y = cvFloor(box0.center.y - m*(0.4 + cvtest::randReal(rng)*0.2)); @@ -415,8 +384,8 @@ int CV_MeanShiftTest::prepare_test_case( int test_case_idx ) init_rect.y + init_rect.height >= img_size.height ) continue; - cvGetSubRect( img, &temp, init_rect ); - area = cvCountNonZero( &temp ); + Mat temp = img(init_rect); + area = countNonZero( temp ); if( area >= 0.5*area0 ) break; @@ -428,7 +397,8 @@ int CV_MeanShiftTest::prepare_test_case( int test_case_idx ) void CV_MeanShiftTest::run_func(void) { - cvMeanShift( img, init_rect, criteria, &comp ); + rect = init_rect; + meanShift( img, rect, criteria ); } @@ -438,15 +408,8 @@ int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ ) Point2f c; double m = MAX(box0.size.width, box0.size.height), delta; - if( cvIsNaN(comp.area) || cvIsInf(comp.area) || comp.area <= 0 ) - { - ts->printf( cvtest::TS::LOG, "Invalid CvConnectedComp was returned by cvMeanShift\n" ); - code = cvtest::TS::FAIL_INVALID_OUTPUT; - goto _exit_; - } - - c.x = (float)(comp.rect.x + comp.rect.width*0.5); - c.y = (float)(comp.rect.y + comp.rect.height*0.5); + c.x = (float)(rect.x + rect.width*0.5); + c.y = (float)(rect.y + rect.height*0.5); if( fabs(c.x - box0.center.x) > m*0.1 || fabs(c.y - box0.center.y) > m*0.1 ) @@ -459,25 +422,16 @@ int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ ) delta = m*0.7; - if( comp.rect.x < box0.center.x - delta || - comp.rect.y < box0.center.y - delta || - comp.rect.x + comp.rect.width > box0.center.x + delta || - comp.rect.y + comp.rect.height > box0.center.y + delta ) + if( rect.x < box0.center.x - delta || + rect.y < box0.center.y - delta || + rect.x + rect.width > box0.center.x + delta || + rect.y + rect.height > box0.center.y + delta ) { ts->printf( cvtest::TS::LOG, "Incorrect CvConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n", - comp.rect.x, comp.rect.y, comp.rect.x + comp.rect.width, comp.rect.y + comp.rect.height, + rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta ); code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; - } - - if( fabs((double)(comp.area - area0)) > fabs((double)(area - area0)) + area0*0.05 ) - { - ts->printf( cvtest::TS::LOG, - "Incorrect CvConnectedComp area (=%.1f, should be %d)\n", comp.area, area0 ); - code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; } _exit_: diff --git a/modules/video/test/test_kalman.cpp b/modules/video/test/test_kalman.cpp index c34746f69d..32c35b94b7 100644 --- a/modules/video/test/test_kalman.cpp +++ b/modules/video/test/test_kalman.cpp @@ -40,7 +40,7 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/video/tracking_c.h" +#include "opencv2/video/tracking.hpp" namespace opencv_test { namespace { @@ -67,54 +67,41 @@ void CV_KalmanTest::run( int ) const double EPSILON = 1.000; RNG& rng = ts->get_rng(); - CvKalman* Kalm; int i, j; - CvMat* Sample = cvCreateMat(Dim,1,CV_32F); - CvMat* Temp = cvCreateMat(Dim,1,CV_32F); + cv::Mat Sample(Dim,1,CV_32F); + cv::Mat Temp(Dim,1,CV_32F); - Kalm = cvCreateKalman(Dim, Dim); - CvMat Dyn = cvMat(Dim,Dim,CV_32F,Kalm->DynamMatr); - CvMat Mes = cvMat(Dim,Dim,CV_32F,Kalm->MeasurementMatr); - CvMat PNC = cvMat(Dim,Dim,CV_32F,Kalm->PNCovariance); - CvMat MNC = cvMat(Dim,Dim,CV_32F,Kalm->MNCovariance); - CvMat PriErr = cvMat(Dim,Dim,CV_32F,Kalm->PriorErrorCovariance); - CvMat PostErr = cvMat(Dim,Dim,CV_32F,Kalm->PosterErrorCovariance); - CvMat PriState = cvMat(Dim,1,CV_32F,Kalm->PriorState); - CvMat PostState = cvMat(Dim,1,CV_32F,Kalm->PosterState); - cvSetIdentity(&PNC); - cvSetIdentity(&PriErr); - cvSetIdentity(&PostErr); - cvSetZero(&MNC); - cvSetZero(&PriState); - cvSetZero(&PostState); - cvSetIdentity(&Mes); - cvSetIdentity(&Dyn); - Mat _Sample = cvarrToMat(Sample); - cvtest::randUni(rng, _Sample, cvScalarAll(-max_init), cvScalarAll(max_init)); - cvKalmanCorrect(Kalm, Sample); + cv::KalmanFilter Kalm(Dim, Dim); + Kalm.transitionMatrix = cv::Mat::eye(Dim, Dim, CV_32F); + Kalm.measurementMatrix = cv::Mat::eye(Dim, Dim, CV_32F); + Kalm.processNoiseCov = cv::Mat::eye(Dim, Dim, CV_32F); + Kalm.errorCovPre = cv::Mat::eye(Dim, Dim, CV_32F); + Kalm.errorCovPost = cv::Mat::eye(Dim, Dim, CV_32F); + Kalm.measurementNoiseCov = cv::Mat::zeros(Dim, Dim, CV_32F); + Kalm.statePre = cv::Mat::zeros(Dim, 1, CV_32F); + Kalm.statePost = cv::Mat::zeros(Dim, 1, CV_32F); + cvtest::randUni(rng, Sample, Scalar::all(-max_init), Scalar::all(max_init)); + Kalm.correct(Sample); for(i = 0; idata.fl[k]; + t += Dyn.at(j,k)*Sample.at(k); } - Temp->data.fl[j]= (float)(t+(cvtest::randReal(rng)*2-1)*max_noise); + Temp.at(j) = (float)(t+(cvtest::randReal(rng)*2-1)*max_noise); } - cvCopy( Temp, Sample ); - cvKalmanCorrect(Kalm,Temp); + Temp.copyTo(Sample); + Kalm.correct(Temp); } - Mat _state_post = cvarrToMat(Kalm->state_post); - code = cvtest::cmpEps2( ts, _Sample, _state_post, EPSILON, false, "The final estimated state" ); - - cvReleaseMat(&Sample); - cvReleaseMat(&Temp); - cvReleaseKalman(&Kalm); + Mat _state_post = Kalm.statePost; + code = cvtest::cmpEps2( ts, Sample, _state_post, EPSILON, false, "The final estimated state" ); if( code < 0 ) ts->set_failed_test_info( code ); diff --git a/modules/video/test/test_optflowpyrlk.cpp b/modules/video/test/test_optflowpyrlk.cpp index e1fe5b1704..905cfebafa 100644 --- a/modules/video/test/test_optflowpyrlk.cpp +++ b/modules/video/test/test_optflowpyrlk.cpp @@ -40,7 +40,6 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/video/tracking_c.h" namespace opencv_test { namespace { @@ -71,7 +70,7 @@ void CV_OptFlowPyrLKTest::run( int ) int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0; char filename[1000]; - CvPoint2D32f *v = 0, *v2 = 0; + cv::Point2f *v = 0, *v2 = 0; cv::Mat _u, _v, _v2; cv::Mat imgI, imgJ; @@ -145,8 +144,8 @@ void CV_OptFlowPyrLKTest::run( int ) calcOpticalFlowPyrLK(imgI, imgJ, _u, _v2, status, cv::noArray(), Size( 41, 41 ), 4, TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.01f ), 0 ); - v = (CvPoint2D32f*)_v.ptr(); - v2 = (CvPoint2D32f*)_v2.ptr(); + v = (cv::Point2f*)_v.ptr(); + v2 = (cv::Point2f*)_v2.ptr(); /* compare results */ for( i = 0; i < n; i++ ) diff --git a/modules/videoio/src/cap_dc1394_v2.cpp b/modules/videoio/src/cap_dc1394_v2.cpp index dcd3de8107..46e4fe65a4 100644 --- a/modules/videoio/src/cap_dc1394_v2.cpp +++ b/modules/videoio/src/cap_dc1394_v2.cpp @@ -578,7 +578,10 @@ bool CvCaptureCAM_DC1394_v2_CPP::grabFrame() // Swap R&B channels: if (nch==3) - cvConvertImage(&fhdr,&fhdr,CV_CVTIMG_SWAP_RB); + { + cv::Mat tmp = cv::cvarrToMat(&fhdr); + cv::cvtColor(tmp, tmp, cv::COLOR_RGB2BGR, tmp.channels()); + } cvCopy(&fhdr, img[i]); } diff --git a/modules/videoio/src/cap_images.cpp b/modules/videoio/src/cap_images.cpp index 0e3c8f2ac2..1de6e18491 100644 --- a/modules/videoio/src/cap_images.cpp +++ b/modules/videoio/src/cap_images.cpp @@ -50,6 +50,7 @@ // #include "precomp.hpp" +#include "opencv2/imgcodecs.hpp" #include #ifdef NDEBUG @@ -62,81 +63,90 @@ #define _MAX_PATH 1024 #endif -class CvCapture_Images : public CvCapture +namespace cv +{ + +class CvCapture_Images: public IVideoCapture { public: - CvCapture_Images() + void init() { - filename = NULL; + filename.clear(); + frame.release(); currentframe = firstframe = 0; length = 0; - frame = NULL; grabbedInOpen = false; } + CvCapture_Images() + { + init(); + } + CvCapture_Images(const String& _filename) + { + init(); + open(_filename); + } virtual ~CvCapture_Images() CV_OVERRIDE { close(); } - - virtual bool open(const char* _filename); - virtual void close(); virtual double getProperty(int) const CV_OVERRIDE; virtual bool setProperty(int, double) CV_OVERRIDE; virtual bool grabFrame() CV_OVERRIDE; - virtual IplImage* retrieveFrame(int) CV_OVERRIDE; + virtual bool retrieveFrame(int, OutputArray) CV_OVERRIDE; + virtual bool isOpened() const CV_OVERRIDE; + virtual int getCaptureDomain() /*const*/ CV_OVERRIDE { return cv::CAP_IMAGES; } - int getCaptureDomain() /*const*/ CV_OVERRIDE { return cv::CAP_IMAGES; } + bool open(const String&); + void close(); protected: - char* filename; // actually a printf-pattern + + std::string filename; // actually a printf-pattern unsigned currentframe; unsigned firstframe; // number of first frame unsigned length; // length of sequence - IplImage* frame; + Mat frame; bool grabbedInOpen; }; - void CvCapture_Images::close() { - if( filename ) - { - free(filename); - filename = NULL; - } - currentframe = firstframe = 0; - length = 0; - cvReleaseImage( &frame ); + init(); } - bool CvCapture_Images::grabFrame() { char str[_MAX_PATH]; - sprintf(str, filename, firstframe + currentframe); + + if( filename.empty() ) + return false; + + sprintf(str, filename.c_str(), firstframe + currentframe); if (grabbedInOpen) { grabbedInOpen = false; ++currentframe; - return frame != NULL; + return !frame.empty(); } - cvReleaseImage(&frame); - frame = cvLoadImage(str, CV_LOAD_IMAGE_UNCHANGED); - if( frame ) + frame = imread(str, CV_LOAD_IMAGE_UNCHANGED); + if( !frame.empty() ) currentframe++; - return frame != NULL; + return !frame.empty(); } -IplImage* CvCapture_Images::retrieveFrame(int) +bool CvCapture_Images::retrieveFrame(int, OutputArray out) { - return grabbedInOpen ? NULL : frame; + frame.copyTo(out); + return grabbedInOpen ? false : !frame.empty(); } + double CvCapture_Images::getProperty(int id) const { switch(id) @@ -151,9 +161,9 @@ double CvCapture_Images::getProperty(int id) const case CV_CAP_PROP_POS_AVI_RATIO: return (double)currentframe / (double)(length - 1); case CV_CAP_PROP_FRAME_WIDTH: - return frame ? frame->width : 0; + return frame.cols; case CV_CAP_PROP_FRAME_HEIGHT: - return frame ? frame->height : 0; + return frame.rows; case CV_CAP_PROP_FPS: CV_WARN("collections of images don't have framerates\n"); return 1; @@ -199,25 +209,25 @@ bool CvCapture_Images::setProperty(int id, double value) return false; } -static char* icvExtractPattern(const char *filename, unsigned *offset) +static std::string extractPattern(const std::string& filename, unsigned& offset) { - char *name = (char *)filename; + std::string name; - if( !filename ) - return 0; + if( filename.empty() ) + return std::string(); // check whether this is a valid image sequence filename - char *at = strchr(name, '%'); + char *at = strchr((char*)filename.c_str(), '%'); if(at) { unsigned int dummy; if(sscanf(at + 1, "%ud", &dummy) != 1) - return 0; - name = strdup(filename); + return std::string(); + name = filename; } else // no pattern filename was given - extract the pattern { - at = name; + at = (char*)filename.c_str(); // ignore directory names char *slash = strrchr(at, '/'); @@ -231,17 +241,12 @@ static char* icvExtractPattern(const char *filename, unsigned *offset) while (*at && !isdigit(*at)) at++; if(!*at) - return 0; + return std::string(); - sscanf(at, "%u", offset); + sscanf(at, "%u", &offset); - int size = (int)strlen(filename) + 20; - name = (char *)malloc(size); - CV_Assert(name != NULL); - strncpy(name, filename, at - filename); - name[at - filename] = 0; - - strcat(name, "%0"); + name = filename.substr(0, at - filename.c_str()); + name += "%0"; int i; char *extension; @@ -250,21 +255,21 @@ static char* icvExtractPattern(const char *filename, unsigned *offset) char places[13] = {0}; sprintf(places, "%dd", i); - strcat(name, places); - strcat(name, extension); + name += places; + name += extension; } return name; } -bool CvCapture_Images::open(const char * _filename) +bool CvCapture_Images::open(const std::string& _filename) { unsigned offset = 0; close(); - filename = icvExtractPattern(_filename, &offset); - if(!filename) + filename = extractPattern(_filename, offset); + if( filename.empty() ) return false; // determine the length of the sequence @@ -272,7 +277,7 @@ bool CvCapture_Images::open(const char * _filename) char str[_MAX_PATH]; for(;;) { - sprintf(str, filename, offset + length); + sprintf(str, filename.c_str(), offset + length); struct stat s; if(stat(str, &s)) { @@ -283,7 +288,7 @@ bool CvCapture_Images::open(const char * _filename) } } - if(!cvHaveImageReader(str)) + if(!haveImageReader(filename)) break; length++; @@ -305,16 +310,19 @@ bool CvCapture_Images::open(const char * _filename) return grabRes; } - -CvCapture* cvCreateFileCapture_Images(const char * filename) +bool CvCapture_Images::isOpened() const { - CvCapture_Images* capture = new CvCapture_Images; + return !filename.empty(); +} - if( capture->open(filename) ) +Ptr createFileCapture_Images(const String& filename) +{ + Ptr capture(new CvCapture_Images(filename)); + if( capture->isOpened() ) return capture; + return Ptr(); +} - delete capture; - return NULL; } // @@ -327,7 +335,7 @@ class CvVideoWriter_Images CV_FINAL : public CvVideoWriter public: CvVideoWriter_Images() { - filename = 0; + filename.clear(); currentframe = 0; } virtual ~CvVideoWriter_Images() { close(); } @@ -339,7 +347,7 @@ public: int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_IMAGES; } protected: - char* filename; + std::string filename; unsigned currentframe; std::vector params; }; @@ -347,24 +355,21 @@ protected: bool CvVideoWriter_Images::writeFrame( const IplImage* image ) { char str[_MAX_PATH]; - sprintf(str, filename, currentframe); + sprintf(str, filename.c_str(), currentframe); std::vector image_params = params; image_params.push_back(0); // append parameters 'stop' mark image_params.push_back(0); - int ret = cvSaveImage(str, image, &image_params[0]); + cv::Mat img = cv::cvarrToMat(image); + bool ret = cv::imwrite(str, img, image_params); currentframe++; - return ret > 0; + return ret; } void CvVideoWriter_Images::close() { - if( filename ) - { - free( filename ); - filename = 0; - } + filename.clear(); currentframe = 0; params.clear(); } @@ -376,13 +381,13 @@ bool CvVideoWriter_Images::open( const char* _filename ) close(); - filename = icvExtractPattern(_filename, &offset); - if(!filename) + filename = cv::extractPattern(_filename, offset); + if(filename.empty()) return false; char str[_MAX_PATH]; - sprintf(str, filename, 0); - if(!cvHaveImageWriter(str)) + sprintf(str, filename.c_str(), 0); + if(!cv::haveImageWriter(str)) { close(); return false; diff --git a/modules/videoio/src/precomp.hpp b/modules/videoio/src/precomp.hpp index 98e78a64dd..4da95ac40e 100644 --- a/modules/videoio/src/precomp.hpp +++ b/modules/videoio/src/precomp.hpp @@ -54,7 +54,6 @@ #include "opencv2/imgproc.hpp" #include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/imgcodecs/imgcodecs_c.h" #include "opencv2/videoio/videoio_c.h" #include @@ -124,7 +123,6 @@ CvCapture* cvCreateCameraCapture_XIMEA( const char* serialNumber ); CvCapture* cvCreateCameraCapture_AVFoundation(int index); CvCapture* cvCreateCameraCapture_Aravis( int index ); -CvCapture* cvCreateFileCapture_Images(const char* filename); CvVideoWriter* cvCreateVideoWriter_Images(const char* filename); @@ -176,6 +174,8 @@ namespace cv virtual int getCaptureDomain() const { return cv::CAP_ANY; } // Return the type of the capture object: CAP_FFMPEG, etc... }; + Ptr createFileCapture_Images(const String& filename); + Ptr createMotionJpegCapture(const String& filename); Ptr createMotionJpegWriter(const String& filename, int fourcc, double fps, Size frameSize, bool iscolor); diff --git a/modules/videoio/src/videoio_registry.cpp b/modules/videoio/src/videoio_registry.cpp index 46f1cc2552..1f3d28ab0d 100644 --- a/modules/videoio/src/videoio_registry.cpp +++ b/modules/videoio/src/videoio_registry.cpp @@ -487,6 +487,7 @@ void VideoCapture_create(CvCapture*& capture, Ptr& icap, VideoCap void VideoCapture_create(CvCapture*& capture, Ptr& icap, VideoCaptureAPIs api, const cv::String& filename) { + CV_UNUSED(capture); switch (api) { default: @@ -521,7 +522,7 @@ void VideoCapture_create(CvCapture*& capture, Ptr& icap, VideoCap break; #endif case CAP_IMAGES: - TRY_OPEN_LEGACY(cvCreateFileCapture_Images(filename.c_str())) + TRY_OPEN(createFileCapture_Images(filename)) break; #ifdef HAVE_FFMPEG case CAP_FFMPEG: