├── .gitignore ├── src ├── opencvx │ ├── cvxmorphological.h │ ├── cvxskincolor.h │ ├── cvmx.h │ ├── cvmatelemcn.h │ ├── cvxmat.h │ ├── cvxrectangle.h │ ├── cvipltocvtype.h │ ├── cvrandgauss.h │ ├── cvopening.h │ ├── cvclosing.h │ ├── cvskincolorpeer.h │ ├── cvpointnorm.h │ ├── cvinvaffine.h │ ├── cvwaitfps.h │ ├── cvipltocvdepth.h │ ├── cvlogsum.h │ ├── cvprintmat.h │ ├── cvskincolorgauss.h │ ├── cvcreateaffine.h │ ├── cvsandwichfill.h │ ├── cvcat.h │ ├── cvgaussnorm.h │ ├── cvsetcol.h │ ├── cvbackground.h │ ├── cvskincolorcbcr.h │ ├── cvgmmpdf.h │ ├── cvsetrow.h │ ├── cvmxtypeconv.h │ ├── cvgausspdf.h │ ├── mexx.h │ ├── cvcropimageroi.h │ ├── cvrect32f.h │ ├── cvputimageroi.h │ ├── cvcreateaffineimage.h │ ├── cvmxmatconv.h │ ├── cvskincolorgmm.h │ ├── cvdrawrectangle.h │ ├── cvpcadiffs.h │ └── cvparticlerbox2.h ├── Global.h ├── cvdrawwatershed.h ├── icformat.h ├── Actions.hpp └── filesystem.h ├── LICENSE ├── haartrainingformat.py ├── CMakeLists.txt ├── README.md ├── haartrainingformat.pl └── cmake └── FindTBB.cmake /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /src/opencvx/cvxmorphological.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvxmorphological.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | #ifndef CV_MORPHOLOGICAL_INCLUDED 15 | #define CV_MORPHOLOGICAL_INCLUDED 16 | 17 | 18 | #include 19 | #include 20 | 21 | #include "cvopening.h" 22 | #include "cvclosing.h" 23 | #include "cvsandwichfill.h" 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/opencvx/cvxskincolor.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvskincolor.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | // 14 | // See skincrop/skincrop.cpp as an example 15 | */ 16 | #ifndef CV_SKINCOLOR_INCLUDED 17 | #define CV_SKINCOLOR_INCLUDED 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #define _USE_MATH_DEFINES 25 | #include 26 | 27 | #include "cvskincolorpeer.h" 28 | #include "cvskincolorgmm.h" 29 | #include "cvskincolorgauss.h" 30 | #include "cvskincolorcbcr.h" 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2008, Naotoshi Seo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /haartrainingformat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | import re 5 | import argparse 6 | 7 | def main(): 8 | parser = argparse.ArgumentParser() 9 | 10 | parser.add_argument("--clipdir", metavar = "CLIPDIR", 11 | help = "Path to a directory containing the clipped images " 12 | "in the format default imageclipper output format: " 13 | "") 14 | 15 | parser.add_argument("--path", metavar = "PATH", default = "", 16 | help = "The path to prepend to the image name in the output.") 17 | 18 | args = parser.parse_args() 19 | 20 | clip_images = os.listdir(args.clipdir) 21 | 22 | for img in clip_images: 23 | if os.path.isdir(os.path.join(args.clipdir, img)): 24 | continue 25 | 26 | try: 27 | m = re.match("(.*?\..*?)_(-?\d+)_(-?\d+)_(-?\d+)_(-?\d+)_(-?\d+)\..*?", img) 28 | basename = m.group(1) 29 | r = max(0, int(m.group(2))) 30 | x = max(0, int(m.group(3))) 31 | y = max(0, int(m.group(4))) 32 | w = max(0, int(m.group(5))) 33 | h = max(0, int(m.group(6))) 34 | 35 | base_img_path = os.path.join(args.path, basename) 36 | 37 | if (os.path.exists(base_img_path)): 38 | print("%s %d %d %d %d %d" % (base_img_path, 1, x, y, w, h)) 39 | except Exception as ex: 40 | print ex 41 | return 42 | 43 | 44 | if __name__ == '__main__': main() 45 | -------------------------------------------------------------------------------- /src/opencvx/cvmx.h: -------------------------------------------------------------------------------- 1 | /** 2 | * OpenCV versus Matlab C Library (MEX) interfaces 3 | * verified under OpenCV 1.00 and Matlab 2007b 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2008, Naotoshi Seo 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #ifndef CVMX_INCLUDED 28 | #define CVMX_INCLUDED 29 | 30 | #include "cvmxtypeconv.h" 31 | #include "cvmxmatconv.h" 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/opencvx/cvmatelemcn.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_MATELEMCN_INCLUDED 25 | #define CV_MATELEMCN_INCLUDED 26 | 27 | #define CV_MAT_ELEM_CN( mat, elemtype, row, chcol ) \ 28 | (*(elemtype*)((mat).data.ptr + (size_t)(mat).step*(row) + sizeof(elemtype)*(chcol))) 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/opencvx/cvxmat.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_MATEXT_INCLUDED 25 | #define CV_MATEXT_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "cvmatelemcn.h" 32 | #include "cvprintmat.h" 33 | #include "cvsetrow.h" 34 | #include "cvsetcol.h" 35 | #include "cvcat.h" 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(imageclipper) 3 | 4 | option(WITH_TBB "Turn on support for TBB, Threading Building Blocks. You must have an OpenCV compiled with support for this" OFF) 5 | 6 | if (MSVC) 7 | # We link statically on windows so we don't have to copy DLLs around. 8 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") 9 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") 10 | set(OpenCV_STATIC ON) 11 | 12 | set(BOOST_INCLUDEDIR "${BOOST_ROOT}/boost") 13 | set(BOOST_LIBRARYDIR "${BOOST_ROOT}/lib32-msvc-12.0") 14 | set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${BOOST_INCLUDEDIR}") 15 | set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${BOOST_LIBRARYDIR}") 16 | set(Boost_USE_STATIC_LIBS ON) 17 | set(Boost_USE_STATIC_RUNTIME ON) 18 | set(Boost_DETAILED_FAILURE_MSG ON) 19 | endif() 20 | 21 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") 22 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) 23 | 24 | if (WITH_TBB) 25 | find_package(TBB REQUIRED) 26 | endif() 27 | 28 | find_package(Boost COMPONENTS system filesystem) 29 | find_package(OpenCV REQUIRED) 30 | 31 | add_executable(imageclipper src/imageclipper.cpp) 32 | 33 | include_directories(${Boost_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS} src) 34 | link_directories(${Boost_LIBRARY_DIR}) 35 | target_link_libraries(imageclipper ${OpenCV_LIBS} ${Boost_LIBRARIES}) 36 | 37 | if (WITH_TBB) 38 | include_directories(${TBB_INCLUDE_DIRS}) 39 | link_directories(${TBB_INCLUDE_DIRS}) 40 | target_link_libraries(imageclipper ${TBB_LIBRARIES}) 41 | endif() 42 | 43 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 44 | 45 | message("OpenCV Libs: ${OpenCV_LIBS}") 46 | message("Boost libs: ${Boost_LIBRARIES}") 47 | message("Boost link dir: ${Boost_LIBRARY_DIR}") 48 | 49 | -------------------------------------------------------------------------------- /src/opencvx/cvxrectangle.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_RECTANGLE_INCLUDED 25 | #define CV_RECTANGLE_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | #include 31 | #include "highgui.h" 32 | #include 33 | #define _USE_MATH_DEFINES 34 | #include 35 | using namespace std; 36 | 37 | #include "cvrect32f.h" 38 | #include "cvcreateaffine.h" 39 | #include "cvdrawrectangle.h" 40 | #include "cvcropimageroi.h" 41 | #include "cvpointnorm.h" 42 | #include "cvinvaffine.h" 43 | #include "cvcreateaffineimage.h" 44 | #include "cvputimageroi.h" 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/opencvx/cvipltocvtype.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_IPLTOCVDEPTH_INCLUDED 25 | #define CV_IPLTOCVDEPTH_INCLUDED 26 | 27 | 28 | #include 29 | 30 | CV_INLINE int cvIplToCvType(int ipl_depth, int nChannels); 31 | 32 | /** 33 | * Convert IplImage depth and nChannels to CvMat type 34 | * 35 | * @param depth IplImage depth. img->depth 36 | * @param nChannels Number of channels. img->nChannels 37 | * @return int CvMat type. mat->type 38 | */ 39 | CV_INLINE int cvIplToCvType(int ipl_depth, int nChannels) 40 | { 41 | int cvmat_depth = cvIplToCvDepth( ipl_depth ); 42 | return CV_MAKETYPE( cvmat_depth, nChannels ); 43 | } 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/opencvx/cvrandgauss.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_RAND_INCLUDED 25 | #define CV_RAND_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | 31 | double cvRandGauss( CvRNG* rng, double sigma ); 32 | 33 | /** 34 | * This function returns a Gaussian random variate, with mean zero and standard deviation sigma. 35 | * 36 | * @param rng cvRNG random state 37 | * @param sigma standard deviation 38 | * @return double 39 | */ 40 | double cvRandGauss( CvRNG* rng, double sigma ) 41 | { 42 | CvMat* mat = cvCreateMat( 1, 1, CV_64FC1 ); 43 | double var = 0; 44 | cvRandArr( rng, mat, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(sigma) ); 45 | var = cvmGet( mat, 0, 0 ); 46 | cvReleaseMat( &mat ); 47 | return var; 48 | } 49 | /* 50 | rng.disttype = CV_RAND_NORMAL; 51 | cvRandSetRange( &rng, 30, 100, -1 ); */ 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/opencvx/cvopening.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_OPENING_INCLUDED 25 | #define CV_OPENING_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | CVAPI( void ) cvOpening( const CvArr* src, CvArr* dst, IplConvKernel* element = NULL, int iterations = 1 ); 32 | /** 33 | // cvOpening - opening morphological operation 34 | // 35 | // opening operation would help to remove noise 36 | // 37 | // @param src Input Array 38 | // @param dst Output Array 39 | // @param [element = NULL] Kernel shape. see cvErode or cvDilate 40 | // @param [iterations = 1] 41 | // @return void 42 | // 43 | // References) 44 | // R. Gonzalez, R. Woods, "Digital Image Processing," chapter 9 45 | */ 46 | CVAPI( void ) cvOpening( const CvArr* src, CvArr* dst, IplConvKernel* element, int iterations ) 47 | { 48 | cvErode( src, dst, element, iterations ); 49 | cvDilate( dst, dst, element, iterations ); 50 | } 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/opencvx/cvclosing.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CLOSING_INCLUDED 25 | #define CV_CLOSING_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | CVAPI(void) cvClosing( const CvArr* src, CvArr* dst, IplConvKernel* element = NULL, int iterations = 1 ); 32 | 33 | /** 34 | // cvClosing - closing morphological operation 35 | // 36 | // closing operation would help to fill disconnected contour 37 | // 38 | // @param src Input Array 39 | // @param dst Output Array 40 | // @param [element = NULL] Kernel shape. see cvErode or cvDilate 41 | // @param [iterations = 1] 42 | // @return void 43 | // 44 | // References) 45 | // R. Gonzalez, R. Woods, "Digital Image Processing," chapter 9 46 | */ 47 | CVAPI(void) cvClosing( const CvArr* src, CvArr* dst, IplConvKernel* element, int iterations ) 48 | { 49 | cvDilate( dst, dst, element, iterations ); 50 | cvErode( src, dst, element, iterations ); 51 | } 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/opencvx/cvskincolorpeer.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvskincolorpeer.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | #ifndef CV_SKINCOLOR_PEER_INCLUDED 15 | #define CV_SKINCOLOR_PEER_INCLUDED 16 | 17 | 18 | #include 19 | #include 20 | using namespace std; 21 | 22 | void cvSkinColorPeer( const IplImage* img, IplImage* mask ); 23 | 24 | /** 25 | // cvSkinColorPeer - Skin Color Detection by Peer, et.al [1] 26 | // 27 | // @param img Input image 28 | // @param mask Generated mask image. 1 for skin and 0 for others 29 | // 30 | // References) 31 | // [1] P. Peer, J. Kovac, J. and F. Solina, ”Human skin colour 32 | // clustering for face detection”, In: submitted to EUROCON – 33 | // International Conference on Computer as a Tool , 2003. 34 | // (R>95)^(G>40)^(B>20)^ 35 | // (max{R,G,B}-min{R,G,B}>15)^ 36 | // (|R -G|>15)^(R>G)^(R>B) 37 | */ 38 | void cvSkinColorPeer( const IplImage* img, IplImage* mask ) 39 | { 40 | int x, y; 41 | uchar r, g, b; 42 | cvSet( mask, cvScalarAll(0) ); 43 | for( y = 0; y < img->height; y++ ) 44 | { 45 | for( x = 0; x < img->width; x++ ) 46 | { 47 | b = img->imageData[img->widthStep * y + x * 3]; 48 | g = img->imageData[img->widthStep * y + x * 3 + 1]; 49 | r = img->imageData[img->widthStep * y + x * 3 + 2]; 50 | 51 | if( r > 95 && g > 40 && b > 20 && 52 | max( r, max( g, b ) ) - min( r, min( g, b ) ) > 15 && 53 | abs( r - g ) > 15 && r > g && r > b ) 54 | { 55 | mask->imageData[mask->widthStep * y + x] = 1; 56 | } 57 | } 58 | } 59 | } 60 | 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /src/opencvx/cvpointnorm.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_POINTNORM_INCLUDED 25 | #define CV_POINTNORM_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | CV_INLINE double cvPointNorm( CvPoint p1, CvPoint p2, int norm_type = CV_L2 ); 34 | 35 | /** 36 | // Compute Norm between two points 37 | // 38 | // @param CvPoint p1 A point 1 39 | // @param CVPoint p2 A point 2 40 | // @param int [norm_type = CV_L2] CV_L2 to compute L2 norm (euclidean distance) 41 | // CV_L1 to compute L1 norm (abs) 42 | // @return double 43 | */ 44 | CV_INLINE double cvPointNorm( CvPoint p1, CvPoint p2, int norm_type ) 45 | { 46 | if( norm_type == CV_L1 ) 47 | return abs( p2.x - p1.x ) + abs( p2.y - p1.y ); 48 | else 49 | return sqrt( pow( (double)p2.x - p1.x, 2 ) + pow( (double)p2.y - p1.y, 2 ) ); 50 | } 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/Global.h: -------------------------------------------------------------------------------- 1 | #ifndef __GLOBAL_H__ 2 | #define __GLOBAL_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************************************ Structure *************************************/ 9 | 10 | /** 11 | * A Callback function structure 12 | */ 13 | typedef struct CvCallbackParam { 14 | const char* w_name; // Title - interactive window's title 15 | const char* miniw_name; // Title - the title of the window showing crop result 16 | IplImage* img_src; // Cache - Current image pointer 17 | CvRect rect; // Cache - Current selected pointer 18 | CvRect circle; // Cache - Current watershed. use x, y for center, width as radius. width == 0 means watershed is off 19 | int rotate; // Cache - Current roteted degree 20 | CvPoint shear; 21 | std::vector imtypes; // Enum - image types 22 | std::vector filelist; // for image or directory load 23 | std::vector::iterator fileiter; 24 | CvCapture* cap; // Cache - for video load 25 | int frame; // Cache - video iter 26 | const char* output_format; // Pattern - output pattern 27 | 28 | CvSize screen_size; // Cache - screen resolution 29 | IplImage* img_display; // Cache - Current image pointer 30 | float scale_factor; // Cache - global scale factor 31 | float cap_scale_factor; // Cache - scale factor of capture 32 | } CvCallbackParam; 33 | 34 | 35 | /** 36 | * MouseCallback param structure 37 | */ 38 | typedef struct MouseCallbackStatus { 39 | CvPoint hit_point; 40 | bool move_rect; 41 | bool resize_rect_left; 42 | bool resize_rect_right; 43 | bool resize_rect_top; 44 | bool resize_rect_bottom; 45 | bool move_watershed; 46 | bool resize_watershed; 47 | } MouseCallbackStatus; 48 | 49 | /** 50 | * Command Argument structure 51 | */ 52 | typedef struct ArgParam { 53 | const char* name; // Path - absolute path of executor 54 | std::string reference; // Path - path of reference folder 55 | const char* imgout_format; // Pattern - default output pattern of image 56 | const char* vidout_format; // Pattern - default output pattern of video 57 | const char* output_format; // Pattern - user specifying output pattern 58 | int frame; // Num - how much frame does the program need read from video 59 | } ArgParam; 60 | 61 | #endif // __GLOBAL_H__ 62 | -------------------------------------------------------------------------------- /src/opencvx/cvinvaffine.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_INVAFFINE_INCLUDED 25 | #define CV_INVAFFINE_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "cvsetrow.h" 32 | 33 | CVAPI(void) cvInvAffine( const CvMat* affine, CvMat* invaffine ); 34 | 35 | /** 36 | * Create an inv affine transform matrix from an affine transform matrix 37 | * 38 | * @param affine The 2 x 3 CV_32FC1|CV_64FC1 affine matrix 39 | * @param invaffine The 2 x 3 CV_32FC1|CV_64FC1 affine matrix to be created 40 | */ 41 | CVAPI(void) cvInvAffine( const CvMat* affine, CvMat* invaffine ) 42 | { 43 | CV_FUNCNAME( "cvCreateAffine" ); 44 | __CV_BEGIN__; 45 | CV_ASSERT( affine->rows == 2 && affine->cols == 3 ); 46 | CV_ASSERT( invaffine->rows == 2 && invaffine->cols == 3 ); 47 | CV_ASSERT( affine->type == invaffine->type ); 48 | 49 | CvMat* Affine = cvCreateMat( 3, 3, affine->type ); 50 | CvMat* InvAffine = cvCreateMat( 3, 3, affine->type ); 51 | CvMat invaffinehdr; 52 | cvSetIdentity( Affine ); 53 | cvSetRows( affine, Affine, 0, 2 ); 54 | cvInv( Affine, InvAffine ); 55 | cvSetRows( InvAffine, invaffine, 0, 2 ); 56 | cvReleaseMat( &Affine ); 57 | cvReleaseMat( &InvAffine ); 58 | __CV_END__; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/opencvx/cvwaitfps.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_WAITFPS_INCLUDED 25 | #define CV_WAITFPS_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | clock_t cvWaitFps( double frame_per_sec, clock_t start = 0 ); 34 | 35 | /** 36 | * Wait amount of seconds per frame totally. 37 | * If processing time exceeded the seconds per frame, 38 | * no wait occurs and negative waiting time is returned. 39 | * 40 | * Example) 41 | * 42 | * // #include 43 | * clock_t start = clock(); 44 | * fps = cvGetCaptureProperty(video, CV_CAP_PROP_FPS); 45 | * // process 46 | * cvWaitFps( fps, start ); 47 | * 48 | * 49 | * @param fps Frame per second video property 50 | * @param [start = 0] Start time 51 | * @return clock_t Wait time 52 | */ 53 | clock_t cvWaitFps( double fps, clock_t start ) 54 | { 55 | clock_t msec_per_frame = (clock_t)( 1.0 / (double) fps * 1000 ); 56 | clock_t current = clock(); 57 | clock_t wait_time; 58 | if( start == 0 ) { 59 | wait_time = msec_per_frame; 60 | } else { 61 | wait_time = msec_per_frame - (current - start); 62 | } 63 | //while( clock() - current < wait_time ) {} 64 | cvWaitKey( wait_time > 0 ? wait_time : 1 ); 65 | return wait_time; 66 | } 67 | 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Image Clipper 2 | ------------- 3 | 4 | This is a fork of the [imageclipper](https://code.google.com/p/imageclipper/) 5 | program initially written by Naotoshi Seo. 6 | 7 | This fork was made initially for myself to be able to compile the program on OSX. 8 | Since I used [CMake](http://www.cmake.org/) it was easy to make one build that 9 | works on all platforms. 10 | 11 | From the original description: 12 | 13 | > It is often required to crop images manually fast for computer vision researchers 14 | > to gather training and testing image sets, e.g., cropping faces in images. 15 | > 16 | > This simple multi-platform (Windows, OSX and Linux were verified) software helps 17 | > you to clip images manually fast. 18 | 19 | ## Building 20 | 21 | To build you need [CMake](http://www.cmake.org/), [OpenCV](http://opencv.org/) and [Boost](http://www.boost.org/) 22 | on your system 23 | 24 | ### Linux & OSX 25 | 26 | Install requirements using your favorite package system or download the sources yourself. 27 | 28 | ```bash 29 | $ sudo apt-get install cmake libopencv-dev libboost-all-dev 30 | ``` 31 | 32 | Using [MacPorts](http://www.macports.org/). 33 | 34 | ```bash 35 | $ sudo port install cmake boost opencv 36 | ``` 37 | 38 | And then build: 39 | 40 | ```bash 41 | $ git clone 42 | $ cd imageclipper 43 | $ mkdir build && cd build 44 | $ cmake .. # This should work in most cases. 45 | 46 | # (Optional) Or specifying your own builds of OpenCV or Boost. 47 | $ cmake -DOpenCV_DIR=/path/to/opencv/build/ -DBOOST_ROOT=/path/to/boost .. 48 | 49 | $ cmake --build . # Builds the program. 50 | ``` 51 | 52 | ### Windows 53 | 54 | On windows you need to download [Visual Studio for Windows Desktop](http://www.visualstudio.com/), [CMake](http://www.cmake.org/cmake/resources/software.html) and [git](http://git-scm.com/). 55 | 56 | ###### Boost 57 | 58 | Has been tested with this verison on Windows 7, but should work with whatever you like: 59 | [Boost 1.55.0 32-bit MSVC12](http://sourceforge.net/projects/boost/files/boost-binaries/1.55.0-build2/) 60 | The default unpack location is **c:\local\boost_1_55_0**, if you change it you need to know the path when building. 61 | 62 | ###### OpenCV 63 | 64 | Download and unpack [OpenCV](http://opencv.org/downloads.html) and remember the path. 65 | 66 | From the **git bash** console: 67 | (use cmd.exe if you prefer) 68 | 69 | ```bash 70 | $ git clone 71 | $ cd imageclipper 72 | $ mkdir build && cd build 73 | $ cmake -DOpenCV_DIR=/path/to/opencv/build/ -DBOOST_ROOT=/c/local/boost_1_55_0/ .. 74 | $ cmake --build . --config Release 75 | $ start imageclipper.sln # If you want to open in Visual Studio instead. 76 | ``` 77 | 78 | The executable can then be found under **build/bin/Release/imageclipper.exe** 79 | -------------------------------------------------------------------------------- /src/opencvx/cvipltocvdepth.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_IPLTOCVDEPTH_INCLUDED 25 | #define CV_IPLTOCVDEPTH_INCLUDED 26 | 27 | 28 | #include 29 | 30 | CVAPI(int) cvIplToCvDepth(int depth); 31 | 32 | /** 33 | * Convert IplImage depth to CvMat depth 34 | * 35 | * Below codes would be useful as a reference: 36 | * 37 | * int depth = CV_MAT_DEPTH(mat->type); 38 | * int nChannels = CV_MAT_CN(mat->type); 39 | * int type = CV_MAKETYPE(depth, nChannels); 40 | * 41 | * 42 | * @param int IplImage depth 43 | * @return int cvMat depth 44 | * @reference cxcore/src/_cxcore.h#icvIplToCvDepth 45 | * @reference ./cxcore/src/cxtables.cpp 46 | * @see cvCvToIplDepth(mat->type) 47 | */ 48 | CVAPI(int) cvIplToCvDepth(int depth) 49 | { 50 | /* 51 | #define CV_8U 0 52 | #define CV_8S 1 53 | #define CV_16U 2 54 | #define CV_16S 3 55 | #define CV_32S 4 56 | #define CV_32F 5 57 | #define CV_64F 6 58 | #define CV_USRTYPE1 7 */ 59 | /* 60 | #define IPL_DEPTH_SIGN 0x80000000 61 | #define IPL_DEPTH_1U 1 62 | #define IPL_DEPTH_8U 8 63 | #define IPL_DEPTH_16U 16 64 | #define IPL_DEPTH_32F 32 65 | #define IPL_DEPTH_64F 64 66 | #define IPL_DEPTH_8S (IPL_DEPTH_SIGN| 8) 67 | #define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16) 68 | #define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32) */ 69 | static const signed char IplToCvDepth[] = 70 | { 71 | -1, -1, CV_8U, CV_8S, CV_16U, CV_16S, -1, -1, 72 | CV_32F, CV_32S, -1, -1, -1, -1, -1, -1, CV_64F, -1 73 | }; 74 | return IplToCvDepth[(((depth) & 255) >> 2) + ((depth) < 0)]; 75 | } 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/opencvx/cvlogsum.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_LOGSUM_INCLUDED 25 | #define CV_LOGSUM_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | CvScalar cvLogSum( const CvArr *arr ); 35 | 36 | /** 37 | * cvLogSum 38 | * 39 | * Get log(a + b + c) from log(a), log(b), log(c) 40 | * Useful to take sum of probabilities from log probabilities 41 | * Useful to avoid loss of precision caused by taking exp 42 | * 43 | * @param arr array having log values. 32F or 64F 44 | * @return CvScalar 45 | */ 46 | CvScalar cvLogSum( const CvArr *arr ) 47 | { 48 | IplImage* img = (IplImage*)arr, imgstub; 49 | IplImage *tmp, *tmp2; 50 | int row, col, ch; 51 | CvScalar sumval; 52 | CvScalar minval, maxval; 53 | CV_FUNCNAME( "cvLogSum" ); 54 | __CV_BEGIN__; 55 | 56 | if( !CV_IS_IMAGE(img) ) 57 | { 58 | CV_CALL( img = cvGetImage( img, &imgstub ) ); 59 | } 60 | tmp = cvCreateImage( cvGetSize(img), img->depth, img->nChannels ); 61 | tmp2 = cvCreateImage( cvGetSize(img), img->depth, img->nChannels ); 62 | 63 | // to avoid loss of precision caused by taking exp as much as possible 64 | // if this trick is not required, cvExp -> cvSum are enough 65 | for( ch = 0; ch < img->nChannels; ch++ ) 66 | { 67 | cvSetImageCOI( img, ch + 1 ); 68 | cvMinMaxLoc( img, &minval.val[ch], &maxval.val[ch] ); 69 | } 70 | cvSetImageCOI( img, 0 ); 71 | cvSubS( img, maxval, tmp ); 72 | 73 | cvExp( tmp, tmp2 ); 74 | sumval = cvSum( tmp2 ); 75 | for( ch = 0; ch < img->nChannels; ch++ ) 76 | { 77 | sumval.val[ch] = log( sumval.val[ch] ) + maxval.val[ch]; 78 | } 79 | cvReleaseImage( &tmp ); 80 | cvReleaseImage( &tmp2 ); 81 | __CV_END__; 82 | return sumval; 83 | } 84 | 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /src/cvdrawwatershed.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * 3 | * The MIT License 4 | * 5 | * Copyright (c) 2008, Naotoshi Seo 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | 26 | #ifndef CV_DRAWWATERSHED_INCLUDED 27 | #define CV_DRAWWATERSHED_INCLUDED 28 | 29 | #include 30 | #include 31 | 32 | // marker's shape is like circle 33 | // just for imageclipper.cpp for now 34 | CvRect cvDrawWatershed(IplImage* img, const CvRect circle) 35 | { 36 | IplImage* markers = cvCreateImage(cvGetSize(img), IPL_DEPTH_32S, 1); 37 | CvPoint center = cvPoint(circle.x, circle.y); 38 | int radius = circle.width; 39 | 40 | // Set watershed markers. Now, marker's shape is like circle 41 | // Set (1 * radius) - (3 * radius) region as ambiguous region (0), intuitively 42 | cvSet(markers, cvScalarAll(1)); 43 | cvCircle(markers, center, 3 * radius, cvScalarAll(0), CV_FILLED, 8, 0); 44 | cvCircle(markers, center, radius, cvScalarAll(2), CV_FILLED, 8, 0); 45 | cvWatershed(img, markers); 46 | 47 | // Draw watershed markers and rectangle surrounding watershed markers 48 | cvCircle(img, center, radius, cvScalarAll(255), 2, 8, 0); 49 | 50 | CvPoint minpoint = cvPoint(markers->width, markers->height); 51 | CvPoint maxpoint = cvPoint(0, 0); 52 | for (int y = 1; y < markers->height - 1; y++) { // looks outer boundary is always -1. 53 | for (int x = 1; x < markers->width - 1; x++) { 54 | int* idx = (int *)cvPtr2D(markers, y, x, NULL); 55 | if (*idx == -1) { // watershed marker -1 56 | cvSet2D(img, y, x, cvScalarAll(255)); 57 | if (x < minpoint.x) minpoint.x = x; 58 | if (y < minpoint.y) minpoint.y = y; 59 | if (x > maxpoint.x) maxpoint.x = x; 60 | if (y > maxpoint.y) maxpoint.y = y; 61 | } 62 | } 63 | } 64 | return cvRect(minpoint.x, minpoint.y, maxpoint.x - minpoint.x, maxpoint.y - minpoint.y); 65 | } 66 | 67 | inline CvRect cvShowImageAndWatershed(const char* w_name, const IplImage* img, const CvRect &circle) 68 | { 69 | IplImage* clone = cvCloneImage(img); 70 | CvRect rect = cvDrawWatershed(clone, circle); 71 | cvRectangle(clone, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height), CV_RGB(255, 255, 0), 1); 72 | cvShowImage(w_name, clone); 73 | cvReleaseImage(&clone); 74 | return rect; 75 | } 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/opencvx/cvprintmat.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_PRINTMAT_INCLUDED 25 | #define CV_PRINTMAT_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | CV_INLINE void cvPrintMatProperty( const CvMat* mat ); 34 | CV_INLINE void cvPrintImageProperty( const IplImage* img ); 35 | CV_INLINE void cvPrintMat( const CvArr* arr ); 36 | 37 | /** 38 | * Print CvMat Property 39 | * 40 | * @param mat 41 | * @return void 42 | */ 43 | CV_INLINE void cvPrintMatProperty( const CvMat* mat ) 44 | { 45 | printf("CvMat Property\n"); 46 | printf(" rows: %d\n", mat->rows); 47 | printf(" cols: %d\n", mat->cols); 48 | printf(" type: %d\n", mat->type); 49 | printf(" step: %d\n", mat->step); 50 | fflush( stdout ); 51 | } 52 | 53 | /** 54 | * Print IplImage Property 55 | * 56 | * @param img 57 | * @return void 58 | */ 59 | CV_INLINE void cvPrintImageProperty( const IplImage* img ) 60 | { 61 | printf("IplImage Property\n"); 62 | printf(" width: %d\n", img->width); 63 | printf(" height: %d\n", img->height); 64 | printf(" depth: %d\n", img->depth); 65 | printf(" nChannels: %d\n", img->nChannels); 66 | fflush( stdout ); 67 | } 68 | 69 | /** 70 | * Print array 71 | * 72 | * @param arr array 73 | * @return void 74 | */ 75 | CV_INLINE void cvPrintMat( const CvArr* arr ) 76 | { 77 | CV_FUNCNAME( "cvPrintMat" ); 78 | __CV_BEGIN__; 79 | int row, col, ch; 80 | int coi = 0; 81 | CvMat matstub, *mat = (CvMat*)arr; 82 | int depth, nChannels; 83 | CvScalar value; 84 | if( !CV_IS_MAT( mat ) ) 85 | { 86 | CV_CALL( mat = cvGetMat( mat, &matstub, &coi ) ); // i.e. IplImage to CvMat 87 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 88 | } 89 | depth = CV_MAT_DEPTH( mat->type ); 90 | nChannels = CV_MAT_CN( mat->type ); 91 | for( row = 0; row < mat->rows; row++ ) 92 | { 93 | for( col = 0; col < mat->cols; col++ ) 94 | { 95 | value = cvGet2D( mat, row, col ); 96 | if( nChannels > 1 ) 97 | { 98 | printf( "(%lf", value.val[0] ); 99 | for( ch = 1; ch < nChannels; ch++ ) 100 | { 101 | printf( " %lf", value.val[ch] ); 102 | } 103 | printf( ") " ); 104 | } 105 | else 106 | { 107 | printf( "%lf ", value.val[0] ); 108 | } 109 | } 110 | printf("\n"); 111 | } 112 | fflush( stdout ); 113 | __CV_END__; 114 | } 115 | 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /src/opencvx/cvskincolorgauss.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvskincolorgauss.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | #ifndef CV_SKINCOLOR_GAUSS_INCLUDED 15 | #define CV_SKINCOLOR_GAUSS_INCLUDED 16 | 17 | 18 | #include 19 | #include 20 | #define _USE_MATH_DEFINES 21 | #include 22 | 23 | void cvSkinColorGauss( const IplImage* _img, IplImage* mask, double factor = 2.5 ); 24 | 25 | /** 26 | // cvSkinColorGauss - Skin Color Detection with a Gaussian model 27 | // 28 | // @param img Input image 29 | // @param mask Generated mask image. 1 for skin and 0 for others 30 | // @param [factor = 2.5] A factor to determine threshold value. 31 | // The default threshold is -2.5 * sigma and 2.5 * sigma which 32 | // supports more than 95% region of Gaussian PDF. 33 | // 34 | // References) 35 | // [1] @INPROCEEDINGS{Yang98skin-colormodeling, 36 | // author = {Jie Yang and Weier Lu and Alex Waibel}, 37 | // title = {Skin-color modeling and adaptation}, 38 | // booktitle = {}, 39 | // year = {1998}, 40 | // pages = {687--694} 41 | // } 42 | // [2] C. Stauffer, and W. E. L. Grimson, “Adaptive 43 | // background mixture models for real-time tracking”, In: 44 | // Proceedings 1999 IEEE Computer Society Conference 45 | // on Computer Vision and Pattern Recognition (Cat. No 46 | // PR00149), IEEE Comput. Soc. Part vol. 2, 1999. 47 | */ 48 | void cvSkinColorGauss( const IplImage* _img, IplImage* mask, double factor ) 49 | { 50 | double mean[] = { 188.9069, 142.9157, 115.1863 }; 51 | double sigma[] = { 58.3542, 45.3306, 43.397 }; 52 | double subdata[3]; 53 | 54 | IplImage* img = cvCreateImage( cvGetSize(_img), _img->depth, _img->nChannels ); 55 | cvCvtColor( _img, img, CV_BGR2RGB ); 56 | CvMat* mat = cvCreateMat( img->height, img->width, CV_64FC3 ); 57 | cvConvert( img, mat ); 58 | 59 | for( int i = 0; i < mat->rows; i++ ) 60 | { 61 | for( int j = 0; j < mat->cols; j++ ) 62 | { 63 | bool skin; 64 | CvScalar data = cvGet2D( mat, i, j ); 65 | subdata[0] = data.val[0] - mean[0]; 66 | subdata[1] = data.val[1] - mean[1]; 67 | subdata[2] = data.val[2] - mean[2]; 68 | //if( CV_SKINCOLOR_GAUSS_CUBE ) // cube-like judgement 69 | //{ 70 | skin = 1; 71 | // 2 * sigma => 95% confidence region, 2.5 gives more 72 | skin &= ( - factor *sigma[0] < subdata[0] && subdata[0] < factor *sigma[0] ); 73 | skin &= ( - factor *sigma[1] < subdata[1] && subdata[1] < factor *sigma[1] ); 74 | skin &= ( - factor *sigma[2] < subdata[2] && subdata[2] < factor *sigma[2] ); 75 | //} 76 | //else if( CV_SKINCOLOR_GAUSS_ELLIPSOID ) // ellipsoid-like judgement 77 | //{ 78 | // double val = 0; 79 | // val += pow( subdata[0] / sigma[0] , 2); 80 | // val += pow( subdata[1] / sigma[1] , 2); 81 | // val += pow( subdata[2] / sigma[2] , 2); 82 | // skin = ( val <= pow( factor , 2 ) ); 83 | //} 84 | //else if( CV_SKINCOLOR_GAUSS_LIKELIHOODRATIO ) // likelihood-ratio test (need data for non-skin gaussain model) 85 | //{ 86 | //} 87 | mask->imageData[mask->widthStep * i + j] = skin; 88 | } 89 | } 90 | cvReleaseMat( &mat ); 91 | cvReleaseImage( &img ); 92 | } 93 | 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/opencvx/cvcreateaffine.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CREATEAFFINE_INCLUDED 25 | #define CV_CREATEAFFINE_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | #include "cvrect32f.h" 34 | 35 | CVAPI(void) cvCreateAffine( CvMat* affine, 36 | CvRect32f rect = cvRect32f(0,0,1,1,0), 37 | CvPoint2D32f shear = cvPoint2D32f(0,0) ); 38 | 39 | /** 40 | * Create an affine transform matrix 41 | * 42 | * @param affine The 2 x 3 CV_32FC1|CV_64FC1 affine matrix to be created 43 | * @param [rect = cvRect32f(0,0,1,1,0)] 44 | * The translation (x, y) and scaling (width, height) and 45 | * rotation (angle) paramenter in degree 46 | * @param [shear = cvPoint2D32f(0,0)] 47 | * The shear deformation parameter shx and shy 48 | * @return void 49 | * @Book{Hartley2004, 50 | * author = "Hartley, R.~I. and Zisserman, A.", 51 | * title = "Multiple View Geometry in Computer Vision", 52 | * edition = "Second", 53 | * year = "2004", 54 | * publisher = "Cambridge University Press, ISBN: 0521540518" 55 | * } 56 | */ 57 | CVAPI(void) cvCreateAffine( CvMat* affine, CvRect32f rect, CvPoint2D32f shear ) 58 | { 59 | double c, s; 60 | CvMat *R, *S, *A, hdr; 61 | CV_FUNCNAME( "cvCreateAffine" ); 62 | __CV_BEGIN__; 63 | CV_ASSERT( rect.width > 0 && rect.height > 0 ); 64 | CV_ASSERT( affine->rows == 2 && affine->cols == 3 ); 65 | 66 | // affine = [ A T ] 67 | // A = [ a b; c d ] 68 | // Translation T = [ tx; ty ] 69 | // (1) A = Rotation * Shear(-phi) * [sx 0; 0 sy] * Shear(phi) 70 | // (2) A = Rotation * [sx shx; shy sy] 71 | // Use (2) 72 | 73 | // T = [tx; ty] 74 | cvmSet( affine, 0, 2, rect.x ); cvmSet( affine, 1, 2, rect.y ); 75 | 76 | // R = [cos -sin; sin cos] 77 | R = cvCreateMat( 2, 2, affine->type ); 78 | /*CvMat* R = cvCreateMat( 2, 3, CV_32FC1 ); 79 | cv2DRotationMatrix( cvPoint2D32f( 0, 0 ), rect.angle, 1.0, R ); */ 80 | c = cos( -M_PI / 180 * rect.angle ); 81 | s = sin( -M_PI / 180 * rect.angle ); 82 | cvmSet( R, 0, 0, c ); cvmSet( R, 0, 1, -s ); 83 | cvmSet( R, 1, 0, s ); cvmSet( R, 1, 1, c ); 84 | 85 | // S = [sx shx; shy sy] 86 | S = cvCreateMat( 2, 2, affine->type ); 87 | cvmSet( S, 0, 0, rect.width ); cvmSet( S, 0, 1, shear.x ); 88 | cvmSet( S, 1, 0, shear.y ); cvmSet( S, 1, 1, rect.height ); 89 | 90 | // A = R * S 91 | A = cvGetCols( affine, &hdr, 0, 2 ); 92 | cvMatMul ( R, S, A ); 93 | 94 | cvReleaseMat( &R ); 95 | cvReleaseMat( &S ); 96 | __CV_END__; 97 | } 98 | 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /src/icformat.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * 3 | * Image clipper filename format 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2008, Naotoshi Seo 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | #ifndef IC_FORMAT_INCLUDED 29 | #define IC_FORMAT_INCLUDED 30 | 31 | #include 32 | #include 33 | 34 | /** 35 | * Convert format to string 36 | * 37 | * @param format The format string 38 | * - \%d => dirname 39 | * - \%i => filename 40 | * - \%e => extension 41 | * - \%x => x 42 | * - \%y => y 43 | * - \%w => width 44 | * - \%h => height 45 | * - \%f => frame number (for video file) 46 | * - \%r => rotation (int degree) 47 | * - \%. => shear deformation in x coordinate 48 | * - \%, => shear deformation in y coordinate 49 | * @param dirname The directoryname 50 | * @param filename The filename excluding file extension 51 | * @param extension The file extension 52 | * @param x 53 | * @param y 54 | * @param width 55 | * @param height 56 | * @param frame 57 | * @param rotation 58 | * @param shear_x 59 | * @param shear_y 60 | * @return string 61 | */ 62 | string icFormat(const string& format, const string& dirname, const string& filename, const string& extension, 63 | int x, int y, int width, int height, int frame = 0, int rotation = 0, int shear_x = 0, int shear_y = 0) 64 | { 65 | string ret = format; 66 | char tmp[2048]; 67 | char intkeys[] = { 'x', 'y', 'w', 'h', 'f', 'r', '.', ',' }; 68 | int intvals[] = { x, y, width, height, frame, rotation, shear_x, shear_y }; 69 | int nintkeys = 8; 70 | char strkeys[] = { 'i', 'e', 'd' }; 71 | std::string strvals[] = { filename, extension, dirname }; 72 | int nstrkeys = 3; 73 | for(int i = 0; i < nintkeys + nstrkeys; i++) { 74 | std::string::size_type start = ret.find("%"); 75 | if(start == std::string::npos) break; 76 | std::string::size_type minstrpos = std::string::npos; 77 | std::string::size_type minintpos = std::string::npos; 78 | int minstrkey = INT_MAX; int minintkey = INT_MAX; 79 | for(int j = 0; j < nstrkeys; j++) { 80 | std::string::size_type pos = ret.find(strkeys[j], start); 81 | if(pos < minstrpos) { 82 | minstrpos = pos; 83 | minstrkey = j; 84 | } 85 | } 86 | for(int j = 0; j < nintkeys; j++) { 87 | std::string::size_type pos = ret.find(intkeys[j], start); 88 | if(pos < minintpos) { 89 | minintpos = pos; 90 | minintkey = j; 91 | } 92 | } 93 | if(minstrpos == std::string::npos && minintpos == std::string::npos) break; 94 | if(minstrpos < minintpos) { 95 | string format_substr = ret.substr(start, minstrpos - start) + "s"; 96 | std::sprintf(tmp, format_substr.c_str(), strvals[minstrkey].c_str()); 97 | ret.replace(start, minstrpos - start + 1, string(tmp)); 98 | } else { 99 | string format_substr = ret.substr(start, minintpos - start) + "d"; 100 | std::sprintf(tmp, format_substr.c_str(), intvals[minintkey]); 101 | ret.replace(start, minintpos - start + 1, string(tmp)); 102 | } 103 | } 104 | return ret; 105 | } 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /src/opencvx/cvsandwichfill.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CLOSING_INCLUDED 25 | #define CV_CLOSING_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | CVAPI(void) cvSandwichFill( const IplImage* src, IplImage* dst ); 32 | 33 | /** 34 | // cvSandwichFill - Search boundary (non-zero pixel) from both side and fill inside 35 | // 36 | // @param IplImage* src One channel image with 0 or 1 value (mask image) 37 | // @param IplImage* dst 38 | // @see cvSmooth( src, dst, CV_MEDIAN, 3 ) 39 | // @see cvClosing( src, dst, NULL, 3 ) 40 | */ 41 | CVAPI(void) cvSandwichFill( const IplImage* src, IplImage* dst ) 42 | { 43 | cvCopy( src, dst ); 44 | for( int y = 0; y < dst->height; y++ ) 45 | { 46 | int start = -1; 47 | int end = -1; 48 | for( int x = 0; x < dst->width - 1; x++) 49 | { 50 | int p1 = dst->imageData[dst->widthStep * y + x]; 51 | int p2 = dst->imageData[dst->widthStep * y + x + 1]; 52 | if( p1 > 0 && p2 > 0 ) 53 | { 54 | start = x; 55 | break; 56 | } 57 | } 58 | for( int x = dst->width - 1; x > start; x--) 59 | { 60 | int p1 = dst->imageData[dst->widthStep * y + x]; 61 | int p2 = dst->imageData[dst->widthStep * y + x + 1]; 62 | if( p1 > 0 && p2 > 0 ) 63 | { 64 | end = x; 65 | break; 66 | } 67 | } 68 | if( start != -1 && end != -1 ) 69 | { 70 | for( int x = start; x <= end; x++) 71 | { 72 | dst->imageData[dst->widthStep * y + x] = 1; 73 | } 74 | } 75 | } 76 | for( int x = 0; x < dst->width; x++ ) 77 | { 78 | int start = -1; 79 | int end = -1; 80 | for( int y = 0; y < dst->height - 1; y++) 81 | { 82 | int p1 = dst->imageData[dst->widthStep * y + x]; 83 | int p2 = dst->imageData[dst->widthStep * y + x + 1]; 84 | if( p1 > 0 && p2 > 0 ) 85 | { 86 | start = y; 87 | break; 88 | } 89 | } 90 | for( int y = dst->height - 1; y > start; y--) 91 | { 92 | int p1 = dst->imageData[dst->widthStep * y + x]; 93 | int p2 = dst->imageData[dst->widthStep * y + x + 1]; 94 | if( p1 > 0 && p2 > 0 ) 95 | { 96 | end = y; 97 | break; 98 | } 99 | } 100 | if( start != -1 && end != -1 ) 101 | { 102 | for( int y = start; y <= end; y++) 103 | { 104 | dst->imageData[dst->widthStep * y + x] = 1; 105 | } 106 | } 107 | } 108 | //// Tried to use cvFindContours, but did not work for disconnected contours. 109 | //CvMemStorage* storage = cvCreateMemStorage(0); 110 | //CvSeq* contour = 0; 111 | //cvFindContours( dst, storage, &contour, 112 | // sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); 113 | //for( ; contour != 0; contour = contour->h_next ) 114 | //{ 115 | // CvScalar color = cvScalar(255); 116 | // cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 ); 117 | //} 118 | } 119 | 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /src/opencvx/cvcat.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CAT_INCLUDED 25 | #define CV_CAT_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | #include "cvsetrow.h" 31 | #include "cvsetcol.h" 32 | 33 | CVAPI( void ) cvCat( const CvArr* src1arr, const CvArr* src2arr, CvArr* dstarr, int dim = -1 ); 34 | #define cvHcat( src1, src2, dst ) cvCat( (src1), (src2), (dst), 0 ) 35 | #define cvVcat( src1, src2, dst ) cvCat( (src1), (src2), (dst), 1 ) 36 | 37 | /** 38 | * Concatinate arrays 39 | * 40 | * Example) 41 | * IplImage* img = cvCreateImage( cvSize(4,4), IPL_DEPTH_8U, 1 ); // width(cols), height(rows) 42 | * IplImage* subimg = cvCreateImage( cvSize(1,4), IPL_DEPTH_8U, 1 ); 43 | * IplImage* catimg = cvCreateImage( cvSize(5,4), IPL_DEPTH_8U, 1 ); 44 | * cvSet( img, cvScalar(1) ); 45 | * cvSet( subimg, cvScalar(0) ); 46 | * cvHcat( img, subimg, catimg ); // 4x4 + 4x1 = 4x5 47 | * cvMatPrint( catimg ); 48 | * 49 | * @param src1 Input array 1 50 | * @param src2 Input array 2 51 | * @param dst Target array 52 | * @param dim 0 horizontally, 1 vertically, -1 flexibly 53 | * @see cvHcat( src1, src2, dst ) // cvCat( src1, src2, dst, 0 ) 54 | * @see cvVcat( src1, src2, dst ) // cvCat( src1, src2, dst, 1 ) 55 | */ 56 | //CV_IMPL void 57 | CVAPI( void ) cvCat( const CvArr* src1arr, const CvArr* src2arr, CvArr* dstarr, int dim ) 58 | { 59 | int coi = 0; 60 | CvMat *src1 = (CvMat*)src1arr, src1stub; 61 | CvMat *src2 = (CvMat*)src2arr, src2stub; 62 | CvMat *dst = (CvMat*)dstarr, dststub; 63 | CV_FUNCNAME( "cvCat" ); 64 | __CV_BEGIN__; 65 | if( !CV_IS_MAT(src1) ) 66 | { 67 | src1 = cvGetMat( src1, &src1stub, &coi ); 68 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 69 | } 70 | if( !CV_IS_MAT(src2) ) 71 | { 72 | src2 = cvGetMat( src2, &src2stub, &coi ); 73 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 74 | } 75 | if( !CV_IS_MAT(dst) ) 76 | { 77 | dst = cvGetMat( dst, &dststub, &coi ); 78 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 79 | } 80 | if( dim == -1 ) 81 | { 82 | if( src1->rows == src2->rows && src1->rows == dst->rows ) 83 | { 84 | dim = 0; 85 | } 86 | else if( src1->cols == src2->cols && src1->cols == dst->cols ) 87 | { 88 | dim = 1; 89 | } 90 | else 91 | { 92 | CV_ERROR( CV_StsBadArg, "The size of matrices does not fit to concatinate." ); 93 | } 94 | } 95 | if( dim == 0 ) // horizontal cat 96 | { 97 | CV_ASSERT( src1->rows == src2->rows && src1->rows == dst->rows ); 98 | CV_ASSERT( src1->cols + src2->cols == dst->cols ); 99 | } 100 | else if( dim == 1 ) // vertical cat 101 | { 102 | CV_ASSERT( src1->cols == src2->cols && src1->cols == dst->cols ); 103 | CV_ASSERT( src1->rows + src2->rows == dst->rows ); 104 | } 105 | else 106 | { 107 | CV_ERROR( CV_StsBadArg, "The dim is 0 (horizontal) or 1 (vertical) or -1 (flexible)." ); 108 | } 109 | 110 | if( dim == 0 ) // horizontal cat 111 | { 112 | cvSetCols( src1, dst, 0, src1->cols ); 113 | cvSetCols( src2, dst, src1->cols, src1->cols + src2->cols ); 114 | } 115 | else // vertical cat 116 | { 117 | cvSetRows( src1, dst, 0, src1->rows ); 118 | cvSetRows( src2, dst, src1->rows, src1->rows + src2->rows ); 119 | } 120 | __CV_END__; 121 | } 122 | 123 | 124 | #endif 125 | 126 | -------------------------------------------------------------------------------- /src/opencvx/cvgaussnorm.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_GAUSSNORM_INCLUDED 25 | #define CV_GAUSSNORM_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | #include "cvmatelemcn.h" 34 | 35 | // @todo 36 | // void cvMatGaussNorm( const CvMat* samples, CvMat* dst ); 37 | // #define cvGaussNorm( sample, dst ) cvMatGaussNorm( sample, dst ) 38 | // void cvImgGaussNorm( const IplImage* img, IplImage* normed ) { 39 | // IplImage* sample, samplehdr; 40 | // IplImage* dst, dsthdr; 41 | // sample = cvReshape( img, &samplehdr, 1, img->nChannels ); 42 | // dst = cvReshape( normed, &dsthdr, 1, dst->nChannels ); 43 | // // make sure how it is reshaped 44 | // cvMatGaussNorm( sample, dst ); 45 | // } 46 | 47 | void cvImgGaussNorm( const CvArr* img, CvArr* normed ); 48 | 49 | /** 50 | // cvImgGaussNorm - Zero mean and unit covariance normalization of an image 51 | // Each channel is processed independently 52 | // 53 | // @param img input image 54 | // @param normed normalized image. 32F or 64F should be preferred. 55 | // @return void 56 | */ 57 | void cvImgGaussNorm( const CvArr* img, CvArr* normed ) 58 | { 59 | CvMat instub, *in = (CvMat*)img; 60 | CvMat outstub, *out = (CvMat*)normed; 61 | int coi = 0; 62 | CvScalar mean, std; 63 | int rows, cols, nChannels; 64 | int ch, row, col; 65 | CvMat *tmp_in; 66 | CvMat *sub_in; 67 | CV_FUNCNAME( "cvImgGaussNorm" ); 68 | __CV_BEGIN__; 69 | if( !CV_IS_MAT(in) ) 70 | { 71 | CV_CALL( in = cvGetMat( in, &instub, &coi ) ); 72 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 73 | } 74 | if( !CV_IS_MAT(out) ) 75 | { 76 | CV_CALL( out = cvGetMat( out, &outstub, &coi ) ); 77 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 78 | } 79 | CV_ASSERT( in->rows == out->rows && in->cols == out->cols ); 80 | CV_ASSERT( CV_MAT_CN(in->type) == CV_MAT_CN(out->type) ); 81 | 82 | if( in->type != out->type ) { 83 | tmp_in = cvCreateMat( out->rows, out->cols, out->type ); 84 | cvConvert( in, tmp_in ); 85 | } else { 86 | tmp_in = in; 87 | } 88 | sub_in = cvCreateMat( out->rows, out->cols, out->type ); 89 | 90 | cvAvgSdv( tmp_in, &mean, &std ); 91 | cvSubS( tmp_in, mean, sub_in ); 92 | //cvScale( sub_in, out, 1.0/std.val[0] ); 93 | rows = out->rows; 94 | cols = out->cols; 95 | nChannels = CV_MAT_CN(out->type); 96 | if( CV_MAT_DEPTH(out->type) == CV_64F ) { 97 | for( ch = 0; ch < nChannels; ch++ ) { 98 | for( row = 0; row < rows; row++ ) { 99 | for( col = 0; col < cols; col++ ) { 100 | CV_MAT_ELEM_CN( *out, double, row, col * nChannels + ch ) 101 | = CV_MAT_ELEM_CN( *sub_in, double, row, col * nChannels + ch ) / std.val[ch]; 102 | } 103 | } 104 | } 105 | } else if( CV_MAT_DEPTH(out->type) == CV_32F ) { 106 | for( ch = 0; ch < nChannels; ch++ ) { 107 | for( row = 0; row < rows; row++ ) { 108 | for( col = 0; col < cols; col++ ) { 109 | CV_MAT_ELEM_CN( *out, float, row, col * nChannels + ch ) 110 | = CV_MAT_ELEM_CN( *sub_in, float, row, col * nChannels + ch ) / std.val[ch]; 111 | } 112 | } 113 | } 114 | } 115 | 116 | if( in->type != out->type ) { 117 | cvReleaseMat( &tmp_in ); 118 | } 119 | cvReleaseMat( &sub_in ); 120 | __CV_END__; 121 | } 122 | 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /src/opencvx/cvsetcol.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_SETCOL_INCLUDED 25 | #define CV_SETCOL_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | 31 | CV_INLINE void cvSetCols( const CvArr* src, CvArr* dst, 32 | int start_col, int end_col ); 33 | #define cvSetCol(src, dst, col) (cvSetCols( src, dst, col, col + 1)) 34 | 35 | /** 36 | * Set array col or col span 37 | * 38 | * Following code is faster than using this function because it does not 39 | * require cvCopy() 40 | * 41 | * CvMat* submat, submathdr; 42 | * submat = cvGetCols( mat, &submathdr, start_col, end_col, delta_col ); 43 | * // Write on submat 44 | * 45 | * 46 | * @param src Source array 47 | * @param dst Target array. Either of array must be size of setting cols. 48 | * @param start_col Zero-based index of the starting col (inclusive) of the span. 49 | * @param end_col Zero-based index of the ending col (exclusive) of the span. 50 | * @return void 51 | * @see cvSetCol( src, dst, col ) // cvSetCols( src, dst, col, col + 1 ) 52 | */ 53 | CV_INLINE void cvSetCols( const CvArr* src, CvArr* dst, 54 | int start_col, int end_col ) 55 | { 56 | int coi; 57 | CvMat *srcmat = (CvMat*)src, srcmatstub; 58 | CvMat *dstmat = (CvMat*)dst, dstmatstub; 59 | CvMat *refmat, refmathdr; 60 | int cols; 61 | CV_FUNCNAME( "cvSetCols" ); 62 | __CV_BEGIN__; 63 | if( !CV_IS_MAT(dstmat) ) 64 | { 65 | CV_CALL( dstmat = cvGetMat( dstmat, &dstmatstub, &coi ) ); 66 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 67 | } 68 | if( !CV_IS_MAT(srcmat) ) 69 | { 70 | CV_CALL( srcmat = cvGetMat( srcmat, &srcmatstub, &coi ) ); 71 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 72 | } 73 | cols = end_col - start_col; 74 | CV_ASSERT( srcmat->cols == cols || dstmat->cols == cols ); 75 | if( srcmat->cols == cols ) 76 | { 77 | refmat = cvGetCols( dstmat, &refmathdr, start_col, end_col ); 78 | cvCopy( srcmat, refmat ); 79 | } 80 | else 81 | { 82 | refmat = cvGetCols( srcmat, &refmathdr, start_col, end_col ); 83 | cvCopy( refmat, dstmat ); 84 | } 85 | __CV_END__; 86 | } 87 | 88 | /* 89 | CVAPI( void ) cvSetCols( const CvArr* subarr, CvArr* arr, int start_col, int end_col ) 90 | { 91 | CV_FUNCNAME( "cvSetCols" ); 92 | __CV_BEGIN__; 93 | int col, row, elem; 94 | int coi = 0; 95 | CvMat* submat = (CvMat*)subarr, submatstub; 96 | CvMat* mat = (CvMat*)arr, matstub; 97 | 98 | if( !CV_IS_MAT(submat) ) 99 | { 100 | CV_CALL( submat = cvGetMat( submat, &submatstub, &coi ) ); 101 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 102 | } 103 | if( !CV_IS_MAT(mat) ) 104 | { 105 | CV_CALL( mat = cvGetMat( mat, &matstub, &coi ) ); 106 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 107 | } 108 | CV_ARE_TYPES_EQ( submat, mat ); 109 | CV_ARE_DEPTHS_EQ( submat, mat ); 110 | CV_ASSERT( 0 <= start_col && end_col <= mat->cols ); 111 | CV_ASSERT( end_col - start_col == submat->cols ); 112 | CV_ASSERT( mat->rows == submat->rows ); 113 | 114 | int elem_size = CV_ELEM_SIZE( mat->type ); 115 | for( col = start_col; col < end_col; col++ ) 116 | { 117 | for( row = 0; row < mat->rows; row++ ) 118 | { 119 | for( elem = 0; elem < elem_size; elem++ ) 120 | { 121 | (mat->data.ptr + ((size_t)mat->step * row) + (elem_size * col))[elem] = 122 | (submat->data.ptr + ((size_t)submat->step * row) + (elem_size * (col - start_col)))[elem]; 123 | } 124 | } 125 | } 126 | __CV_END__; 127 | } 128 | */ 129 | 130 | #endif 131 | 132 | -------------------------------------------------------------------------------- /src/opencvx/cvbackground.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_BACKGROUND_INCLUDED 25 | #define CV_BACKGROUND_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | CVAPI(void) cvBackground( const IplImage* _img, const IplImage* _ref, IplImage* _mask, int thresh = 100 ); 32 | 33 | /** 34 | // Obtain non-background pixels using reference image (such as previous frame in video ) 35 | // 36 | // @param mg The target image 37 | // @param ref The reference image. Usually the previous frame of video 38 | // @param mask The generated mask image where 0 is for bg and 1 is for non-bg. Must be 8U and 1 channel 39 | // @param [thresh = 100] The threshold. [0 - 255^2] for single channel image. [0 - 255^2 * 3] for 3 channel image. 40 | // @return void 41 | */ 42 | CVAPI(void) cvBackground( const IplImage* _img, const IplImage* _ref, IplImage* _mask, int thresh ) 43 | { 44 | CV_FUNCNAME( "cvBackground" ); // error handling 45 | __CV_BEGIN__; 46 | CV_ASSERT( _img->width == _ref->width ); 47 | CV_ASSERT( _img->width == _mask->width ); 48 | CV_ASSERT( _img->height == _ref->height ); 49 | CV_ASSERT( _img->height == _ref->height ); 50 | CV_ASSERT( _img->nChannels == _ref->nChannels ); 51 | CV_ASSERT( _mask->nChannels == 1 ); 52 | CV_ASSERT( _mask->depth == IPL_DEPTH_8U ); 53 | 54 | IplImage *img = cvCreateImage( cvGetSize(_img), IPL_DEPTH_32F, _img->nChannels ); 55 | IplImage *ref = cvCreateImage( cvGetSize(_img), IPL_DEPTH_32F, _img->nChannels ); 56 | IplImage *mask = cvCreateImage( cvGetSize(_img), IPL_DEPTH_32F, 1 ); 57 | cvConvert( _img, img ); 58 | cvConvert( _ref, ref ); 59 | cvSub( img, ref, img ); 60 | cvMul( img, img, img ); // square 61 | 62 | if( img->nChannels > 1 ) 63 | { 64 | IplImage* imgB = cvCreateImage( cvGetSize(img), img->depth, 1 ); 65 | IplImage* imgG = cvCreateImage( cvGetSize(img), img->depth, 1 ); 66 | IplImage* imgR = cvCreateImage( cvGetSize(img), img->depth, 1 ); 67 | cvSplit( img, imgB, imgG, imgR, NULL ); 68 | cvAdd( imgB, imgG, mask ); 69 | cvAdd( mask, imgR, mask ); 70 | cvReleaseImage( &imgB ); 71 | cvReleaseImage( &imgG ); 72 | cvReleaseImage( &imgR ); 73 | } 74 | else 75 | { 76 | cvCopy( img, mask ); 77 | } 78 | cvThreshold( mask, _mask, thresh, 1, CV_THRESH_BINARY ); 79 | 80 | cvReleaseImage( &img ); 81 | cvReleaseImage( &ref ); 82 | cvReleaseImage( &mask ); 83 | __CV_END__; 84 | } 85 | 86 | /* 87 | //this is a sample for foreground detection functions 88 | //this is not training, but using pre-determined params 89 | //See cvCreateGaussainBGModel in ./cvaux/src/cvbgfg_gaussmix.cpp 90 | //This is setting paramets such as CV_BGFG_MOG_BACKGROUND_THRESHOLD (cvaux.h) 91 | //cvUpdateBGStatModel means just setting bg_model->foreground, background 92 | int main(int argc, char** argv) 93 | { 94 | IplImage* tmp_frame = NULL; 95 | CvCapture* video = NULL; 96 | 97 | video = cvCaptureFromFile(argv[1]); 98 | tmp_frame = cvQueryFrame(video); 99 | if(!tmp_frame) 100 | { 101 | printf("bad video \n"); 102 | exit(0); 103 | } 104 | 105 | cvNamedWindow("BG", 1); 106 | cvNamedWindow("FG", 1); 107 | 108 | //create BG model 109 | CvBGStatModel* bg_model = cvCreateGaussianBGModel( tmp_frame ); 110 | 111 | for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(video), fr++ ) 112 | { 113 | cvUpdateBGStatModel( tmp_frame, bg_model ); 114 | cvShowImage("BG", bg_model->background); 115 | cvShowImage("FG", bg_model->foreground); 116 | int k = cvWaitKey(5); 117 | if( k == 27 ) break; 118 | //printf("frame# %d \r", fr); 119 | } 120 | 121 | 122 | cvReleaseBGStatModel( &bg_model ); 123 | cvReleaseCapture(&video); 124 | 125 | return 0; 126 | }*/ 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /src/opencvx/cvskincolorcbcr.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvskincolorcbcr.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | #ifndef CV_SKINCOLOR_CBCR_INCLUDED 15 | #define CV_SKINCOLOR_CBCR_INCLUDED 16 | 17 | 18 | #include 19 | #include 20 | #define _USE_MATH_DEFINES 21 | #include 22 | 23 | void cvSkinColorCrCb( const IplImage* _img, IplImage* mask, CvArr* distarr = NULL ); 24 | 25 | /** 26 | // cvSkinColorCbCr - Skin Color Detection in (Cb, Cr) space by [1][2] 27 | // 28 | // @param img Input image 29 | // @param mask Generated mask image. 1 for skin and 0 for others 30 | // @param [dist = NULL] The distortion valued array rather than mask if you want 31 | // 32 | // References) 33 | // [1] R.L. Hsu, M. Abdel-Mottaleb, A.K. Jain, "Face Detection in Color Images," 34 | // IEEE Transactions on Pattern Analysis and Machine Intelligence ,vol. 24, no. 5, 35 | // pp. 696-706, May, 2002. (Original) 36 | // [2] P. Peer, J. Kovac, J. and F. Solina, ”Human skin colour 37 | // clustering for face detection”, In: submitted to EUROCON – 38 | // International Conference on Computer as a Tool , 2003. (Tuned) 39 | */ 40 | void cvSkinColorCrCb( const IplImage* _img, IplImage* mask, CvArr* distarr ) 41 | { 42 | CV_FUNCNAME( "cvSkinColorCbCr" ); 43 | __CV_BEGIN__; 44 | int width = _img->width; 45 | int height = _img->height; 46 | CvMat* dist = (CvMat*)distarr, diststub; 47 | int coi = 0; 48 | IplImage* img; 49 | 50 | double Wcb = 46.97; 51 | double WLcb = 23; 52 | double WHcb = 14; 53 | double Wcr = 38.76; 54 | double WLcr = 20; 55 | double WHcr = 10; 56 | double Kl = 125; 57 | double Kh = 188; 58 | double Ymin = 16; 59 | double Ymax = 235; 60 | double alpha = 0.56; 61 | 62 | double Cx = 109.38; 63 | double Cy = 152.02; 64 | double theta = 2.53; 65 | double ecx = 1.6; 66 | double ecy = 2.41; 67 | double a = 25.39; 68 | double b = 14.03; 69 | 70 | CV_ASSERT( width == mask->width && height == mask->height ); 71 | CV_ASSERT( _img->nChannels >= 3 && mask->nChannels == 1 ); 72 | 73 | if( dist && !CV_IS_MAT(dist) ) 74 | { 75 | CV_CALL( dist = cvGetMat( dist, &diststub, &coi ) ); 76 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 77 | CV_ASSERT( width == dist->cols && height == dist->rows ); 78 | CV_ASSERT( CV_MAT_TYPE(dist->type) == CV_32FC1 || CV_MAT_TYPE(dist->type) == CV_64FC1 ); 79 | } 80 | 81 | img = cvCreateImage( cvGetSize(_img), IPL_DEPTH_8U, 3 ); 82 | cvCvtColor( _img, img, CV_BGR2YCrCb ); 83 | 84 | cvSet( mask, cvScalarAll(0) ); 85 | for( int row = 0; row < height; row++ ) 86 | { 87 | for( int col = 0; col < width; col++ ) 88 | { 89 | uchar Y = img->imageData[img->widthStep * row + col * 3]; 90 | uchar Cr = img->imageData[img->widthStep * row + col * 3 + 1]; 91 | uchar Cb = img->imageData[img->widthStep * row + col * 3 + 2]; 92 | 93 | double Cb_Y, Cr_Y, Wcb_Y, Wcr_Y; 94 | if( Y < Kl ) 95 | Wcb_Y = WLcb + (Y - Ymin) * (Wcb - WLcb) / (Kl - Ymin); 96 | else if( Kh < Y ) 97 | Wcb_Y = WHcb + (Ymax - Y) * (Wcb - WHcb) / (Ymax - Kh); 98 | else 99 | Wcb_Y = Wcb; 100 | 101 | if( Y < Kl ) 102 | Wcr_Y = WLcr + (Y - Ymin) * (Wcr - WLcr) / (Kl - Ymin); 103 | else if( Kh < Y ) 104 | Wcr_Y = WHcr + (Ymax - Y) * (Wcr - WHcr) / (Ymax - Kh); 105 | else 106 | Wcr_Y = Wcr; 107 | 108 | if( Y < Kl ) 109 | Cb_Y = 108 + (Kl - Y) * 10 / ( Kl - Ymin ); 110 | else if( Kh < Y ) 111 | Cb_Y = 108 + (Y - Kh) * 10 / ( Ymax - Kh ); 112 | else 113 | Cb_Y = 108; 114 | 115 | if( Y < Kl ) 116 | Cr_Y = 154 - (Kl - Y) * 10 / ( Kl - Ymin ); 117 | else if( Kh < Y ) 118 | Cr_Y = 154 + (Y - Kh) * 22 / ( Ymax - Kh ); 119 | else 120 | Cr_Y = 108; 121 | 122 | double x = cos(theta) * (Cb - Cx) + sin(theta) * (Cr - Cy); 123 | double y = -1 * sin(theta) * (Cb - Cx) + cos(theta) * (Cr - Cy); 124 | 125 | double distort = pow(x-ecx,2) / pow(a,2) + pow(y-ecy,2) / pow(b,2); 126 | if( dist ) 127 | cvmSet( dist, row, col, distort ); 128 | 129 | if( distort <= 1 ) 130 | { 131 | mask->imageData[mask->widthStep * row + col] = 1; 132 | } 133 | } 134 | } 135 | __CV_END__; 136 | } 137 | 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /src/opencvx/cvgmmpdf.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | // cvgmmpdf.h (Gaussian Mixture Model) 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | // 14 | // @requirements cvgausspdf.h 15 | */ 16 | #ifndef CV_GMMPDF_INCLUDED 17 | #define CV_GMMPDF_INCLUDED 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #define _USE_MATH_DEFINES 25 | #include 26 | 27 | #include "cvgausspdf.h" 28 | 29 | /** 30 | // cvMatGmmPdf - compute gaussian mixture pdf for a set of sample vectors 31 | // 32 | // Example) 33 | // const int D = 2; 34 | // const int N = 3; 35 | // const int K = 2; 36 | // 37 | // double vs[] = { 3, 4, 5, 38 | // 3, 4, 5 }; // col vectors 39 | // double ms[] = { 3, 5, 40 | // 3, 5 }; // col vectors 41 | // double cs0[] = { 1, 0, 42 | // 0, 1 }; 43 | // double cs1[] = { 1, 0.1, 44 | // 0.1, 1 }; 45 | // double ws[] = { 0.5, 0.5 }; 46 | // 47 | // CvMat vecs = cvMat(D, N, CV_64FC1, vs); 48 | // CvMat means = cvMat(D, K, CV_64FC1, ms); 49 | // CvMat **covs = (CvMat**)cvAlloc( K * sizeof(*covs) ); 50 | // covs[0] = &cvMat(D, D, CV_64FC1, cs0); 51 | // covs[1] = &cvMat(D, D, CV_64FC1, cs0); 52 | // CvMat weights = cvMat( 1, K, CV_64FC1, ws); 53 | // CvMat *probs = cvCreateMat(K, N, CV_64FC1); 54 | // cvMatGmmPdf( &vecs, &means, covs, &weights, probs, false); 55 | // cvMatPrint( probs ); 56 | // cvReleaseMat( &probs ); 57 | // cvFree( &covs ); 58 | // 59 | // @param samples D x N data vector (Note: not N x D for clearness of matrix operation) 60 | // @param means D x K mean vector 61 | // @param covs (D x D) x K covariance matrix for each cluster 62 | // @param weights 1 x K weights 63 | // @param probs K x N or 1 x N computed probabilites 64 | // @param [normalize = false] Compute normalization term or not 65 | // @uses cvMatGaussPdf 66 | */ 67 | void cvMatGmmPdf( const CvMat* samples, const CvMat* means, CvMat** covs, const CvMat* weights, CvMat* probs, bool normalize = false ) 68 | { 69 | int D = samples->rows; 70 | int N = samples->cols; 71 | int K = means->cols; 72 | int type = samples->type; 73 | CV_FUNCNAME( "cvMatGmmPdf" ); // error handling 74 | __CV_BEGIN__; 75 | CV_ASSERT( CV_IS_MAT(samples) ); 76 | CV_ASSERT( CV_IS_MAT(means) ); 77 | for( int k = 0; k < K; k++ ) 78 | CV_ASSERT( CV_IS_MAT(covs[k]) ); 79 | CV_ASSERT( CV_IS_MAT(weights) ); 80 | CV_ASSERT( CV_IS_MAT(probs) ); 81 | CV_ASSERT( D == means->rows ); 82 | for( int k = 0; k < K; k++ ) 83 | CV_ASSERT( D == covs[k]->rows && D == covs[k]->cols ); // D x D 84 | CV_ASSERT( 1 == weights->rows && K == weights->cols ); // 1 x K 85 | CV_ASSERT( ( 1 == probs->rows || K == probs->rows ) && N == probs->cols ); // 1 x N or K x N 86 | 87 | CvMat *mean = cvCreateMat( D, 1, type ); 88 | const CvMat *cov; 89 | CvMat *_probs = cvCreateMat( 1, N, type ); 90 | cvZero( probs ); 91 | for( int k = 0; k < K; k++ ) 92 | { 93 | cvGetCol( means, mean, k ); 94 | cov = covs[k]; 95 | cvMatGaussPdf( samples, mean, cov, _probs, normalize ); 96 | cvConvertScale( _probs, _probs, cvmGet( weights, 0, k ) ); 97 | if( 1 == probs->rows ) 98 | { 99 | cvAdd( probs, _probs, probs ); 100 | } 101 | else 102 | { 103 | for( int n = 0; n < N; n++ ) 104 | { 105 | cvmSet( probs, k, n, cvmGet( _probs, 0, n ) ); 106 | } 107 | } 108 | } 109 | 110 | cvReleaseMat( &mean ); 111 | cvReleaseMat( &_probs ); 112 | 113 | __CV_END__; 114 | } 115 | 116 | /** 117 | // cvGmmPdf - compute gaussian mixture pdf 118 | // 119 | // @param sample D x 1 sample vector 120 | // @param means D x K mean vector for each cluster 121 | // @param covs (D x D) x K covariance matrix for each cluster 122 | // @param weights 1 x K weights 123 | // @param probs K x 1 probabilities for each cluster if want 124 | // @param [normalize = false] use normalization term or not 125 | // @return double prob 126 | // @uses cvMatGmmPdf 127 | */ 128 | double cvGmmPdf( const CvMat* sample, const CvMat* means, CvMat** covs, const CvMat* weights, CvMat* probs = NULL, bool normalize = false ) 129 | { 130 | double prob; 131 | CvMat* _probs; 132 | int K = means->cols; 133 | if( probs ) 134 | _probs = probs; 135 | else 136 | _probs = cvCreateMat( K, 1, sample->type ); 137 | 138 | cvMatGmmPdf( sample, means, covs, weights, _probs, normalize ); 139 | prob = cvSum( _probs ).val[0]; 140 | 141 | if( !probs ) 142 | cvReleaseMat( &probs ); 143 | return prob; 144 | } 145 | 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /src/opencvx/cvsetrow.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_SETROW_INCLUDED 25 | #define CV_SETROW_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | 31 | CV_INLINE void cvSetRows( const CvArr* src, CvArr* dst, 32 | int start_row, int end_row, int delta_row = 1 ); 33 | #define cvSetRow(src, dst, row) (cvSetRows( src, dst, row, row + 1)) 34 | 35 | /** 36 | * Set array row or row span 37 | * 38 | * Following code is faster than using this function because it does not 39 | * require cvCopy() 40 | * 41 | * CvMat* submat, submathdr; 42 | * submat = cvGetRows( mat, &submathdr, start_row, end_row, delta_row ); 43 | * // Write on submat 44 | * 45 | * 46 | * @param src Source array 47 | * @param dst Target array. Either of array must be size of setting rows. 48 | * @param start_row Zero-based index of the starting row (inclusive) of the span. 49 | * @param end_row Zero-based index of the ending row (exclusive) of the span. 50 | * @param [delta_row = 1] 51 | * Index step in the row span. That is, the function extracts every 52 | * delta_row-th row from start_row and up to (but not including) end_row. 53 | * @return void 54 | * @see cvSetRow( src, dst, row ) // cvSetCols( src, dst, row, row + 1 ) 55 | */ 56 | CV_INLINE void cvSetRows( const CvArr* src, CvArr* dst, 57 | int start_row, int end_row, int delta_row ) 58 | { 59 | int coi; 60 | CvMat *srcmat = (CvMat*)src, srcmatstub; 61 | CvMat *dstmat = (CvMat*)dst, dstmatstub; 62 | CvMat *refmat, refmathdr; 63 | int rows; 64 | CV_FUNCNAME( "cvSetRows" ); 65 | __CV_BEGIN__; 66 | if( !CV_IS_MAT(dstmat) ) 67 | { 68 | CV_CALL( dstmat = cvGetMat( dstmat, &dstmatstub, &coi ) ); 69 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 70 | } 71 | if( !CV_IS_MAT(srcmat) ) 72 | { 73 | CV_CALL( srcmat = cvGetMat( srcmat, &srcmatstub, &coi ) ); 74 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 75 | } 76 | rows = cvFloor( ( end_row - start_row ) / delta_row ); 77 | CV_ASSERT( srcmat->rows == rows || dstmat->rows == rows ); 78 | if( srcmat->rows == rows ) 79 | { 80 | refmat = cvGetRows( dstmat, &refmathdr, start_row, end_row, delta_row ); 81 | cvCopy( srcmat, refmat ); 82 | } 83 | else 84 | { 85 | refmat = cvGetRows( srcmat, &refmathdr, start_row, end_row, delta_row ); 86 | cvCopy( refmat, dstmat ); 87 | } 88 | __CV_END__; 89 | } 90 | /* 91 | CVAPI( void ) cvSetRows( const CvArr* subarr, CvArr* arr, int start_row, int end_row ) 92 | { 93 | CV_FUNCNAME( "cvSetRows" ); 94 | __CV_BEGIN__; 95 | int row, col, elem; 96 | int coi = 0; 97 | CvMat* submat = (CvMat*)subarr, submatstub; 98 | CvMat* mat = (CvMat*)arr, matstub; 99 | 100 | if( !CV_IS_MAT(submat) ) 101 | { 102 | CV_CALL( submat = cvGetMat( submat, &submatstub, &coi ) ); 103 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 104 | } 105 | if( !CV_IS_MAT(mat) ) 106 | { 107 | CV_CALL( mat = cvGetMat( mat, &matstub, &coi ) ); 108 | if (coi != 0) CV_ERROR_FROM_CODE(CV_BadCOI); 109 | } 110 | CV_ARE_TYPES_EQ( submat, mat ); // cxtypes.h 111 | //CV_ARE_DEPTHS_EQ( submat, mat ); 112 | //CV_ARE_CNS_EQ( submat, mat ); 113 | //CV_ARE_SIZES_EQ( submat, mat ); 114 | CV_ASSERT( 0 <= start_row && end_row <= mat->rows ); 115 | CV_ASSERT( end_row - start_row == submat->rows ); 116 | CV_ASSERT( mat->cols == submat->cols ); 117 | 118 | int elem_size = CV_ELEM_SIZE( mat->type ); 119 | // FYI: CV_ELEM_SIZE returns not number_of_channels, but number_of_uchar* 120 | // nChannels = CV_MAT_CN( mat->type ); refer cxarray.cpp#cvRawDataToScalar to separate into channel values 121 | for( row = start_row; row < end_row; row++ ) 122 | { 123 | for( col = 0; col < mat->cols; col++ ) 124 | { 125 | //cvSet2D( mat, row, col, cvGet2D( submat, row - start_row, col ) ); 126 | for( elem = 0; elem < elem_size; elem++ ) 127 | { 128 | // cvPtr2D( mat, row, col )[elem] = cvPtr2D( submat, row - start_row, col )[elem]; 129 | (mat->data.ptr + ((size_t)mat->step * row) + (elem_size * col))[elem] = 130 | (submat->data.ptr + ((size_t)submat->step * (row - start_row)) + (elem_size * col))[elem]; 131 | } 132 | } 133 | } 134 | __CV_END__; 135 | } 136 | */ 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /src/opencvx/cvmxtypeconv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * OpenCV versus Matlab C Library (MEX) interfaces 3 | * verified under OpenCV 1.00 and Matlab 2007b 4 | * 5 | * Type conversion 6 | * 7 | * The MIT License 8 | * 9 | * Copyright (c) 2008, Naotoshi Seo 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy 12 | * of this software and associated documentation files (the "Software"), to deal 13 | * in the Software without restriction, including without limitation the rights 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | * copies of the Software, and to permit persons to whom the Software is 16 | * furnished to do so, subject to the following conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in 19 | * all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 | * THE SOFTWARE. 28 | */ 29 | #ifndef CVMX_TYPECONV_INCLUDED 30 | #define CVMX_TYPECONV_INCLUDED 31 | 32 | 33 | #include 34 | #include "mat.h" 35 | #include "matrix.h" 36 | #include "cvipltocvdepth.h" 37 | 38 | /************** Definitions *******************************/ 39 | #define cvmxCvToIplDepth(type) (cvCvToIplDepth(type)) 40 | #define cvmxIplToCvDepth(depth) (cvIplToCvDepth(depth)) 41 | CVAPI(int) cvmxIplToCvDepth(int depth); 42 | CVAPI(mxClassID) cvmxClassIDFromIplDepth(int depth); 43 | CVAPI(int) cvmxClassIDToIplDepth(mxClassID classid); 44 | CV_INLINE mxClassID cvmxClassIDFromCvDepth(int type); 45 | CV_INLINE int cvmxClassIDToCvDepth(mxClassID classid); 46 | 47 | /********************* Memo *******************************/ 48 | /* 49 | * @reference cxcore/src/_cxcore.h#icvIplToCvDepth 50 | * @reference ./cxcore/src/cxtables.cpp 51 | */ 52 | /* 53 | #define CV_8U 0 54 | #define CV_8S 1 55 | #define CV_16U 2 56 | #define CV_16S 3 57 | #define CV_32S 4 58 | #define CV_32F 5 59 | #define CV_64F 6 60 | #define CV_USRTYPE1 7 */ 61 | /* 62 | #define IPL_DEPTH_SIGN 0x80000000 63 | #define IPL_DEPTH_1U 1 64 | #define IPL_DEPTH_8U 8 65 | #define IPL_DEPTH_16U 16 66 | #define IPL_DEPTH_32F 32 67 | #define IPL_DEPTH_64F 64 68 | #define IPL_DEPTH_8S (IPL_DEPTH_SIGN| 8) 69 | #define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16) 70 | #define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32) */ 71 | /* 72 | typedef enum { 73 | mxUNKNOWN_CLASS, 74 | mxCELL_CLASS, 75 | mxSTRUCT_CLASS, 76 | mxLOGICAL_CLASS, 77 | mxCHAR_CLASS, 78 | mxDOUBLE_CLASS, 79 | mxSINGLE_CLASS, 80 | mxINT8_CLASS, 81 | mxUINT8_CLASS, 82 | mxINT16_CLASS, 83 | mxUINT16_CLASS, 84 | mxINT32_CLASS, 85 | mxUINT32_CLASS, 86 | mxINT64_CLASS, 87 | mxUINT64_CLASS, 88 | mxFUNCTION_CLASS 89 | } mxClassID; */ 90 | 91 | /************** Functions *********************************/ 92 | 93 | /** 94 | * Get mxClassID from IplImage depth 95 | * 96 | * @param int depth IplImage depth 97 | * @return mxClassID 98 | * @see cvmxClassIDFromCvDepth 99 | * @see cvmxClassIDToCvDepth 100 | * @see cvmxClassIDFromIplDepth 101 | * @see cvmxClassIDToIplDepth 102 | * @see cvmxIplToCvDepth 103 | * @see cvCvToIplDepth 104 | */ 105 | CVAPI(mxClassID) cvmxClassIDFromIplDepth(int depth) 106 | { 107 | static const unsigned char ClassIDFromIplDepth[] = 108 | { 109 | 0, 0, mxUINT8_CLASS, mxINT8_CLASS, mxUINT16_CLASS, mxINT16_CLASS, 0, 0, 110 | mxSINGLE_CLASS, mxINT32_CLASS, 0, 0, 0, 0, 0, 0, mxDOUBLE_CLASS, 0 111 | }; 112 | if (depth == IPL_DEPTH_1U) { 113 | return mxLOGICAL_CLASS; 114 | } else { 115 | return (mxClassID)ClassIDFromIplDepth[(((depth) & 255) >> 2) + ((depth) < 0)]; 116 | } 117 | } 118 | 119 | /** 120 | * Convert mxClassID to IplImage depth 121 | * 122 | * @param int depth IplImage's depth 123 | * @return mxClassID 124 | * @see cvmxClassIDFromCvDepth 125 | * @see cvmxClassIDToCvDepth 126 | * @see cvmxClassIDFromIplDepth 127 | * @see cvmxClassIDToIplDepth 128 | * @see cvmxIplToCvDepth 129 | * @see cvCvToIplDepth 130 | */ 131 | CVAPI(int) cvmxClassIDToIplDepth(mxClassID classid) 132 | { 133 | static const signed int ClassIDToIplDepth[] = { 134 | 0, 135 | 0, 136 | 0, 137 | IPL_DEPTH_1U, 138 | 0, 139 | IPL_DEPTH_32F, 140 | IPL_DEPTH_64F, 141 | IPL_DEPTH_8S, 142 | IPL_DEPTH_8U, 143 | IPL_DEPTH_16S, 144 | IPL_DEPTH_16U, 145 | IPL_DEPTH_32S, 146 | 0, 147 | 0, 148 | 0, 149 | 0 150 | }; 151 | return ClassIDToIplDepth[classid]; 152 | } 153 | 154 | /** 155 | * Get mxClassID from cvMat type 156 | * 157 | * @param int cvMat type 158 | * @return mxClassID 159 | * @see cvmxClassIDFromCvDepth 160 | * @see cvmxClassIDToCvDepth 161 | * @see cvmxClassIDFromIplDepth 162 | * @see cvmxClassIDToIplDepth 163 | * @see cvmxIplToCvDepth 164 | * @see cvCvToIplDepth 165 | */ 166 | CV_INLINE mxClassID cvmxClassIDFromCvDepth(int type) 167 | { 168 | return cvmxClassIDFromIplDepth(cvCvToIplDepth(type)); 169 | } 170 | 171 | /** 172 | * Convert mxClassID to cvMat depth 173 | * 174 | * One may use CV_MAKETYPE(depth, nChannel) to create cvMat type 175 | * 176 | * @param mxClassID classid 177 | * @return int cvMat depth 178 | * @see cvmxClassIDFromCvDepth 179 | * @see cvmxClassIDToCvDepth 180 | * @see cvmxClassIDFromIplDepth 181 | * @see cvmxClassIDToIplDepth 182 | * @see cvmxIplToCvDepth 183 | * @see cvCvToIplDepth 184 | */ 185 | CV_INLINE int cvmxClassIDToCvDepth(mxClassID classid) 186 | { 187 | return cvmxIplToCvDepth(cvmxClassIDToIplDepth(classid)); 188 | } 189 | 190 | 191 | #endif 192 | -------------------------------------------------------------------------------- /haartrainingformat.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | use strict; 3 | use File::Basename; 4 | ################################################################################ 5 | # The MIT License 6 | # 7 | # Copyright (c) 2008, Naotoshi Seo 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in 17 | # all copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | # THE SOFTWARE. 26 | ################################################################################ 27 | 28 | sub usage { 29 | print 'Helper tool to convert a specific format into a haartraining format.' . "\n"; 30 | print 'Usage: perl haartrainingformat.pl [option]... [filename]' . "\n"; 31 | print ' filename' . "\n"; 32 | print ' file to be read. STDIN is also supported. ' . "\n"; 33 | print 'Options:' . "\n"; 34 | print ' -l' . "\n"; 35 | print ' --ls' . "\n"; 36 | print ' input is an output of ls.' . "\n"; 37 | print ' -t' . "\n"; 38 | print ' --trim' . "\n"; 39 | print ' trim heading 0 of numbers like from 00xx to xx.' . "\n"; 40 | print ' -b' . "\n"; 41 | print ' --basename' . "\n"; 42 | print ' get basenames from filenames.' . "\n"; 43 | print ' -h' . "\n"; 44 | print ' --help' . "\n"; 45 | print ' show this help.' . "\n"; 46 | print "\n"; 47 | print 'Example:' . "\n"; 48 | print ' $ cat a.txt' . "\n"; 49 | print ' image.jpg 68 47 89 101' . "\n"; 50 | print ' image.jpg 87 66 90 80' . "\n"; 51 | print ' image.jpg 95 105 33 32' . "\n"; 52 | print ' $ perl haartrainingformal.pl a.txt' . "\n"; 53 | print ' image.jpg 3 68 47 89 101 87 66 90 80 95 105 33 32' . "\n"; 54 | print "\n"; 55 | print ' $ \ls images/' . "\n"; 56 | print ' images/image.jpg_0068_0047_0089_0101.png' . "\n"; 57 | print ' images/image.jpg_0087_0066_0090_0080.png' . "\n"; 58 | print ' images/image.jpg_0095_0105_0033_0032.png' . "\n"; 59 | print ' $ \ls images/ | perl --ls --trim --basename haartrainingformal.pl' . "\n"; 60 | print ' image.jpg 3 68 47 89 101 87 66 90 80 95 105 33 32' . "\n"; 61 | } 62 | 63 | sub parse_args { 64 | my @ARGV = @_; 65 | my $infile = ""; 66 | my $lsformat = 0; 67 | my $trim = 0; # trim heading 0 or not (0010 => 10) 68 | my $basename = 0; 69 | for (my $i = 0; $i <= $#ARGV; $i++) { 70 | if ($ARGV[$i] eq "-t" || $ARGV[$i] eq "--trim" || $ARGV[$i] eq "-trim") { 71 | $trim = 1; 72 | } elsif ($ARGV[$i] eq "-b" || $ARGV[$i] eq "--basename" || $ARGV[$i] eq "-basename") { 73 | $basename = 1; 74 | } elsif ($ARGV[$i] eq "-l" || $ARGV[$i] eq "-ls" || $ARGV[$i] eq "--ls") { 75 | $lsformat = 1; 76 | } elsif ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") { 77 | &usage(); 78 | exit; 79 | } elsif (substr($ARGV[$i],0,1) eq "-") { 80 | print STDERR $ARGV[$i] . ': No such option exist' . "\n"; 81 | exit; 82 | } else { 83 | $infile = $ARGV[$i]; 84 | if (!-f $infile) { 85 | print STDERR $infile . ': No such file exist' . "\n"; 86 | } 87 | } 88 | } 89 | return ($infile, $lsformat, $trim, $basename); 90 | } 91 | 92 | sub ls2typical { 93 | my $trim = pop(@_); 94 | my @lines = @_; 95 | for (my $i = 0; $i <= $#lines; $i++) { 96 | if ($trim) { 97 | $lines[$i] =~ s/([^_]*).*_0*(\d+)_0*(\d+)_0*(\d+)_0*(\d+)\.[^.]*$/$1 $2 $3 $4 $5\n/g; 98 | } else { 99 | $lines[$i] =~ s/([^_]*).*_(\d+)_(\d+)_(\d+)_(\d+)\.[^.]*$/$1 $2 $3 $4 $5\n/g; 100 | } 101 | } 102 | return @lines; 103 | } 104 | 105 | sub typical2haartraining { 106 | my $basename = pop(@_); 107 | my $trim = pop(@_); 108 | my @lines = @_; 109 | my %counts = (); 110 | my %coords = (); 111 | foreach my $line (@lines) { 112 | $line =~ s/\s+$//; 113 | my @list = split(/ /, $line, 5); 114 | my $fname = shift(@list); 115 | if ($basename) { $fname = basename($fname); } 116 | if ($trim) { 117 | for (my $i = 0; $i <= $#list; $i++) { 118 | $list[$i] =~ s/^0*//; 119 | } 120 | } 121 | my $coord = ' ' . join(' ', @list); 122 | if (exists($counts{$fname})) { 123 | $counts{$fname}++; 124 | $coords{$fname} .= $coord; 125 | } else { 126 | $counts{$fname} = 1; 127 | $coords{$fname} = $coord; 128 | } 129 | } 130 | my @newlines = (); 131 | foreach my $fname (sort(keys(%counts))) { 132 | push(@newlines, $fname . ' ' . $counts{$fname} . $coords{$fname} . "\n"); 133 | } 134 | return @newlines; 135 | } 136 | 137 | sub main { 138 | ## arguments 139 | my ($infile, $lsformat, $trim, $basename) = &parse_args(@_); 140 | ## read 141 | my @lines; 142 | if ($infile ne "") { 143 | open(INPUT, $infile); @lines = ; close(INPUT); 144 | } else { 145 | @lines = ; 146 | } 147 | ## body 148 | if ($lsformat) { 149 | @lines = &ls2typical(@lines, $trim); 150 | } 151 | @lines = &typical2haartraining(@lines, $trim, $basename); 152 | 153 | ## output 154 | print @lines; 155 | } 156 | 157 | &main(@ARGV); 158 | -------------------------------------------------------------------------------- /src/opencvx/cvgausspdf.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_GAUSSPDF_INCLUDED 25 | #define CV_GAUSSPDF_INCLUDED 26 | 27 | 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | /** 34 | // cvMatGaussPdf - compute multivariate gaussian pdf for a set of sample vectors 35 | // 36 | // Example) 37 | // double vs[] = { 3, 4, 5, 38 | // 3, 4, 5 }; // col vectors 39 | // double m[] = { 5, 40 | // 5 }; 41 | // double a[] = { 1, 0, 42 | // 0, 1 }; 43 | // CvMat vecs = cvMat(2, 3, CV_64FC1, vs); 44 | // CvMat mean = cvMat(2, 1, CV_64FC1, m); 45 | // CvMat cov = cvMat(2, 2, CV_64FC1, a); 46 | // CvMat *probs = cvCreateMat(1, 3, CV_64FC1); 47 | // cvMatGaussPdf( &vecs, &mean, &cov, probs, false, false); 48 | // cvMatPrint( probs ); // 0.018316 0.367879 1.000000 49 | // cvMatGaussPdf( &vecs, &mean, &cov, probs, true, false); 50 | // cvMatPrint( probs ); // 0.002915 0.058550 0.159155 51 | // cvMatGaussPdf( &vecs, &mean, &cov, probs, false, true); 52 | // cvMatPrint( probs ); // -4.000000 -1.000000 -0.000000 53 | // cvMatGaussPdf( &vecs, &mean, &cov, probs, true, true); 54 | // cvMatPrint( probs ); // -5.837877 -2.837877 -1.837877 55 | // cvReleaseMat( &probs ); 56 | // 57 | // @param samples D x N data vectors where D is the number of 58 | // dimensions and N is the number of data 59 | // (Note: not N x D for clearness of matrix operation) 60 | // @param mean D x 1 mean vector 61 | // @param cov D x D covariance matrix 62 | // @param probs 1 x N computed probabilites 63 | // @param [normalize = false] Compute normalization term or not 64 | // @param [logprob = false] Log probability or not 65 | // @return void 66 | // @see cvCalcCovarMatrix, cvAvg 67 | */ 68 | void cvMatGaussPdf( const CvMat* samples, const CvMat* mean, const CvMat* cov, CvMat* probs, bool normalize = false, bool logprob = false ) 69 | { 70 | int D = samples->rows; 71 | int N = samples->cols; 72 | int type = samples->type; 73 | CV_FUNCNAME( "cvMatGaussPdf" ); // error handling 74 | __CV_BEGIN__; 75 | CV_ASSERT( CV_IS_MAT(samples) ); 76 | CV_ASSERT( CV_IS_MAT(mean) ); 77 | CV_ASSERT( CV_IS_MAT(cov) ); 78 | CV_ASSERT( CV_IS_MAT(probs) ); 79 | CV_ASSERT( D == mean->rows && 1 == mean->cols ); 80 | CV_ASSERT( D == cov->rows && D == cov->cols ); 81 | CV_ASSERT( 1 == probs->rows && N == probs->cols ); 82 | 83 | CvMat *invcov = cvCreateMat( D, D, type ); 84 | cvInvert( cov, invcov, CV_SVD ); 85 | 86 | CvMat *sample = cvCreateMat( D, 1, type ); 87 | CvMat *subsample = cvCreateMat( D, 1, type ); 88 | CvMat *subsample_T = cvCreateMat( 1, D, type ); 89 | CvMat *value = cvCreateMat( 1, 1, type ); 90 | double prob; 91 | for( int n = 0; n < N; n++ ) 92 | { 93 | cvGetCol( samples, sample, n ); 94 | 95 | cvSub( sample, mean, subsample ); 96 | cvTranspose( subsample, subsample_T ); 97 | cvMatMul( subsample_T, invcov, subsample_T ); 98 | cvMatMul( subsample_T, subsample, value ); 99 | prob = -0.5 * cvmGet(value, 0, 0); 100 | if( !logprob ) prob = exp( prob ); 101 | 102 | cvmSet( probs, 0, n, prob ); 103 | } 104 | if( normalize ) 105 | { 106 | double norm = pow( 2* M_PI, D/2.0 ) * sqrt( cvDet( cov ) ); 107 | if( logprob ) cvSubS( probs, cvScalar( log( norm ) ), probs ); 108 | else cvConvertScale( probs, probs, 1.0 / norm ); 109 | } 110 | 111 | cvReleaseMat( &invcov ); 112 | cvReleaseMat( &sample ); 113 | cvReleaseMat( &subsample ); 114 | cvReleaseMat( &subsample_T ); 115 | cvReleaseMat( &value ); 116 | 117 | __CV_END__; 118 | } 119 | 120 | /** 121 | // cvGaussPdf - compute multivariate gaussian pdf 122 | // 123 | // Example) 124 | // double v[] = { 3, 125 | // 3 }; 126 | // double m[] = { 5, 127 | // 5 }; 128 | // double a[] = { 1, 0, 129 | // 0, 1 }; 130 | // CvMat vec = cvMat(2, 1, CV_64FC1, v); 131 | // CvMat mean = cvMat(2, 1, CV_64FC1, m); 132 | // CvMat cov = cvMat(2, 2, CV_64FC1, a); 133 | // std::cout << cvGaussPdf( &vec, &mean, &cov, false ) << std::endl; 134 | // 135 | // @param sample D x 1 data vector 136 | // @param mean D x 1 mean vector 137 | // @param cov D x D covariance matrix 138 | // @param [normalize = false] Compute normalization term or not 139 | // @param [logprob = false] Log probability or not 140 | // @return double probability 141 | // @see cvCalcCovarMatrix, cvAvg 142 | // @uses cvMatGaussPdf 143 | */ 144 | double cvGaussPdf( const CvMat* sample, const CvMat* mean, const CvMat* cov, bool normalize = false, bool logprob = false ) 145 | { 146 | double prob; 147 | CvMat *_probs = cvCreateMat( 1, 1, sample->type ); 148 | 149 | cvMatGaussPdf( sample, mean, cov, _probs, normalize, logprob ); 150 | prob = cvmGet(_probs, 0, 0); 151 | 152 | cvReleaseMat( &_probs ); 153 | return prob; 154 | } 155 | 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /src/opencvx/mexx.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Matlab C Library (MEX) eXtension 3 | * verified Matlab 2007b 4 | * 5 | * The MIT License 6 | * 7 | * Copyright (c) 2008, Naotoshi Seo 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #ifndef MXX_INCLUDED 28 | #define MXX_INCLUDED 29 | 30 | 31 | #include "mat.h" 32 | #include "matrix.h" 33 | 34 | /** 35 | * definitions 36 | */ 37 | void mxPrintMatrix(const mxArray* mxarr); 38 | void mxSetCol(mxArray* X, mxArray* X1, int col); 39 | void mxSetRow(mxArray* X, mxArray* X1, int row); 40 | mxArray* mxGetCol(mxArray* X, int col); 41 | mxArray* mxGetRow(mxArray* X, int row); 42 | 43 | 44 | /** 45 | * Get a pointer to (row,col) 46 | * 47 | * @param mxArray *X 2-D Array 48 | * @param type object type such as double, uint 49 | * @param int row 50 | * @param int col 51 | */ 52 | #define MX_ARRAY_ELEM(X, type, row, col) (((type*)mxGetPr(X))[mxGetM(X)*col+row]) 53 | 54 | /** 55 | * Print mxArray 56 | * 57 | * Currently support only uint8, float, double 58 | * Currently support only upto 3-D. 59 | * 60 | * @param mxArray* mxarr 61 | * @return void 62 | */ 63 | void mxPrintMatrix(const mxArray* mxarr) 64 | { 65 | int nDim; 66 | const mwSize *dims; 67 | int row, col, ch, nChannel = 1; 68 | mxClassID classid; 69 | classid = mxGetClassID(mxarr); 70 | nDim = mxGetNumberOfDimensions(mxarr); 71 | dims = mxGetDimensions(mxarr); 72 | if (nDim >= 3) nChannel = dims[2]; 73 | 74 | if (classid == mxUINT8_CLASS) { 75 | unsigned char *mxData = (unsigned char*)mxGetData(mxarr); 76 | for (ch = 0; ch < nChannel; ch++) { 77 | for (row = 0; row < dims[0]; row++) { 78 | for (col = 0; col < dims[1]; col++) { 79 | printf("%d ", mxData[ 80 | dims[0] * dims[1] * ch + dims[0] * col + row]); 81 | } 82 | printf("\n"); 83 | } 84 | printf("\n"); 85 | } 86 | } else if (classid == mxDOUBLE_CLASS) { 87 | double *mxData = (double*)mxGetData(mxarr); 88 | for (ch = 0; ch < nChannel; ch++) { 89 | for (row = 0; row < dims[0]; row++) { 90 | for (col = 0; col < dims[1]; col++) { 91 | printf("%lf ", mxData[ 92 | dims[0] * dims[1] * ch + dims[0] * col + row]); 93 | } 94 | printf("\n"); 95 | } 96 | printf("\n"); 97 | } 98 | } else if (classid == mxSINGLE_CLASS) { 99 | float *mxData = (float*)mxGetData(mxarr); 100 | for (ch = 0; ch < nChannel; ch++) { 101 | for (row = 0; row < dims[0]; row++) { 102 | for (col = 0; col < dims[1]; col++) { 103 | printf("%lf ", mxData[ 104 | dims[0] * dims[1] * ch + dims[0] * col + row]); 105 | } 106 | printf("\n"); 107 | } 108 | printf("\n"); 109 | } 110 | } 111 | } 112 | 113 | /** 114 | * Set a column 115 | * 116 | * @param mxArray *X 2-D Array 117 | * @param int col 118 | * @return mxArray* 119 | */ 120 | void mxSetCol(mxArray* X, mxArray* X1, int col) 121 | { 122 | int nRow = mxGetM(X); 123 | int nCol = mxGetN(X); 124 | int row = 0; 125 | double *Xdata, *X1data; 126 | Xdata = (double*)mxGetPr(X); 127 | X1data = (double*)mxGetPr(X1); 128 | for (row = 0; row < nRow; row++) { 129 | Xdata[nRow * col + row] = X1data[row]; 130 | } 131 | } 132 | 133 | /** 134 | * Set a row 135 | * 136 | * @param mxArray *X 2-D Array 137 | * @param int col 138 | * @return mxArray* 139 | */ 140 | void mxSetRow(mxArray* X, mxArray* X1, int row) 141 | { 142 | int nRow = mxGetM(X); 143 | int nCol = mxGetN(X); 144 | int col = 0; 145 | double *Xdata, *X1data; 146 | Xdata = (double*)mxGetPr(X); 147 | X1data = (double*)mxGetPr(X1); 148 | for (col = 0; col < nCol; col++) { 149 | Xdata[nRow * col + row] = X1data[col]; 150 | } 151 | } 152 | 153 | /** 154 | * Get a column 155 | * 156 | * @param mxArray *X 2-D Array 157 | * @param int col 158 | * @return mxArray* 159 | */ 160 | mxArray* mxGetCol(mxArray* X, int col) 161 | { 162 | int nRow = mxGetM(X); 163 | int nCol = mxGetN(X); 164 | int row = 0; 165 | mxArray *X1; 166 | double *Xdata, *X1data; 167 | if(col > nCol) return NULL; 168 | X1 = mxCreateDoubleMatrix(nRow, 1, mxREAL); 169 | Xdata = (double*)mxGetPr(X); 170 | X1data = (double*)mxGetPr(X1); 171 | for (row = 0; row < nRow; row++) { 172 | X1data[row] = Xdata[nRow * col + row]; 173 | } 174 | return X1; 175 | } 176 | 177 | /** 178 | * Get a row 179 | * 180 | * @param mxArray *X 2-D Array 181 | * @param int row 182 | * @return mxArray* 183 | */ 184 | mxArray* mxGetRow(mxArray* X, int row) 185 | { 186 | int nRow = mxGetM(X); 187 | int nCol = mxGetN(X); 188 | int col = 0; 189 | mxArray *X1; 190 | double *Xdata, *X1data; 191 | if(row > nRow) return NULL; 192 | X1 = mxCreateDoubleMatrix(1, nCol, mxREAL); 193 | Xdata = (double*)mxGetPr(X); 194 | X1data = (double*)mxGetPr(X1); 195 | for (col = 0; col < nCol; col++) { 196 | X1data[col] = Xdata[nRow * col + row]; 197 | } 198 | return X1; 199 | } 200 | 201 | 202 | #endif -------------------------------------------------------------------------------- /src/opencvx/cvcropimageroi.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CROPIMAGEROI_INCLUDED 25 | #define CV_CROPIMAGEROI_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | #include "cvcreateaffine.h" 34 | #include "cvrect32f.h" 35 | 36 | CVAPI(void) cvCropImageROI(IplImage* img, IplImage* dst, 37 | CvRect32f rect32f = cvRect32f(0, 0, 1, 1, 0), 38 | CvPoint2D32f shear = cvPoint2D32f(0, 0)); 39 | CVAPI(void) cvShowCroppedImage(const char* w_name, IplImage* orig, 40 | CvRect32f rect32f = cvRect32f(0, 0, 1, 1, 0), 41 | CvPoint2D32f shear = cvPoint2D32f(0, 0), 42 | float cap_scale_factor = 1.0f); 43 | 44 | /** 45 | * Crop image with rotated and sheared rectangle 46 | * 47 | * IplImage* dst = cvCreateImage( cvSize( rect.width, rect.height ), img->depth, img->nChannels ); 48 | * Use CvBox32f to define rotation center as the center of rectangle, 49 | * and use cvRect32fBox32( box32f ) to pass argument. 50 | * 51 | * @param img The target image 52 | * @param dst The cropped image 53 | * @param [rect32f = cvRect32f(0,0,1,1,0)] 54 | * The rectangle region (x,y,width,height) to crop and 55 | * the rotation angle in degree where the rotation center is (x,y) 56 | * @param [shear = cvPoint2D32f(0,0)] 57 | * The shear deformation parameter shx and shy 58 | * @return void 59 | */ 60 | CVAPI(void) cvCropImageROI(IplImage* img, IplImage* dst, CvRect32f rect32f, CvPoint2D32f shear) 61 | { 62 | CvRect rect = cvRectFromRect32f(rect32f); 63 | float angle = rect32f.angle; 64 | CV_FUNCNAME("cvCropImageROI"); 65 | __CV_BEGIN__; 66 | CV_ASSERT(rect.width > 0 && rect.height > 0); 67 | CV_ASSERT(dst->width == rect.width); 68 | CV_ASSERT(dst->height == rect.height); 69 | 70 | if (angle == 0 && shear.x == 0 && shear.y == 0 && 71 | rect.x >= 0 && rect.y >= 0 && 72 | rect.x + rect.width < img->width && rect.y + rect.height < img->height) 73 | { 74 | cvSetImageROI(img, rect); 75 | cvCopy(img, dst); 76 | cvResetImageROI(img); 77 | } 78 | else if (shear.x == 0 && shear.y == 0) 79 | { 80 | int x, y, ch, xp, yp; 81 | double c = cos(-M_PI / 180 * angle); 82 | double s = sin(-M_PI / 180 * angle); 83 | /*CvMat* R = cvCreateMat( 2, 3, CV_32FC1 ); 84 | cv2DRotationMatrix( cvPoint2D32f( 0, 0 ), angle, 1.0, R ); 85 | double c = cvmGet( R, 0, 0 ); 86 | double s = cvmGet( R, 1, 0 ); 87 | cvReleaseMat( &R );*/ 88 | cvZero(dst); 89 | 90 | for (x = 0; x < rect.width; x++) 91 | { 92 | for (y = 0; y < rect.height; y++) 93 | { 94 | xp = (int)(c * x + -s * y + 0.5) + rect.x; 95 | yp = (int)(s * x + c * y + 0.5) + rect.y; 96 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 97 | for (ch = 0; ch < img->nChannels; ch++) 98 | { 99 | dst->imageData[dst->widthStep * y + x * dst->nChannels + ch] 100 | = img->imageData[img->widthStep * yp + xp * img->nChannels + ch]; 101 | } 102 | } 103 | } 104 | } 105 | else 106 | { 107 | int x, y, ch, xp, yp; 108 | CvMat* affine = cvCreateMat(2, 3, CV_32FC1); 109 | CvMat* xy = cvCreateMat(3, 1, CV_32FC1); 110 | CvMat* xyp = cvCreateMat(2, 1, CV_32FC1); 111 | cvCreateAffine(affine, rect32f, shear); 112 | cvmSet(xy, 2, 0, 1.0); 113 | cvZero(dst); 114 | 115 | for (x = 0; x < rect.width; x++) 116 | { 117 | cvmSet(xy, 0, 0, x / rect32f.width); 118 | for (y = 0; y < rect.height; y++) 119 | { 120 | cvmSet(xy, 1, 0, y / rect32f.height); 121 | cvMatMul(affine, xy, xyp); 122 | xp = (int)(cvmGet(xyp, 0, 0) + 0.5); 123 | yp = (int)(cvmGet(xyp, 1, 0) + 0.5); 124 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 125 | for (ch = 0; ch < img->nChannels; ch++) 126 | { 127 | dst->imageData[dst->widthStep * y + x * dst->nChannels + ch] 128 | = img->imageData[img->widthStep * yp + xp * img->nChannels + ch]; 129 | } 130 | } 131 | } 132 | cvReleaseMat(&affine); 133 | cvReleaseMat(&xy); 134 | cvReleaseMat(&xyp); 135 | } 136 | __CV_END__; 137 | } 138 | 139 | /** 140 | * Crop and show the Cropped Image 141 | * 142 | * @param w_name Window name 143 | * @param img Image to be cropped 144 | * @param [rect32f = cvRect32f(0,0,1,1,0)] 145 | * The rectangle region (x,y,width,height) to crop and 146 | * the rotation angle in degree 147 | * @param [shear = cvPoint2D32f(0,0)] 148 | * The shear deformation parameter shx and shy 149 | * @return void 150 | * @uses cvCropImageROI 151 | */ 152 | CVAPI(void) cvShowCroppedImage(const char* w_name, IplImage* img, CvRect32f rect32f, CvPoint2D32f shear, float cap_scale_factor) 153 | { 154 | CvRect rect = cvRectFromRect32f(rect32f); 155 | if (rect.width <= 0 || rect.height <= 0) return; 156 | IplImage* crop = cvCreateImage(cvSize(rect.width, rect.height), img->depth, img->nChannels); 157 | cvCropImageROI(img, crop, rect32f, shear); 158 | 159 | if (cap_scale_factor != 1.0f) 160 | { 161 | IplImage* crop_resize = cvCreateImage(cvSize(rect.width * cap_scale_factor, rect.height * cap_scale_factor), img->depth, img->nChannels); 162 | cvResize(crop, crop_resize); 163 | cvShowImage(w_name, crop); 164 | cvReleaseImage(&crop_resize); 165 | } 166 | else 167 | cvShowImage(w_name, crop); 168 | 169 | cvReleaseImage(&crop); 170 | } 171 | 172 | 173 | #endif 174 | -------------------------------------------------------------------------------- /src/Actions.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __ACTIONS_HPP__ 2 | #define __ACTIONS_HPP__ 3 | 4 | #include 5 | #include "Global.h" 6 | 7 | 8 | void act_draw(CvCallbackParam* param, MouseCallbackStatus* status, int x, int y, int flags) 9 | { 10 | // Is pressing Ctrl? 11 | if (flags & CV_EVENT_FLAG_CTRLKEY) // Draw a square 12 | { 13 | param->rect.width = param->rect.height = max(abs(status->hit_point.x - x), abs(status->hit_point.y - y)); 14 | param->rect.x = (x > status->hit_point.x) ? status->hit_point.x : status->hit_point.x - param->rect.width; 15 | param->rect.y = (y > status->hit_point.y) ? status->hit_point.y : status->hit_point.y - param->rect.height; 16 | } 17 | else // Draw a rectangle 18 | { 19 | param->rect.x = min(status->hit_point.x, x); 20 | param->rect.y = min(status->hit_point.y, y); 21 | param->rect.width = abs(status->hit_point.x - x); 22 | param->rect.height = abs(status->hit_point.y - y); 23 | } 24 | 25 | // Is pressing Alt? 26 | if (flags & CV_EVENT_FLAG_ALTKEY) 27 | { 28 | if (x > status->hit_point.x) 29 | param->rect.x -= param->rect.width; 30 | 31 | if (y > status->hit_point.y) 32 | param->rect.y -= param->rect.height; 33 | 34 | param->rect.width *= 2; 35 | param->rect.height *= 2; 36 | } 37 | 38 | // clean 39 | param->circle.width = 0; // disable watershed 40 | param->rotate = 0; 41 | param->shear.x = param->shear.y = 0; 42 | 43 | // show 44 | cvShowImageAndRectangle(param->w_name, param->img_display, cvRect32fFromRect(param->rect, param->rotate), cvPointTo32f(param->shear)); 45 | cvShowCroppedImage(param->miniw_name, param->img_src, cvRect32fFromRect(cvScaleRect(param->rect, 1 / param->scale_factor), param->rotate), cvPointTo32f(param->shear), param->cap_scale_factor); 46 | } 47 | 48 | 49 | void inline act_watershed_begin(CvCallbackParam* param, MouseCallbackStatus* status, int x, int y, int flags) 50 | { 51 | param->circle.x = x; 52 | param->circle.y = y; 53 | } 54 | 55 | void act_watershed_process(CvCallbackParam* param, MouseCallbackStatus* status, int x, int y, int flags) 56 | { 57 | param->rotate = 0; 58 | param->shear.x = param->shear.y = 0; 59 | 60 | param->circle.width = (int)cvPointNorm(cvPoint(param->circle.x, param->circle.y), cvPoint(x, y)); 61 | param->rect = cvShowImageAndWatershed(param->w_name, param->img_display, param->circle); 62 | cvShowCroppedImage(param->miniw_name, param->img_src, cvRect32fFromRect(cvScaleRect(param->rect, 1 / param->scale_factor), param->rotate), cvPointTo32f(param->shear), param->cap_scale_factor); 63 | } 64 | 65 | 66 | void act_move_begin(CvCallbackParam* param, MouseCallbackStatus* status, int x, int y, int flags) 67 | { 68 | status->hit_point = cvPoint(x, y); 69 | 70 | // move watershed 71 | if (param->circle.width != 0) 72 | { 73 | CvPoint center = cvPoint(param->circle.x, param->circle.y); 74 | int radius = (int)cvPointNorm(center, status->hit_point); 75 | if (param->circle.width - 1 <= radius && radius <= param->circle.width) 76 | status->resize_watershed = true; 77 | else if (radius <= param->circle.width) 78 | status->move_watershed = true; 79 | } 80 | 81 | // move reatangle 82 | else if ((param->rect.x < x && x < param->rect.x + param->rect.width) && (param->rect.y < y && y < param->rect.y + param->rect.height)) 83 | status->move_rect = true; 84 | 85 | else 86 | { 87 | if (x <= param->rect.x) 88 | status->resize_rect_left = true; 89 | else if (x >= param->rect.x + param->rect.width) 90 | status->resize_rect_right = true; 91 | 92 | if (y <= param->rect.y) 93 | status->resize_rect_top = true; 94 | else if (y >= param->rect.y + param->rect.height) 95 | status->resize_rect_bottom = true; 96 | } 97 | } 98 | 99 | void act_move_process(CvCallbackParam* param, MouseCallbackStatus* status, int x, int y, int flags) 100 | { 101 | if (status->move_watershed) 102 | { 103 | CvPoint move = cvPoint(x - status->hit_point.x, y - status->hit_point.y); 104 | param->circle.x += move.x; 105 | param->circle.y += move.y; 106 | 107 | param->rect = cvShowImageAndWatershed(param->w_name, param->img_display, param->circle); 108 | cvShowCroppedImage(param->miniw_name, param->img_src, cvRect32fFromRect(cvScaleRect(param->rect, 1 / param->scale_factor), param->rotate), cvPointTo32f(param->shear), param->cap_scale_factor); 109 | 110 | status->hit_point = cvPoint(x, y); 111 | } 112 | else if (status->resize_watershed) 113 | { 114 | param->circle.width = (int)cvPointNorm(cvPoint(param->circle.x, param->circle.y), cvPoint(x, y)); 115 | param->rect = cvShowImageAndWatershed(param->w_name, param->img_display, param->circle); 116 | cvShowCroppedImage(param->miniw_name, param->img_src, cvRect32fFromRect(cvScaleRect(param->rect, 1 / param->scale_factor), param->rotate), cvPointTo32f(param->shear), param->cap_scale_factor); 117 | } 118 | else 119 | { 120 | if (status->move_rect) 121 | { 122 | CvPoint move = cvPoint(x - status->hit_point.x, y - status->hit_point.y); 123 | param->rect.x += move.x; 124 | param->rect.y += move.y; 125 | } 126 | if (status->resize_rect_left) 127 | { 128 | int move_x = x - status->hit_point.x; 129 | param->rect.x += move_x; 130 | param->rect.width -= move_x; 131 | } 132 | else if (status->resize_rect_right) 133 | { 134 | int move_x = x - status->hit_point.x; 135 | param->rect.width += move_x; 136 | } 137 | if (status->resize_rect_top) 138 | { 139 | int move_y = y - status->hit_point.y; 140 | param->rect.y += move_y; 141 | param->rect.height -= move_y; 142 | } 143 | else if (status->resize_rect_bottom) 144 | { 145 | int move_y = y - status->hit_point.y; 146 | param->rect.height += move_y; 147 | } 148 | 149 | // assure width is positive 150 | if (param->rect.width <= 0) 151 | { 152 | param->rect.x += param->rect.width; 153 | param->rect.width *= -1; 154 | bool tmp = status->resize_rect_right; 155 | status->resize_rect_right = status->resize_rect_left; 156 | status->resize_rect_left = tmp; 157 | } 158 | // assure height is positive 159 | if (param->rect.height <= 0) 160 | { 161 | param->rect.y += param->rect.height; 162 | param->rect.height *= -1; 163 | bool tmp = status->resize_rect_top; 164 | status->resize_rect_top = status->resize_rect_bottom; 165 | status->resize_rect_bottom = tmp; 166 | } 167 | 168 | cvShowImageAndRectangle(param->w_name, param->img_display, cvRect32fFromRect(param->rect, param->rotate), cvPointTo32f(param->shear)); 169 | cvShowCroppedImage(param->miniw_name, param->img_src, cvRect32fFromRect(cvScaleRect(param->rect, 1 / param->scale_factor), param->rotate), cvPointTo32f(param->shear), param->cap_scale_factor); 170 | status->hit_point = cvPoint(x, y); 171 | } 172 | } 173 | 174 | #endif // __ACTIONS_HPP__ 175 | -------------------------------------------------------------------------------- /src/opencvx/cvrect32f.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | #ifndef CV_RECT32F_INCLUDED 26 | #define CV_RECT32F_INCLUDED 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | /******************* Structure Definitions ***************************/ 33 | 34 | typedef struct CvRect32f { 35 | float x; /* left x coord of rectangle */ 36 | float y; /* top y coord of rectangle */ 37 | float width; /* width of rectangle */ 38 | float height; /* height of rectangle */ 39 | float angle; /* counter-clockwise rotation angle in degree */ 40 | /* rotation center is (x, y) coordinates */ 41 | } CvRect32f; 42 | 43 | /* This is quivalent with CvBox2D, but I wanted this structure because 44 | CvBox2D's parameters are too long such as box.center.x, box.size.width and 45 | CvBox2D does not have a constructor cvBox2D(...). */ 46 | typedef struct CvBox32f { 47 | float cx; /* center x coord of rectangle */ 48 | float cy; /* center y coord of center of rectangle */ 49 | float width; /* width of rectangle */ 50 | float height; /* height of rectangle */ 51 | float angle; /* counter-clockwise rotation angle in degree */ 52 | /* rotation center is center of rectangle */ 53 | } CvBox32f; 54 | 55 | /******************* Function Prototypes ********************************/ 56 | 57 | CV_INLINE CvRect32f cvRect32f(float x, float y, float width, float height, float angle = 0.0); 58 | CV_INLINE CvBox32f cvBox32f(float cx, float cy, float width, float height, float angle = 0.0); 59 | 60 | CV_INLINE CvRect32f cvRect32fFromRect(CvRect rect, float angle = 0); 61 | CV_INLINE CvRect cvRectFromRect32f(CvRect32f rect); 62 | CV_INLINE CvBox32f cvBox32fFromBox2D(CvBox2D box); 63 | CV_INLINE CvBox2D cvBox2DFromBox32f(CvBox32f box); 64 | 65 | CVAPI(CvBox32f) cvBox32fFromRect32f(CvRect32f rect); 66 | CVAPI(CvRect32f) cvRect32fFromBox32f(CvBox32f box); 67 | 68 | CV_INLINE CvRect cvScaleRect(CvRect rect, float scale_factor); 69 | 70 | #define cvBox32fFromRect(rect) (cvBox32fFromRect32f(cvRect32fFromRect(rect))) 71 | #define cvBox2DFromRect(rect) (cvBox2DFromBox32f(cvBox32fFromRect(rect))) 72 | #define cvRectFromBox32f(box) (cvRectFromRect32f(cvRect32fFromBox32f(box))) 73 | #define cvRectFromBox2D(box) (cvRectFromBox32f(cvBox32fFromBox2D(box))) 74 | #define cvBox2DFromRect32f(rect) (cvBox2DFromBox32f(cvBox32fFromRect32f(rect))) 75 | #define cvRect32fFromBox2D(box) (cvRect32fFromBox32f(cvBox32fFromBox2D(box))) 76 | 77 | #define cvPrintRect32f(rect) \ 78 | printf( "x=%f y=%f width=%f height=%f angle=%f\n", \ 79 | rect.x, rect.y, rect.width, rect.height, rect.angle ); \ 80 | fflush( stdout ); 81 | 82 | #define cvPrintBox32f(box) \ 83 | printf( "cx=%f cy=%f width=%f height=%f angle=%f\n", \ 84 | box.cx, box.cy, box.width, box.height, box.angle ); \ 85 | fflush( stdout ); 86 | 87 | #ifndef cvPrintBox2D 88 | #define cvPrintBox2D(box) \ 89 | printf( "cx=%f cy=%f width=%f height=%f angle=%f\n", \ 90 | box.center.x, box.center.y, box.size.width, \ 91 | box.size.height, box.angle ); \ 92 | fflush( stdout ); 93 | #endif 94 | 95 | #ifndef cvPrintRect 96 | #define cvPrintRect(rect) \ 97 | printf( "x=%d y=%d width=%d height=%d", \ 98 | rect.x, rect.y, rect.width, rect.height ); \ 99 | fflush( stdout ); 100 | #endif 101 | 102 | 103 | /******************* Function Implementations ***************************/ 104 | 105 | CV_INLINE CvRect32f cvRect32f(float x, float y, float width, float height, float angle) 106 | { 107 | CvRect32f rect = { x, y, width, height, angle }; 108 | return rect; 109 | } 110 | 111 | CV_INLINE CvBox32f cvBox32f(float cx, float cy, float width, float height, float angle) 112 | { 113 | CvBox32f box = { cx, cy, width, height, angle }; 114 | return box; 115 | } 116 | 117 | CV_INLINE CvRect32f cvRect32fFromRect(CvRect rect, float angle) 118 | { 119 | return cvRect32f(rect.x, rect.y, rect.width, rect.height, angle); 120 | } 121 | 122 | CV_INLINE CvRect cvRectFromRect32f(CvRect32f rect) 123 | { 124 | return cvRect(cvRound(rect.x), cvRound(rect.y), 125 | cvRound(rect.width), cvRound(rect.height)); 126 | } 127 | 128 | CV_INLINE CvBox32f cvBox32fFromBox2D(CvBox2D box) 129 | { 130 | return cvBox32f(box.center.x, box.center.y, 131 | box.size.width, box.size.height, 132 | box.angle); 133 | } 134 | 135 | CV_INLINE CvBox2D cvBox2DFromBox32f(CvBox32f box) 136 | { 137 | CvBox2D box2d; 138 | box2d.center.x = box.cx; 139 | box2d.center.y = box.cy; 140 | box2d.size.width = box.width; 141 | box2d.size.height = box.height; 142 | box2d.angle = box.angle; 143 | return box2d; 144 | } 145 | 146 | CVAPI(CvBox32f) cvBox32fFromRect32f(CvRect32f rect) 147 | { 148 | float cx, cy; 149 | // x + ( x + width - 1 ) / 2 = cx 150 | cx = (2 * rect.x + rect.width - 1) / 2.0; 151 | cy = (2 * rect.y + rect.height - 1) / 2.0; 152 | if (rect.angle != 0) 153 | { 154 | CvMat* R = cvCreateMat(2, 3, CV_32FC1); 155 | cv2DRotationMatrix(cvPoint2D32f(rect.x, rect.y), rect.angle, 1.0, R); 156 | cx = cvmGet(R, 0, 0) * cx + cvmGet(R, 0, 1) * cy + cvmGet(R, 0, 2); 157 | cy = cvmGet(R, 1, 0) * cx + cvmGet(R, 1, 1) * cy + cvmGet(R, 1, 2); 158 | cvReleaseMat(&R); 159 | } 160 | return cvBox32f(cx, cy, rect.width, rect.height, rect.angle); 161 | } 162 | 163 | CVAPI(CvRect32f) cvRect32fFromBox32f(CvBox32f box) 164 | { 165 | float x, y; 166 | // x + ( x + width - 1 ) / 2 = cx 167 | x = (2 * box.cx + 1 - box.width) / 2.0; 168 | y = (2 * box.cy + 1 - box.height) / 2.0; 169 | if (box.angle != 0.0) 170 | { 171 | CvMat* R = cvCreateMat(2, 3, CV_32FC1); 172 | cv2DRotationMatrix(cvPoint2D32f(box.cx, box.cy), box.angle, 1.0, R); 173 | x = cvmGet(R, 0, 0) * x + cvmGet(R, 0, 1) * y + cvmGet(R, 0, 2); 174 | y = cvmGet(R, 1, 0) * x + cvmGet(R, 1, 1) * y + cvmGet(R, 1, 2); 175 | cvReleaseMat(&R); 176 | } 177 | return cvRect32f(x, y, box.width, box.height, box.angle); 178 | } 179 | 180 | CV_INLINE CvRect cvScaleRect(CvRect rect, float scale_factor) 181 | { 182 | return cvRect(rect.x * scale_factor, rect.y * scale_factor, 183 | rect.width * scale_factor, 184 | rect.height * scale_factor); 185 | } 186 | 187 | 188 | 189 | #endif 190 | -------------------------------------------------------------------------------- /src/opencvx/cvputimageroi.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CROPIMAGEROI_INCLUDED 25 | #define CV_CROPIMAGEROI_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | #include "cvrect32f.h" 34 | #include "cvcreateaffine.h" 35 | #include "cvcreateaffineimage.h" 36 | 37 | CVAPI(void) cvPutImageROI( const IplImage* src, 38 | IplImage* dst, 39 | CvRect32f rect32f = cvRect32f(0,0,1,1,0), 40 | CvPoint2D32f shear = cvPoint2D32f(0,0), 41 | const IplImage* mask = NULL, 42 | bool circumscribe = 0 ); 43 | 44 | /** 45 | * Put a source image on the specified region on a target image 46 | * 47 | * Use CvBox32f to define rotation center as the center of rectangle, 48 | * and use cvRect32fBox32( box32f ) to pass argument. 49 | * 50 | * @param src The source image 51 | * @param dst The target image 52 | * @param [rect32f = cvRect32f(0,0,1,1,0)] 53 | * The rectangle region (x,y,width,height) to put 54 | * the rotation angle in degree where the rotation center is (x,y) 55 | * @param [shear = cvPoint2D32f(0,0)] 56 | * The shear deformation parameter shx and shy 57 | * @param [mask = NULL] The mask image 58 | * @param [circumscribe = 0] 59 | * Put a circular (ellipsoidal) image as a circumscribed 60 | * circle (ellipsoid) rather than a inscribed circle (ellipsoid) 61 | * @return void 62 | */ 63 | CVAPI(void) cvPutImageROI( const IplImage* src, 64 | IplImage* dst, 65 | CvRect32f rect32f, 66 | CvPoint2D32f shear, 67 | const IplImage* mask, 68 | bool circumscribe ) 69 | { 70 | CvRect rect; 71 | float tx, ty, sx, sy, angle; 72 | IplImage* _src = NULL; 73 | IplImage* _mask = NULL; 74 | CV_FUNCNAME( "cvPutImageROI" ); 75 | __CV_BEGIN__; 76 | rect = cvRectFromRect32f( rect32f ); 77 | angle = rect32f.angle; 78 | 79 | CV_ASSERT( rect.width > 0 && rect.height > 0 ); 80 | CV_ASSERT( src->depth == dst->depth ); 81 | CV_ASSERT( src->nChannels == dst->nChannels ); 82 | if( mask != NULL ) 83 | CV_ASSERT( src->width == mask->width && src->height == mask->height ); 84 | 85 | if( circumscribe ) 86 | { 87 | CvBox32f box32f = cvBox32fFromRect32f( rect32f ); 88 | // width and height from center (inscribed ellipsoid's a and b parameters) 89 | float a = box32f.width / 2.0; 90 | float b = box32f.height / 2.0; 91 | // diagonal distance to the corner of rectangle from center 92 | float d = sqrt( a * a + b * b ); 93 | // get distance to the intersectional point of inscribed ellipsoid 94 | // line with rectangle's diagonal line from center 95 | float cost = a / d; 96 | float sint = b / d; 97 | float c = sqrt( pow( a * cost, 2 ) + pow( b * sint, 2 ) ); 98 | // this ratio enables to make circumscribed ellipsoid 99 | float ratio = d / c; 100 | box32f.width *= ratio; 101 | box32f.height *= ratio; 102 | // ellipsoidal paramter to rectangle parameter 103 | rect32f = cvRect32fFromBox32f( box32f ); 104 | rect = cvRectFromRect32f( rect32f ); 105 | } 106 | 107 | _src = (IplImage*)src; 108 | _mask = (IplImage*)mask; 109 | if( rect.width != src->width && rect.height != src->height ) 110 | { 111 | _src = cvCreateImage( cvSize( rect.width, rect.height ), src->depth, src->nChannels ); 112 | cvResize( src, _src ); 113 | if( mask != NULL ) 114 | { 115 | _mask = cvCreateImage( cvSize( rect.width, rect.height ), mask->depth, mask->nChannels ); 116 | cvResize( mask, _mask ); 117 | } 118 | } 119 | 120 | if( angle == 0 && shear.x == 0 && shear.y == 0 && 121 | rect.x >= 0 && rect.y >= 0 && 122 | rect.x + rect.width < dst->width && rect.y + rect.height < dst->height ) 123 | { 124 | cvSetImageROI( dst, rect ); 125 | cvCopy( _src, dst, _mask ); 126 | cvResetImageROI( dst ); 127 | } 128 | else 129 | { 130 | if( _mask == NULL ) 131 | { 132 | _mask = cvCreateImage( cvGetSize(_src), IPL_DEPTH_8U, 1 ); 133 | cvSet( _mask, cvScalar(1) ); 134 | } 135 | 136 | CvMat* affine = cvCreateMat( 2, 3, CV_32FC1 ); 137 | tx = 0; 138 | ty = 0; 139 | sx = rect32f.width / (float)_src->width; 140 | sy = rect32f.height / (float)_src->height; 141 | angle = rect32f.angle; 142 | cvCreateAffine( affine, cvRect32f( tx, ty, sx, sy, angle ), shear ); 143 | 144 | CvPoint origin; 145 | IplImage* srctrans = cvCreateAffineImage( _src, affine, CV_AFFINE_FULL, &origin, CV_RGB(0,0,0) ); 146 | IplImage* masktrans = cvCreateAffineImage( _mask, affine, CV_AFFINE_FULL, NULL, cvScalar(0) ); 147 | for( int xp = 0; xp < srctrans->width; xp++ ) 148 | { 149 | int x = xp + rect.x + origin.x; 150 | for( int yp = 0; yp < srctrans->height; yp++ ) 151 | { 152 | int y = yp + rect.y + origin.y; 153 | if( x < 0 || x >= dst->width || y < 0 || y >= dst->height ) continue; 154 | if( CV_IMAGE_ELEM( masktrans, uchar, yp, xp ) == 0 ) continue; 155 | for( int ch = 0; ch < srctrans->nChannels; ch++ ) 156 | { 157 | dst->imageData[dst->widthStep * y + x * dst->nChannels + ch] 158 | = srctrans->imageData[srctrans->widthStep * yp + xp * srctrans->nChannels + ch]; 159 | } 160 | } 161 | } 162 | cvReleaseMat( &affine ); 163 | cvReleaseImage( &srctrans ); 164 | cvReleaseImage( &masktrans ); 165 | } 166 | if( mask != _mask ) 167 | cvReleaseImage( &_mask ); 168 | if( src != _src ) 169 | cvReleaseImage( &_src ); 170 | __CV_END__; 171 | } 172 | 173 | #endif 174 | -------------------------------------------------------------------------------- /src/opencvx/cvcreateaffineimage.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_CREATEAFFINEIMAGE_INCLUDED 25 | #define CV_CREATEAFFINEIMAGE_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | #include 33 | 34 | #include "cvinvaffine.h" 35 | 36 | #define CV_AFFINE_SAME 0 37 | #define CV_AFFINE_FULL 1 38 | CVAPI(IplImage*) cvCreateAffineImage( const IplImage* src, const CvMat* affine, 39 | int flags = CV_AFFINE_SAME, CvPoint* origin = NULL, 40 | CvScalar color = CV_RGB(0,0,0) ); 41 | CV_INLINE IplImage* cvCreateAffineMask( const IplImage* src, const CvMat* affine, 42 | int flags = CV_AFFINE_SAME, CvPoint* origin = NULL ); 43 | 44 | /** 45 | * Create a mask image for cvCreateAffineImage 46 | * 47 | * @param src Image. Used to get image size. 48 | * @param affine 2 x 3 Affine transform matrix 49 | * @param flags CV_AFFINE_SAME - Outside image coordinates are cut off 50 | * CV_AFFINE_FULL - Fully contain the original image pixel values 51 | * @param origin The coordinate of origin (the coordinate in original image respective to 52 | * the transformed image origin). 53 | * Useful when CV_AFFINE_FULL is used. 54 | */ 55 | CV_INLINE IplImage* cvCreateAffineMask( const IplImage* src, const CvMat* affine, 56 | int flags, CvPoint* origin ) 57 | { 58 | IplImage* orig = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 ); 59 | cvSet( orig, cvScalar(1) ); 60 | IplImage* mask = cvCreateAffineImage( orig, affine, flags, origin, cvScalar(0) ); 61 | cvReleaseImage( &orig ); 62 | return mask; 63 | } 64 | 65 | /** 66 | * Affine transform of an image 67 | * 68 | * Do not forget cvReleaseImage( &ret ); 69 | * 70 | * @param src Image 71 | * @param affine 2 x 3 Affine transform matrix 72 | * @param flags CV_AFFINE_SAME - Outside image coordinates are cut off 73 | * CV_AFFINE_FULL - Fully contain the original image pixel values 74 | * @param origin The coordinate of origin (the coordinate in original image respective to 75 | * the transformed image origin). 76 | * Useful when CV_AFFINE_FULL is used. 77 | * @return IplImage* 78 | * @see cvWarpAffine - this does not support CV_AFFINE_FULL, but supports 79 | * several interpolation methods and so on. 80 | */ 81 | CVAPI(IplImage*) cvCreateAffineImage( const IplImage* src, const CvMat* affine, 82 | int flags, CvPoint* origin, 83 | CvScalar color ) 84 | { 85 | IplImage* dst; 86 | int minx = INT_MAX; 87 | int miny = INT_MAX; 88 | int maxx = INT_MIN; 89 | int maxy = INT_MIN; 90 | int i, x, y, xx, yy, xp, yp; 91 | int ch, width, height; 92 | CvPoint pt[4]; 93 | CvMat* invaffine; 94 | CV_FUNCNAME( "cvAffineImage" ); 95 | __CV_BEGIN__; 96 | CV_ASSERT( src->depth == IPL_DEPTH_8U ); 97 | CV_ASSERT( affine->rows == 2 && affine->cols == 3 ); 98 | 99 | // cvBoxPoints supports only rotation (no shear deform) 100 | // original 4 corner 101 | pt[0].x = 0; pt[0].y = 0; 102 | pt[1].x = src->width - 1; pt[1].y = 0; 103 | pt[2].x = 0; pt[2].y = src->height - 1; 104 | pt[3].x = src->width - 1; pt[3].y = src->height - 1; 105 | // 4 corner after transformed 106 | for( i = 0; i < 4; i++ ) 107 | { 108 | x = cvRound( pt[i].x * cvmGet( affine, 0, 0 ) + 109 | pt[i].y * cvmGet( affine, 0, 1 ) + 110 | cvmGet( affine, 0, 2 ) ); 111 | y = cvRound( pt[i].x * cvmGet( affine, 1, 0 ) + 112 | pt[i].y * cvmGet( affine, 1, 1 ) + 113 | cvmGet( affine, 1, 2 ) ); 114 | pt[i].x = x; pt[i].y = y; 115 | } 116 | // min, max 117 | for( i = 0; i < 4; i++ ) 118 | { 119 | minx = MIN( pt[i].x, minx ); 120 | miny = MIN( pt[i].y, miny ); 121 | maxx = MAX( pt[i].x, maxx ); 122 | maxy = MAX( pt[i].y, maxy ); 123 | } 124 | // target image width and height 125 | if( flags == CV_AFFINE_FULL ) 126 | { 127 | width = maxx - minx + 1; 128 | height = maxy - miny + 1; 129 | } 130 | else if( flags == CV_AFFINE_SAME ) 131 | { 132 | width = src->width; 133 | height = src->height; 134 | minx = miny = 0; 135 | maxx = src->width - 1; 136 | maxy = src->height - 1; 137 | } 138 | //cvPrintMat( affine ); 139 | //printf( "%d %d %d %d\n", minx, miny, maxx, maxy ); 140 | if( origin != NULL ) 141 | { 142 | origin->x = minx; 143 | origin->y = miny; 144 | } 145 | dst = cvCreateImage( cvSize(width, height), src->depth, src->nChannels ); 146 | cvSet( dst, color ); 147 | 148 | // inverse affine 149 | invaffine = cvCreateMat( 2, 3, affine->type ); 150 | cvInvAffine( affine, invaffine ); 151 | 152 | // loop based on image coordinates of transformed image 153 | for( x = 0; x < width; x++ ) 154 | { 155 | xx = x + minx; 156 | for( y = 0; y < height; y++ ) 157 | { 158 | yy = y + miny; 159 | xp = cvRound( xx * cvmGet( invaffine, 0, 0 ) + 160 | yy * cvmGet( invaffine, 0, 1 ) + 161 | cvmGet( invaffine, 0, 2 ) ); 162 | yp = cvRound( xx * cvmGet( invaffine, 1, 0 ) + 163 | yy * cvmGet( invaffine, 1, 1 ) + 164 | cvmGet( invaffine, 1, 2 ) ); 165 | if( xp < 0 || xp >= src->width || yp < 0 || yp >= src->height ) continue; 166 | for( ch = 0; ch < src->nChannels; ch++ ) 167 | { 168 | dst->imageData[dst->widthStep * y + x * dst->nChannels + ch] 169 | = src->imageData[src->widthStep * yp + xp * src->nChannels + ch]; 170 | } 171 | } 172 | } 173 | cvReleaseMat( &invaffine ); 174 | __CV_END__; 175 | return dst; 176 | } 177 | 178 | 179 | #endif 180 | -------------------------------------------------------------------------------- /src/opencvx/cvmxmatconv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * OpenCV versus Matlab C Library (MEX) interfaces 3 | * verified under OpenCV 1.00 and Matlab 2007b 4 | * 5 | * Matrix conversion 6 | * 7 | * The MIT License 8 | * 9 | * Copyright (c) 2008, Naotoshi Seo 10 | * 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy 12 | * of this software and associated documentation files (the "Software"), to deal 13 | * in the Software without restriction, including without limitation the rights 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | * copies of the Software, and to permit persons to whom the Software is 16 | * furnished to do so, subject to the following conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be included in 19 | * all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 | * THE SOFTWARE. 28 | */ 29 | #ifndef CVMX_MATCONV_INCLUDED 30 | #define CVMX_MATCONV_INCLUDED 31 | 32 | 33 | #include 34 | #include "mat.h" 35 | #include "matrix.h" 36 | #include "cvmxtypeconv.h" 37 | 38 | /************** Definitions *******************************/ 39 | CVAPI(mxArray*) cvmxArrayFromCvArr(const CvArr* arr); 40 | #define cvmxArrayFromIplImage(img) (cvmxArrayFromCvArr(img)) 41 | #define cvmxArrayFromCvMat(mat) (cvmxArrayFromCvArr(mat)) 42 | CVAPI(IplImage*) cvmxArrayToIplImage(const mxArray* mxarr); 43 | CVAPI(CvMat*) cvmxArrayToCvMat(const mxArray* mxarr); 44 | 45 | /************** Functions *********************************/ 46 | /** 47 | * Convert CvArr to mxArray. Allocate memory too 48 | * 49 | * Currently support only uint8, float, double 50 | * Currently support only 1 channel (grayscale) and 3 channels (rgb etc) 51 | * 52 | * @param CvArr* arr 53 | * @return mxArray* 54 | */ 55 | CVAPI(mxArray*) cvmxArrayFromCvArr(const CvArr* arr) 56 | { 57 | CV_FUNCNAME( "cvmxArraFromCvArr" ); 58 | IplImage imghdr, *img = (IplImage*)arr; 59 | int nChannel, nRow, nCol, nDim; 60 | mwSize dims[3]; 61 | mxClassID classid; mxArray* mxarr = NULL; 62 | int row, col, ch; 63 | 64 | __CV_BEGIN__; 65 | if (!CV_IS_IMAGE(img)) { 66 | CV_CALL(img = cvGetImage(img, &imghdr)); 67 | } 68 | nChannel = img->nChannels; 69 | nRow = img->height; 70 | nCol = img->width; 71 | nDim = 2; 72 | classid = cvmxClassIDFromIplDepth(img->depth); 73 | dims[0] = nRow; dims[1] = nCol; dims[2] = nChannel; 74 | if (nChannel > 1) nDim = 3; 75 | mxarr = mxCreateNumericArray(nDim, dims, classid, mxREAL); 76 | 77 | if (classid == mxUINT8_CLASS) { 78 | unsigned char *mxData = (unsigned char*)mxGetData(mxarr); 79 | for (ch = 0; ch < nChannel; ch++) { 80 | for (row = 0; row < dims[0]; row++) { 81 | for (col = 0; col < dims[1]; col++) { 82 | mxData[dims[0] * dims[1] * ch + dims[0] * col + row] 83 | = CV_IMAGE_ELEM(img, unsigned char, row, nChannel * col + ch); 84 | } 85 | } 86 | } 87 | } else if (classid == mxDOUBLE_CLASS) { 88 | double *mxData = (double*)mxGetData(mxarr); 89 | for (ch = 0; ch < nChannel; ch++) { 90 | for (row = 0; row < dims[0]; row++) { 91 | for (col = 0; col < dims[1]; col++) { 92 | mxData[dims[0] * dims[1] * ch + dims[0] * col + row] 93 | = CV_IMAGE_ELEM(img, double, row, nChannel * col + ch); 94 | } 95 | } 96 | } 97 | } else if (classid == mxSINGLE_CLASS) { 98 | float *mxData = (float*)mxGetData(mxarr); 99 | for (ch = 0; ch < nChannel; ch++) { 100 | for (row = 0; row < dims[0]; row++) { 101 | for (col = 0; col < dims[1]; col++) { 102 | mxData[dims[0] * dims[1] * ch + dims[0] * col + row] 103 | = CV_IMAGE_ELEM(img, float, row, nChannel * col + ch); 104 | } 105 | } 106 | } 107 | } 108 | __CV_END__; 109 | return mxarr; 110 | } 111 | 112 | /** 113 | * Convert mxArray to IplImage. Allocate memory too 114 | * 115 | * Currently support only uint8, float, double 116 | * Currently support only 1 channel (grayscale) and 3 channels (rgb etc) 117 | * 118 | * @param mxArray* mxarr 119 | * @return IplImage* 120 | */ 121 | CVAPI(IplImage*) cvmxArrayToIplImage(const mxArray* mxarr) 122 | { 123 | CV_FUNCNAME( "cvmxArrayToCvArr" ); 124 | IplImage *img = NULL; 125 | int depth; 126 | mwSize nChannel = 1; 127 | mwSize nDim; 128 | const mwSize *dims; 129 | mxClassID classid; 130 | int row, col, ch; 131 | 132 | __CV_BEGIN__; 133 | classid = mxGetClassID(mxarr); 134 | nDim = mxGetNumberOfDimensions(mxarr); 135 | dims = mxGetDimensions(mxarr); 136 | if (nDim >= 3) nChannel = dims[2]; 137 | depth = cvmxClassIDToIplDepth(mxGetClassID(mxarr)); 138 | img = cvCreateImage(cvSize(dims[1], dims[0]), depth, nChannel); 139 | 140 | if (classid == mxUINT8_CLASS) { 141 | unsigned char *mxData = (unsigned char*)mxGetData(mxarr); 142 | for (ch = 0; ch < nChannel; ch++) { 143 | for (row = 0; row < dims[0]; row++) { 144 | for (col = 0; col < dims[1]; col++) { 145 | CV_IMAGE_ELEM(img, unsigned char, row, nChannel * col + ch) 146 | = mxData[dims[0] * dims[1] * ch + dims[0] * col + row]; 147 | } 148 | } 149 | } 150 | } else if (classid == mxDOUBLE_CLASS) { 151 | double *mxData = (double*)mxGetData(mxarr); 152 | for (ch = 0; ch < nChannel; ch++) { 153 | for (row = 0; row < dims[0]; row++) { 154 | for (col = 0; col < dims[1]; col++) { 155 | CV_IMAGE_ELEM(img, double, row, nChannel * col + ch) 156 | = mxData[dims[0] * dims[1] * ch + dims[0] * col + row]; 157 | } 158 | } 159 | } 160 | } else if (classid == mxSINGLE_CLASS) { 161 | float *mxData = (float*)mxGetData(mxarr); 162 | for (ch = 0; ch < nChannel; ch++) { 163 | for (row = 0; row < dims[0]; row++) { 164 | for (col = 0; col < dims[1]; col++) { 165 | CV_IMAGE_ELEM(img, float, row, nChannel * col + ch) 166 | = mxData[dims[0] * dims[1] * ch + dims[0] * col + row]; 167 | } 168 | } 169 | } 170 | } 171 | __CV_END__; 172 | return img; 173 | } 174 | 175 | /** 176 | * Convert mxArray to CvMat. Allocate memory too 177 | * 178 | * Currently support only uint8, float, double 179 | * Currently support only 1 channel (grayscale) and 3 channels (rgb etc) 180 | * 181 | * @param mxArray* mxarr 182 | * @return CvMat* 183 | */ 184 | CVAPI(CvMat*) cvmxArrayToCvMat(const mxArray* mxarr) 185 | { 186 | IplImage* img = cvmxArrayToIplImage(mxarr); 187 | CvMat *mat = cvCreateMat(img->height, img->width, CV_MAKETYPE(cvmxIplToCvDepth(img->depth), img->nChannels)); 188 | cvConvert(img, mat); 189 | cvReleaseImage(&img); 190 | // @todo: why cvGetMat results in error? 191 | //int coi = 0; 192 | //IplImage *img = cvmxArrayToIplImage(mxarr); 193 | //CvMat *mat, mathdr; 194 | //mat = cvGetMat(mat, &mathdr, &coi, 0); 195 | return mat; 196 | } 197 | 198 | 199 | 200 | #endif 201 | -------------------------------------------------------------------------------- /src/filesystem.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * 3 | * Filesystem functions 4 | * Currently, this is a boost::filesystem wrapper. 5 | * If you do not like to use (or install) boost::filesystem, modify here without 6 | * changing function interfaces so that the main program is not necessary modified. 7 | * 8 | * The MIT License 9 | * 10 | * Copyright (c) 2008, Naotoshi Seo 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy 13 | * of this software and associated documentation files (the "Software"), to deal 14 | * in the Software without restriction, including without limitation the rights 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | * copies of the Software, and to permit persons to whom the Software is 17 | * furnished to do so, subject to the following conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be included in 20 | * all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | * THE SOFTWARE. 29 | */ 30 | #ifndef FILESYSTEM_INCLUDED 31 | #define FILESYSTEM_INCLUDED 32 | 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | namespace fs { 38 | 39 | inline void create_directories( const string& path ) 40 | { 41 | boost::filesystem::path fspath( path ); 42 | boost::filesystem::create_directories( fspath ); 43 | } 44 | 45 | inline bool is_directory( const string& path ) 46 | { 47 | boost::filesystem::path fspath( path ); 48 | return boost::filesystem::is_directory( fspath ); 49 | } 50 | 51 | inline bool exists( const string& path ) 52 | { 53 | boost::filesystem::path fspath( path ); 54 | return boost::filesystem::exists( fspath ); 55 | } 56 | 57 | inline string realpath( const string& path ) 58 | { 59 | boost::filesystem::path fspath( path ); 60 | return fspath.string(); 61 | } 62 | 63 | inline string dirname( const string& path ) 64 | { 65 | boost::filesystem::path fspath( path ); 66 | return fspath.branch_path().string(); 67 | } 68 | 69 | inline string basename( const string& path ) 70 | { 71 | boost::filesystem::path fspath( path ); 72 | return fspath.filename().string(); 73 | } 74 | 75 | inline string filename( const string& path ) 76 | { 77 | boost::filesystem::path fspath( path ); 78 | return boost::filesystem::basename( fspath ); 79 | } 80 | 81 | // @todo: string( ext, 1 ) errors when ext does not exist. 82 | inline string extension( const string& path ) 83 | { 84 | boost::filesystem::path fspath( path ); 85 | return string( boost::filesystem::extension( fspath ), 1 ); 86 | } 87 | 88 | inline string strtolower( const std::string& str ) 89 | { 90 | std::string ret = str; 91 | for( std::string::size_type i = 0; i < ret.size(); i++ ) 92 | { 93 | ret[i] = tolower(ret[i]); 94 | } 95 | return ret; 96 | } 97 | 98 | bool match_extensions( const string& filename, const vector& extensions ) 99 | { 100 | std::string extension = boost::filesystem::extension( filename ); 101 | extension = fs::strtolower( extension ); 102 | for( std::size_t i = 0; i < extensions.size() - 1; i++ ) { 103 | if( extension == "." + extensions[i] ) return true; 104 | } 105 | return false; 106 | } 107 | 108 | vector filelist( const string& dirpath, const vector& extensions, string file_type = "all" ) 109 | { 110 | vector filelist; 111 | boost::filesystem::path fs_dirpath( dirpath ); 112 | 113 | bool list_directory = ( file_type == "dir" ); 114 | bool list_regular_file = ( file_type == "file" ); 115 | bool list_symlink = ( file_type == "symlink" ); 116 | bool list_other = ( !list_directory && !list_regular_file && !list_symlink ); 117 | bool list_all = ( file_type == "all" ); 118 | 119 | if( !boost::filesystem::exists(fs_dirpath) || !boost::filesystem::is_directory(fs_dirpath) ) 120 | return filelist; 121 | 122 | boost::filesystem::directory_iterator iter( fs_dirpath ), end_iter; 123 | for( ; iter != end_iter; ++iter ) { 124 | boost::filesystem::path filename = iter->path(); 125 | if( match_extensions( filename.string(), extensions ) ) { 126 | if(list_all) { 127 | filelist.push_back( filename.string() ); 128 | } else if(list_regular_file && boost::filesystem::is_regular( filename )) { 129 | filelist.push_back( filename.string() ); 130 | } else if(list_directory && boost::filesystem::is_directory( filename )) { 131 | filelist.push_back( filename.string() ); 132 | } else if(list_symlink && boost::filesystem::is_symlink( filename )) { 133 | filelist.push_back( filename.string() ); 134 | } else if(list_other && boost::filesystem::is_other( filename )) { 135 | filelist.push_back( filename.string() ); 136 | } 137 | } 138 | } 139 | return filelist; 140 | } 141 | 142 | /* 143 | vector 144 | filesystem(const boost::filesystem::path& dirpath, 145 | const boost::regex regex = boost::regex(".*"), 146 | boost::filesystem::file_type file_type = boost::filesystem::type_unknown) 147 | { 148 | vector filelist; 149 | bool list_directory = (file_type == boost::filesystem::directory_file); 150 | bool list_regular_file = (file_type == boost::filesystem::regular_file); 151 | bool list_symlink = (file_type == boost::filesystem::symlink_file); 152 | bool list_other = (!list_directory && !list_regular_file && !list_symlink); 153 | bool list_all = (file_type == boost::filesystem::type_unknown); // just for now 154 | 155 | if(!boost::filesystem::exists(dirpath) || !boost::filesystem::is_directory(dirpath)) { 156 | return filelist; 157 | } 158 | 159 | boost::filesystem::directory_iterator iter(dirpath), end_iter; 160 | for(; iter != end_iter; ++iter) { 161 | boost::filesystem::path filename = iter->path(); 162 | if(boost::regex_match(filename.string(), regex)) { 163 | if(list_all) { 164 | filelist.push_back(filename); 165 | } else if(list_regular_file && boost::filesystem::is_regular(filename)) { 166 | filelist.push_back(filename); 167 | } else if(list_directory && boost::filesystem::is_directory(filename)) { 168 | filelist.push_back(filename); 169 | } else if(list_symlink && boost::filesystem::is_symlink(filename)) { 170 | filelist.push_back(filename); 171 | } else if(list_other && boost::filesystem::is_other(filename)) { 172 | filelist.push_back(filename); 173 | } 174 | } 175 | } 176 | return filelist; 177 | } 178 | */ 179 | } 180 | 181 | #endif 182 | -------------------------------------------------------------------------------- /src/opencvx/cvskincolorgmm.h: -------------------------------------------------------------------------------- 1 | /** 2 | // cvskincolorgmm.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | #ifndef CV_SKINCOLOR_GMM_INCLUDED 15 | #define CV_SKINCOLOR_GMM_INCLUDED 16 | 17 | 18 | #include 19 | #include 20 | #define _USE_MATH_DEFINES 21 | #include 22 | 23 | #include "cvxmat.h" 24 | #include "cvgmmpdf.h" 25 | 26 | void cvSkinColorGmm( const IplImage* _img, IplImage* mask, double threshold = 1.0, IplImage* probs = NULL ); 27 | 28 | /** 29 | // cvSkinColorGMM - Skin Color Detection with GMM model 30 | // 31 | // @param img Input image 32 | // @param mask Generated mask image. 1 for skin and 0 for others 33 | // @param threshold Threshold value for likelihood-ratio test 34 | // The preferrable threshold = (number of other pixels / number of skin pixels) 35 | // You may guess this value by looking your image. 36 | // You may reduce this number when you can allow larger false alaram rate which 37 | // results in to reduce reduce miss detection rate. 38 | // @param [probs = NULL] The likelihood-ratio valued array rather than mask if you want 39 | // 40 | // References) 41 | // @article{606260, 42 | // author = {Michael J. Jones and James M. Rehg}, 43 | // title = {Statistical color models with application to skin detection}, 44 | // journal = {Int. J. Comput. Vision}, 45 | // volume = {46}, 46 | // number = {1}, 47 | // year = {2002}, 48 | // issn = {0920-5691}, 49 | // pages = {81--96}, 50 | // doi = {http://dx.doi.org/10.1023/A:1013200319198}, 51 | // publisher = {Kluwer Academic Publishers}, 52 | // address = {Hingham, MA, USA}, 53 | // } 54 | */ 55 | void cvSkinColorGmm( const IplImage* _img, IplImage* mask, double threshold, IplImage* probs ) 56 | { 57 | CV_FUNCNAME( "cvSkinColorGmm" ); 58 | __CV_BEGIN__; 59 | const int N = _img->height * _img->width; 60 | const int D = 3; 61 | const int K = 16; 62 | IplImage* img; 63 | 64 | double skin_mean[] = { 65 | 73.5300, 249.7100, 161.6800, 186.0700, 189.2600, 247.0000, 150.1000, 206.8500, 212.7800, 234.8700, 151.1900, 120.5200, 192.2000, 214.2900, 99.5700, 238.8800, 66 | 29.9400, 233.9400, 116.2500, 136.6200, 98.3700, 152.2000, 72.6600, 171.0900, 152.8200, 175.4300, 97.7400, 77.5500, 119.6200, 136.0800, 54.3300, 203.0800, 67 | 17.7600, 217.4900, 96.9500, 114.4000, 51.1800, 90.8400, 37.7600, 156.3400, 120.0400, 138.9400, 74.5900, 59.8200, 82.3200, 87.2400, 38.0600, 176.9100 68 | }; 69 | double skin_cov[] = { // only diagonal components 70 | 765.4000, 39.9400, 291.0300, 274.9500, 633.1800, 65.2300, 408.6300, 530.0800, 160.5700, 163.8000, 425.4000, 330.4500, 152.7600, 204.9000, 448.1300, 178.3800, 71 | 121.4400, 154.4400, 60.4800, 64.6000, 222.4000, 691.5300, 200.7700, 155.0800, 84.5200, 121.5700, 73.5600, 70.3400, 92.1400, 140.1700, 90.1800, 156.2700, 72 | 112.8000, 396.0500, 162.8500, 198.2700, 250.6900, 609.9200, 257.5700, 572.7900, 243.9000, 279.2200, 175.1100, 151.8200, 259.1500, 270.1900, 151.2900, 404.9900 73 | }; 74 | double skin_weight[] = { 75 | 0.0294, 0.0331, 0.0654, 0.0756, 0.0554, 0.0314, 0.0454, 0.0469, 0.0956, 0.0763, 0.1100, 0.0676, 0.0755, 0.0500, 0.0667, 0.0749 76 | }; 77 | double nonskin_mean[] = { 78 | 254.3700, 9.3900, 96.5700, 160.4400, 74.9800, 121.8300, 202.1800, 193.0600, 51.8800, 30.8800, 44.9700, 236.0200, 207.8600, 99.8300, 135.0600, 135.9600, 79 | 254.4100, 8.0900, 96.9500, 162.4900, 63.2300, 60.8800, 154.8800, 201.9300, 57.1400, 26.8400, 85.9600, 236.2700, 191.2000, 148.1100, 131.9200, 103.8900, 80 | 253.8200, 8.5200, 91.5300, 159.0600, 46.3300, 18.3100, 91.0400, 206.5500, 61.5500, 25.3200, 131.9500, 230.7000, 164.1200, 188.1700, 123.1000, 66.8800 81 | }; 82 | double nonskin_cov[] = { // only diagonal components 83 | 2.77, 46.84, 280.69, 355.98, 414.84, 2502.2, 957.42, 562.88, 344.11, 222.07, 651.32, 225.03, 494.04, 955.88, 350.35, 806.44, 84 | 2.81, 33.59, 156.79, 115.89, 245.95, 1383.5, 1766.9, 190.23, 191.77, 118.65, 840.52, 117.29, 237.69, 654.95, 130.3, 642.2, 85 | 5.46, 32.48, 436.58, 591.24, 361.27, 237.18, 1582.5, 447.28, 433.4, 182.41, 963.67, 331.95, 533.52, 916.7, 388.43, 350.36 86 | }; 87 | double nonskin_weight[] = { 88 | 0.0637, 0.0516, 0.0864, 0.0636, 0.0747, 0.0365, 0.0349, 0.0649, 0.0656, 0.1189, 0.0362, 0.0849, 0.0368, 0.0389, 0.0943, 0.0477 89 | }; 90 | 91 | CV_ASSERT( _img->width == mask->width && _img->height == mask->height ); 92 | CV_ASSERT( _img->nChannels >= 3 && mask->nChannels == 1 ); 93 | if( probs ) 94 | { 95 | CV_ASSERT( _img->width == probs->width && _img->height == probs->height ); 96 | CV_ASSERT( probs->nChannels == 1 ); 97 | } 98 | 99 | img = cvCreateImage( cvGetSize(_img), _img->depth, _img->nChannels ); 100 | cvCvtColor( _img, img, CV_BGR2RGB ); 101 | 102 | // transform to CvMat 103 | CvMat SkinMeans = cvMat( D, K, CV_64FC1, skin_mean ); 104 | CvMat SkinWeights = cvMat( 1, K, CV_64FC1, skin_weight ); 105 | CvMat **SkinCovs = (CvMat**)cvAlloc( K * sizeof( CvMat* ) ); 106 | for( int k = 0; k < K; k++ ) 107 | { 108 | SkinCovs[k] = cvCreateMat( D, D, CV_64FC1 ); 109 | cvZero( SkinCovs[k] ); 110 | for( int i = 0; i < D; i++ ) 111 | { 112 | cvmSet( SkinCovs[k], i, i, skin_cov[K * i + k] ); 113 | } 114 | } 115 | 116 | // transform to CvMat 117 | CvMat NonSkinMeans = cvMat( D, K, CV_64FC1, nonskin_mean ); 118 | CvMat NonSkinWeights = cvMat( 1, K, CV_64FC1, nonskin_weight ); 119 | CvMat **NonSkinCovs = (CvMat**)cvAlloc( K * sizeof( CvMat* ) ); 120 | for( int k = 0; k < K; k++ ) 121 | { 122 | NonSkinCovs[k] = cvCreateMat( D, D, CV_64FC1 ); 123 | cvZero( NonSkinCovs[k] ); 124 | for( int i = 0; i < D; i++ ) 125 | { 126 | cvmSet( NonSkinCovs[k], i, i, nonskin_cov[K * i + k] ); 127 | } 128 | } 129 | 130 | // reshape IplImage to D (colors) x N matrix of CV_64FC1 131 | CvMat *Mat = cvCreateMat( D, N, CV_64FC1 ); 132 | CvMat *PreMat, hdr; 133 | PreMat = cvCreateMat( img->height, img->width, CV_64FC3 ); 134 | cvConvert( img, PreMat ); 135 | cvTranspose( cvReshape( PreMat, &hdr, 1, N ), Mat ); 136 | cvReleaseMat( &PreMat ); 137 | 138 | // GMM PDF 139 | CvMat *SkinProbs = cvCreateMat(1, N, CV_64FC1); 140 | CvMat *NonSkinProbs = cvCreateMat(1, N, CV_64FC1); 141 | 142 | cvMatGmmPdf( Mat, &SkinMeans, SkinCovs, &SkinWeights, SkinProbs, true); 143 | cvMatGmmPdf( Mat, &NonSkinMeans, NonSkinCovs, &NonSkinWeights, NonSkinProbs, true); 144 | 145 | // Likelihood-ratio test 146 | CvMat *Mask = cvCreateMat( 1, N, CV_8UC1 ); 147 | cvDiv( SkinProbs, NonSkinProbs, SkinProbs ); 148 | cvThreshold( SkinProbs, Mask, threshold, 1, CV_THRESH_BINARY ); 149 | cvConvert( cvReshape( Mask, &hdr, 1, img->height ), mask ); 150 | 151 | if( probs ) cvConvert( cvReshape( SkinProbs, &hdr, 1, img->height ), probs ); 152 | 153 | for( int k = 0; k < K; k++ ) 154 | { 155 | cvReleaseMat( &SkinCovs[k] ); 156 | cvReleaseMat( &NonSkinCovs[k] ); 157 | } 158 | cvFree( &SkinCovs ); 159 | cvFree( &NonSkinCovs ); 160 | 161 | cvReleaseMat( &SkinProbs ); 162 | cvReleaseMat( &NonSkinProbs ); 163 | cvReleaseMat( &Mask ); 164 | cvReleaseImage( &img ); 165 | 166 | __CV_END__; 167 | } 168 | 169 | 170 | #endif 171 | -------------------------------------------------------------------------------- /src/opencvx/cvdrawrectangle.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * The MIT License 3 | * 4 | * Copyright (c) 2008, Naotoshi Seo 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | #ifndef CV_DRAWRECTANGLE_INCLUDED 25 | #define CV_DRAWRECTANGLE_INCLUDED 26 | 27 | #include 28 | #include 29 | #include 30 | #define _USE_MATH_DEFINES 31 | #include 32 | 33 | #include "cvcreateaffine.h" 34 | #include "cvrect32f.h" 35 | 36 | CVAPI(void) cvDrawRectangle(IplImage* img, 37 | CvRect32f rect32f = cvRect32f(0, 0, 1, 1, 0), 38 | CvPoint2D32f shear = cvPoint2D32f(0, 0), 39 | CvScalar color = CV_RGB(255, 255, 255), 40 | int thickness = 1, 41 | int line_type = 8, 42 | int shift = 0); 43 | CVAPI(void) cvShowImageAndRectangle(const char* w_name, const IplImage* img, 44 | CvRect32f rect32f = cvRect32f(0, 0, 1, 1, 0), 45 | CvPoint2D32f shear = cvPoint2D32f(0, 0), 46 | CvScalar color = CV_RGB(255, 255, 0), 47 | int thickness = 1, int line_type = 8, 48 | int shift = 0); 49 | 50 | /** 51 | * Draw an rotated and sheared rectangle 52 | * 53 | * Use CvBox32f to define rotation center as the center of rectangle, 54 | * and use cvRect32fBox32( box32f ) to pass argument. 55 | * 56 | * @param img The image to be drawn rectangle 57 | * @param [rect32f = cvRect32f(0,0,1,1,0)] 58 | * The rectangle (x,y,width,height) to be shown and 59 | * the rotation angle in degree where the rotation center is (x,y) 60 | * @param [shear = cvPoint2D32f(0,0)] 61 | * The shear deformation parameter shx and shy 62 | * @param [color = CV_RGB(255, 255, 0)] 63 | * Line color (RGB) or brightness (grayscale image). 64 | * @param [thickness = 1] Thickness of lines that make up the rectangle. 65 | * Negative values, e.g. CV_FILLED, make the function 66 | * to draw a filled rectangle. 67 | * @param [line_type = 8] Type of the line, see cvLine description. 68 | * @param [shift = 0] Number of fractional bits in the point coordinates. 69 | * @todo thickness, line_type, and shift are available only when rotate == 0 && shear == 0 currently. 70 | * @return void 71 | * @uses cvRectangle 72 | */ 73 | CVAPI(void) cvDrawRectangle(IplImage* img, 74 | CvRect32f rect32f, 75 | CvPoint2D32f shear, 76 | CvScalar color, 77 | int thickness, 78 | int line_type, 79 | int shift) 80 | { 81 | CvRect rect = cvRectFromRect32f(rect32f); 82 | float angle = rect32f.angle; 83 | CV_FUNCNAME("cvDrawRectangle"); 84 | __CV_BEGIN__; 85 | CV_ASSERT(rect.width > 0 && rect.height > 0); 86 | 87 | if (angle == 0 && shear.x == 0 && shear.y == 0) 88 | { 89 | CvPoint pt1 = cvPoint(rect.x, rect.y); 90 | CvPoint pt2 = cvPoint(rect.x + rect.width - 1, rect.y + rect.height - 1); 91 | cvRectangle(img, pt1, pt2, color, thickness, line_type, shift); 92 | } 93 | else if (shear.x == 0 && shear.y == 0) 94 | { 95 | int x, y, ch, xp, yp; 96 | double c = cos(-M_PI / 180 * angle); 97 | double s = sin(-M_PI / 180 * angle); 98 | /*CvMat* R = cvCreateMat( 2, 3, CV_32FC1 ); 99 | cv2DRotationMatrix( cvPoint2D32f( 0, 0 ), angle, 1.0, R ); 100 | double c = cvmGet( R, 0, 0 ); 101 | double s = cvmGet( R, 1, 0 ); 102 | cvReleaseMat( &R );*/ 103 | 104 | for (x = 0; x < rect.width; x++) 105 | { 106 | for (y = 0; y < rect.height; y += max(1, rect.height - 1)) 107 | { 108 | xp = (int)(c * x + -s * y + 0.5) + rect.x; 109 | yp = (int)(s * x + c * y + 0.5) + rect.y; 110 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 111 | for (ch = 0; ch < img->nChannels; ch++) 112 | { 113 | img->imageData[img->widthStep * yp + xp * img->nChannels + ch] = (char)color.val[ch]; 114 | } 115 | } 116 | } 117 | 118 | for (y = 0; y < rect.height; y++) 119 | { 120 | for (x = 0; x < rect.width; x += max(1, rect.width - 1)) 121 | { 122 | xp = (int)(c * x + -s * y + 0.5) + rect.x; 123 | yp = (int)(s * x + c * y + 0.5) + rect.y; 124 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 125 | for (ch = 0; ch < img->nChannels; ch++) 126 | { 127 | img->imageData[img->widthStep * yp + xp * img->nChannels + ch] = (char)color.val[ch]; 128 | } 129 | } 130 | } 131 | } 132 | else 133 | { 134 | int x, y, ch, xp, yp; 135 | CvMat* affine = cvCreateMat(2, 3, CV_32FC1); 136 | CvMat* xy = cvCreateMat(3, 1, CV_32FC1); 137 | CvMat* xyp = cvCreateMat(2, 1, CV_32FC1); 138 | cvmSet(xy, 2, 0, 1.0); 139 | cvCreateAffine(affine, rect32f, shear); 140 | 141 | for (x = 0; x < rect.width; x++) 142 | { 143 | cvmSet(xy, 0, 0, x / rect32f.width); 144 | for (y = 0; y < rect.height; y += max(1, rect.height - 1)) 145 | { 146 | cvmSet(xy, 1, 0, y / rect32f.height); 147 | cvMatMul(affine, xy, xyp); 148 | xp = (int)(cvmGet(xyp, 0, 0) + 0.5); 149 | yp = (int)(cvmGet(xyp, 1, 0) + 0.5); 150 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 151 | for (ch = 0; ch < img->nChannels; ch++) 152 | { 153 | img->imageData[img->widthStep * yp + xp * img->nChannels + ch] = (char)color.val[ch]; 154 | } 155 | } 156 | } 157 | for (y = 0; y < rect.height; y++) 158 | { 159 | cvmSet(xy, 1, 0, y / rect32f.height); 160 | for (x = 0; x < rect.width; x += max(1, rect.width - 1)) 161 | { 162 | cvmSet(xy, 0, 0, x / rect32f.width); 163 | cvMatMul(affine, xy, xyp); 164 | xp = (int)(cvmGet(xyp, 0, 0) + 0.5); 165 | yp = (int)(cvmGet(xyp, 1, 0) + 0.5); 166 | if (xp < 0 || xp >= img->width || yp < 0 || yp >= img->height) continue; 167 | for (ch = 0; ch < img->nChannels; ch++) 168 | { 169 | img->imageData[img->widthStep * yp + xp * img->nChannels + ch] = (char)color.val[ch]; 170 | } 171 | } 172 | } 173 | cvReleaseMat(&affine); 174 | cvReleaseMat(&xy); 175 | cvReleaseMat(&xyp); 176 | } 177 | __CV_END__; 178 | } 179 | 180 | /** 181 | * Show Image and Rectangle 182 | * 183 | * @param w_name Window name 184 | * @param img Image to be shown 185 | * @param [rect32f = cvRect32f(0,0,1,1,0)] 186 | * Rectangle to be shown and 187 | * Rotation degree of rectangle 188 | * @param [shear = cvPoint2D32f(0,0)] 189 | * The shear deformation parameter shx and shy 190 | * @param [color = CV_RGB(255, 255, 0)] 191 | * Line color (RGB) or brightness (grayscale image). 192 | * @param [thickness = 1] Thickness of lines that make up the rectangle. 193 | * Negative values, e.g. CV_FILLED, make the function 194 | * to draw a filled rectangle. 195 | * @param [line_type = 8] Type of the line, see cvLine description. 196 | * @param [shift = 0] Number of fractional bits in the point coordinates. 197 | * @todo thickness, line_type, and shift are available only when angle == 0 && shear == 0 currently. 198 | * @return void 199 | * @uses cvDrawRectangle 200 | */ 201 | CVAPI(void) cvShowImageAndRectangle(const char* w_name, 202 | const IplImage* img, 203 | CvRect32f rect32f, 204 | CvPoint2D32f shear, 205 | CvScalar color, int thickness, int line_type, int shift) 206 | { 207 | CvRect rect = cvRectFromRect32f(rect32f); 208 | if (rect.width <= 0 || rect.height <= 0) { cvShowImage(w_name, img); return; } 209 | IplImage* clone = cvCloneImage(img); 210 | cvDrawRectangle(clone, rect32f, shear, color, thickness, line_type, shift); 211 | cvShowImage(w_name, clone); 212 | cvReleaseImage(&clone); 213 | } 214 | 215 | 216 | #endif 217 | -------------------------------------------------------------------------------- /src/opencvx/cvpcadiffs.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | // cvxpca.h 3 | // 4 | // Copyright (c) 2008, Naotoshi Seo. All rights reserved. 5 | // 6 | // The program is free to use for non-commercial academic purposes, 7 | // but for course works, you must understand what is going inside to 8 | // use. The program can be used, modified, or re-distributed for any 9 | // purposes only if you or one of your group understand not only 10 | // programming codes but also theory and math behind (if any). 11 | // Please contact the authors if you are interested in using the 12 | // program without meeting the above conditions. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #define _USE_MATH_DEFINES 20 | #include 21 | 22 | #ifndef CV_PCADIFFS_INCLUDED 23 | #define CV_PCADIFFS_INCLUDED 24 | 25 | //void cvCalcPCA( const CvArr* data, CvArr* avg, 26 | // CvArr* eigenvalues, CvArr* eigenvectors, int flags ); 27 | //void cvProjectPCA( const CvArr* data, const CvArr* avg, 28 | // const CvArr* eigenvectors, CvArr* result ); 29 | //void cvBackProjectPCA( const CvArr* proj, const CvArr* avg, 30 | // const CvArr* eigenvects, CvArr* result ); 31 | void cvMatPcaDiffs( const CvMat* samples, const CvMat* avg, const CvMat* eigenvalues, 32 | const CvMat* eigenvectors, CvMat* probs, 33 | int normalize = 0, bool logprob = true ); 34 | double cvPcaDiffs( const CvMat* sample, const CvMat* avg, const CvMat* eigenvalues, 35 | const CvMat* eigenvectors, int normalize = 0, bool logprob = true ); 36 | 37 | /** 38 | * cvPcaDiffs - Distance "in" and "from" feature space [1] 39 | * 40 | * Synopsis 41 | * [p] = cvPcaDiffs(X, U, Me, Lambda, normalize, logprob, lambdapad) 42 | * 43 | * Description 44 | * This computes a distance between a point to a PCA subspace as sum of 45 | * distance-from-feature space (DFFS) and distance-in-feature-space 46 | * (DIFS). The DFFS is essentially a reconstruction error and the 47 | * DIFS measures (mahalanobis) distance between projected point and 48 | * origin of PCA subpsace. DFFS and DIFS can be used to measure 49 | * approximated likelihood Gaussian density distribution. 50 | * See [1] for more details. 51 | * 52 | * @param samples D x N sample vectors 53 | * @param avg D x 1 mean vector 54 | * @param eigenvalues nEig x 1 eigen values 55 | * @param eigenvectors M x D or D x M (automatically adjusted) eigen vectors 56 | * @param probs 1 x N computed likelihood probabilities 57 | * @param [normalize = 0] Compute normalization term or not 58 | * 0 - nothing 59 | * 1 - normalization term 60 | * 2 - normalize so that sum becomes 1.0 61 | * @param [logprob = false] Log probability or not 62 | * 63 | * References 64 | * [1] @INPROCEEDINGS{Moghaddam95probabilisticvisual, 65 | * author = {Baback Moghaddam and Alex Pentl}, 66 | * title = {Probabilistic visual learning for object detection}, 67 | * booktitle = {}, 68 | * year = {1995}, 69 | * pages = {786--793} 70 | * } 71 | * [2] @ARTICLE{Moghaddam02principalmanifolds, 72 | * author = {Baback Moghaddam}, 73 | * title = {Principal manifolds and probabilistic subspaces for visual recognition}, 74 | * journal = {IEEE Transactions on Pattern Analysis and Machine Intelligence}, 75 | * year = {2002}, 76 | * volume = {24}, 77 | * pages = {780--788} 78 | * 79 | * Authors 80 | * Naotoshi Seo 81 | * 82 | * License 83 | * The program is free to use for non-commercial academic purposes, 84 | * but for course works, you must understand what is going inside to use. 85 | * The program can be used, modified, or re-distributed for any purposes 86 | * if you or one of your group understand codes (the one must come to 87 | * court if court cases occur.) Please contact the authors if you are 88 | * interested in using the program without meeting the above conditions. 89 | * 90 | * Changes 91 | * 11/18/2008 First Edition 92 | */ 93 | void cvMatPcaDiffs( const CvMat* samples, const CvMat* avg, const CvMat* eigenvalues, 94 | const CvMat* eigenvectors, CvMat* probs, int normalize, bool logprob ) 95 | { 96 | int D = samples->rows; 97 | int N = samples->cols; 98 | int M = (eigenvectors->rows == D) ? eigenvectors->cols : eigenvectors->rows; 99 | int type = samples->type; 100 | int nEig = eigenvalues->rows; 101 | int d, n; 102 | double normterm = 0; 103 | CvMat *_eigenvectors; // cvProjectPCA requires M x D vec 104 | CvMat *proj = cvCreateMat( N, M, type ); 105 | CvMat *subproj, subprojhdr; 106 | CvMat *sclsubproj = cvCreateMat( 1, M, type ); 107 | CvMat *pLambda, pLambdaHdr; 108 | CvMat *rLambda, rLambdaHdr; 109 | CvMat *sqrt_pLambda = cvCreateMat( M, 1, type ); 110 | CvMat *DIFS = cvCreateMat( 1, N, type ); 111 | CvMat *DFFS = cvCreateMat( 1, N, type ); 112 | CvMat *samples0 = cvCreateMat( D, N, type ); // mean subtracted samples 113 | CvMat *subsamples0, subsamples0hdr; 114 | CvScalar rho; 115 | CV_FUNCNAME( "cvMatPcaDiffs" ); 116 | __CV_BEGIN__; 117 | CV_ASSERT( CV_IS_MAT(samples) ); 118 | CV_ASSERT( CV_IS_MAT(avg) ); 119 | CV_ASSERT( CV_IS_MAT(eigenvalues) ); 120 | CV_ASSERT( CV_IS_MAT(eigenvectors) ); 121 | CV_ASSERT( D == eigenvectors->rows || D == eigenvectors->cols ); 122 | CV_ASSERT( D == avg->rows && 1 == avg->cols ); 123 | CV_ASSERT( 1 == probs->rows && N == probs->cols ); 124 | 125 | cvZero( DIFS ); 126 | cvZero( DFFS ); 127 | 128 | if( D == eigenvectors->rows ) { 129 | _eigenvectors = cvCreateMat( M, D, type ); 130 | cvT( eigenvectors, _eigenvectors ); 131 | } else { 132 | _eigenvectors = (CvMat*)eigenvectors; 133 | } 134 | //cvProjectPCA( samples, avg, _eigenvectors, proj ); 135 | for( n = 0; n < N; n++ ) { // want mean subtracted samples for laster too 136 | for( d = 0; d < D; d++ ) { 137 | cvmSet( samples0, d, n, cvmGet( samples, d, n ) - cvmGet( avg, d, 0 ) ); 138 | } 139 | } 140 | CvMat *_proj = cvCreateMat( M, N, type ); 141 | cvMatMul( _eigenvectors, samples0, _proj ); 142 | cvT( _proj, proj ); 143 | cvReleaseMat( &_proj ); 144 | 145 | // distance in feature space 146 | if( M > 0 ) { 147 | pLambda = cvGetRows( eigenvalues, &pLambdaHdr, 0, M ); 148 | cvPow( pLambda, sqrt_pLambda, 0.5 ); 149 | for( n = 0; n < N; n++ ) { 150 | subproj = cvGetRow( proj, &subprojhdr, n ); 151 | for( d = 0; d < M; d++ ) { 152 | cvmSet( sclsubproj, 0, d, cvmGet( subproj, 0, d ) / cvmGet( sqrt_pLambda, d, 0 ) ); 153 | } 154 | cvmSet( DIFS, 0, n, pow(cvNorm( sclsubproj, NULL, CV_L2 ), 2) ); 155 | } 156 | if( normalize == 1 ) { 157 | //cvLog( sqrt_pLambda, sqrt_pLambda ); 158 | //cvSum( sqrt_pLambda ); 159 | for( d = 0; d < M; d++ ) { 160 | normterm += log( cvmGet( sqrt_pLambda, d, 0 ) ); 161 | } 162 | normterm += log(2*M_PI)*(M/2.0); 163 | } 164 | } 165 | 166 | // distance from feature space 167 | if( nEig > M ) { 168 | rLambda = cvGetRows( eigenvalues, &rLambdaHdr, M, nEig ); 169 | rho = cvAvg( rLambda ); 170 | for( n = 0; n < N; n++ ) { 171 | subsamples0 = cvGetCol( samples0, &subsamples0hdr, n ); 172 | subproj = cvGetRow( proj, &subprojhdr, n ); 173 | cvmSet( DFFS, 0, n, pow(cvNorm( subsamples0, NULL, CV_L2 ),2) - pow(cvNorm( subproj, NULL, CV_L2 ),2) ); 174 | } 175 | cvScale( DFFS, DFFS, 1.0/rho.val[0] ); 176 | if( normalize == 1 ) { 177 | normterm += log(2*M_PI*rho.val[0]) * ((nEig - M)/2.0); 178 | } 179 | } 180 | 181 | // logp sum 182 | for( n = 0; n < N; n++ ) { 183 | cvmSet( probs, 0, n, cvmGet( DIFS, 0, n ) / (-2) + cvmGet( DFFS, 0, n) / (-2) - normterm ); 184 | } 185 | if( normalize == 2 ) { 186 | double minval, maxval; 187 | cvMinMaxLoc( probs, &minval, &maxval ); 188 | cvSubS( probs, cvScalar( maxval ), probs ); 189 | } 190 | if( !logprob || normalize == 2 ) { 191 | for( n = 0; n < N; n++ ) { 192 | cvmSet( probs, 0, n, exp(cvmGet( probs, 0, n )) ); 193 | } 194 | if( normalize == 2 ) { 195 | CvScalar sumprob = cvSum( probs ); 196 | cvScale( probs, probs, 1.0 / sumprob.val[0] ); 197 | } 198 | } 199 | if( logprob && normalize == 2 ) { 200 | for( n = 0; n < N; n++ ) { 201 | cvmSet( probs, 0, n, log(cvmGet( probs, 0, n )) ); 202 | } 203 | } 204 | cvReleaseMat( &proj ); 205 | cvReleaseMat( &sclsubproj ); 206 | cvReleaseMat( &sqrt_pLambda ); 207 | cvReleaseMat( &DIFS ); 208 | cvReleaseMat( &DFFS ); 209 | cvReleaseMat( &samples0 ); 210 | if( D == eigenvectors->rows ) { 211 | cvReleaseMat( &_eigenvectors ); 212 | } 213 | __CV_END__; 214 | } 215 | 216 | /** 217 | // cvPcaDiffs - Distance "in" and "from" feature space 218 | // 219 | // @param avg D x 1 mean vector 220 | // @param eigenvalues nEig x 1 eigen values 221 | // @param eigenvectors M x D eigen vectors 222 | // @param [normalize = false] Compute normalization term or not 223 | // @param [logprob = false] Log probability or not 224 | // @return double likelihood 225 | */ 226 | double cvPcaDiffs( const CvMat* sample, const CvMat* avg, const CvMat* eigenvalues, 227 | const CvMat* eigenvectors, int normalize, bool logprob ) 228 | { 229 | double prob; 230 | CvMat *_probs = cvCreateMat( 1, 1, sample->type ); 231 | 232 | cvMatPcaDiffs( sample, avg, eigenvalues, eigenvectors, _probs, normalize, logprob ); 233 | prob = cvmGet(_probs, 0, 0); 234 | 235 | cvReleaseMat( &_probs ); 236 | return prob; 237 | } 238 | 239 | 240 | #endif 241 | -------------------------------------------------------------------------------- /src/opencvx/cvparticlerbox2.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * Rotated rectangle state particle filter + 3 | * 2nd order AR dynamics model ( in fact, next = current + speed + noise ) 4 | * 5 | * Use this file as a template of definitions of states and 6 | * state transition model for particle filter 7 | * 8 | * Currently cvparticle.h supports only linear combination of states transition 9 | * model only. you may create another cvParticleTransition to support more complex 10 | * non-linear state transition model. Most of other functions still should be 11 | * available modifications 12 | * 13 | * The MIT License 14 | * 15 | * Copyright (c) 2008, Naotoshi Seo 16 | * 17 | * Permission is hereby granted, free of charge, to any person obtaining a copy 18 | * of this software and associated documentation files (the "Software"), to deal 19 | * in the Software without restriction, including without limitation the rights 20 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 21 | * copies of the Software, and to permit persons to whom the Software is 22 | * furnished to do so, subject to the following conditions: 23 | * 24 | * The above copyright notice and this permission notice shall be included in 25 | * all copies or substantial portions of the Software. 26 | * 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 30 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 33 | * THE SOFTWARE. 34 | */ 35 | #ifndef CV_PARTICLE_ROTRECT2_H 36 | #define CV_PARTICLE_ROTRECT2_H 37 | 38 | #include "cvparticle.h" 39 | #include "cvdrawrectangle.h" 40 | #include "cvcropimageroi.h" 41 | #include "cvrect32f.h" 42 | #include 43 | using namespace std; 44 | 45 | /********************** Definition of a particle *****************************/ 46 | 47 | int num_states = 10; 48 | 49 | // Definition of meanings of 10 states. 50 | // This kinds of structures is not necessary to be defined, 51 | // but I recommend to define them because it makes clear meanings of states 52 | typedef struct CvParticleState { 53 | double x; // center coord of a rectangle 54 | double y; // center coord of a rectangle 55 | double width; // width of a rectangle 56 | double height; // height of a rectangle 57 | double angle; // rotation around center. degree 58 | double xp; // previous center coord of a rectangle 59 | double yp; // previous center coord of a rectangle 60 | double widthp; // previous width of a rectangle 61 | double heightp; // previous height of a rectangle 62 | double anglep; // previous rotation around center. degree 63 | } CvParticleState; 64 | 65 | // Definition of dynamics model 66 | // new_particle = cvMatMul( dynamics, particle ) + noise 67 | // curr_x =: curr_x + dx + noise = curr_x + (curr_x - prev_x) + noise 68 | // prev_x =: curr_x 69 | double dynamics[] = { 70 | 2, 0, 0, 0, 0, -1, 0, 0, 0, 0, 71 | 0, 2, 0, 0, 0, 0, -1, 0, 0, 0, 72 | 0, 0, 2, 0, 0, 0, 0, -1, 0, 0, 73 | 0, 0, 0, 2, 0, 0, 0, 0, -1, 0, 74 | 0, 0, 0, 0, 2, 0, 0, 0, 0, -1, 75 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76 | 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 77 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 78 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 79 | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 80 | }; 81 | 82 | /********************** Function Prototypes *********************************/ 83 | 84 | // Functions for CvParticleState structure ( constructor, getter, setter ) 85 | inline CvParticleState cvParticleState( double x, double y, double width, double height, double angle = 0, 86 | double xp = 0, double yp = 0, double widthp = 0, double heightp = 0, double anglep =0 ); 87 | CvParticleState cvParticleStateGet( const CvParticle* p, int p_id ); 88 | void cvParticleStateSet( const CvParticle* p, int p_id, CvParticleState &state ); 89 | 90 | // Particle Filter configuration 91 | void cvParticleStateConfig( CvParticle* p, CvSize imsize, CvParticleState& std ); 92 | void cvParticleStateAdditionalBound( CvParticle* p, CvSize imsize ); 93 | 94 | // Utility Functions 95 | void cvParticleStateDraw( const CvParticle* p, IplImage* frame, CvScalar color, int pid = -1 ); 96 | void cvParticleStatePrint( const CvParticleState& state ); 97 | 98 | /****************** Functions for CvParticleState structure ******************/ 99 | 100 | /** 101 | * Constructor 102 | */ 103 | inline CvParticleState cvParticleState( double x, double y, double width, double height, double angle, 104 | double xp, double yp, double widthp, double heightp, double anglep ) 105 | { 106 | CvParticleState state = { x, y, width, height, angle, 107 | xp, yp, widthp, heightp, anglep }; 108 | return state; 109 | } 110 | 111 | /** 112 | * Get a state (particle) 113 | * 114 | * @param p particle filter struct 115 | * @param p_id particle id 116 | */ 117 | CvParticleState cvParticleStateGet( const CvParticle* p, int p_id ) 118 | { 119 | CvParticleState s; 120 | s.x = cvmGet( p->particles, 0, p_id ); 121 | s.y = cvmGet( p->particles, 1, p_id ); 122 | s.width = cvmGet( p->particles, 2, p_id ); 123 | s.height = cvmGet( p->particles, 3, p_id ); 124 | s.angle = cvmGet( p->particles, 4, p_id ); 125 | s.xp = cvmGet( p->particles, 5, p_id ); 126 | s.yp = cvmGet( p->particles, 6, p_id ); 127 | s.widthp = cvmGet( p->particles, 7, p_id ); 128 | s.heightp = cvmGet( p->particles, 8, p_id ); 129 | s.anglep = cvmGet( p->particles, 9, p_id ); 130 | return s; 131 | } 132 | 133 | /** 134 | * Set a state (particle) 135 | * 136 | * @param p particle filter struct 137 | * @param p_id particle id 138 | * @param state A CvParticleState structure 139 | * @return void 140 | */ 141 | void cvParticleStateSet( const CvParticle* p, int p_id, CvParticleState &state ) 142 | { 143 | cvmSet( p->particles, 0, p_id, state.x ); 144 | cvmSet( p->particles, 1, p_id, state.y ); 145 | cvmSet( p->particles, 2, p_id, state.width ); 146 | cvmSet( p->particles, 3, p_id, state.height ); 147 | cvmSet( p->particles, 4, p_id, state.angle ); 148 | cvmSet( p->particles, 5, p_id, state.xp ); 149 | cvmSet( p->particles, 6, p_id, state.yp ); 150 | cvmSet( p->particles, 7, p_id, state.widthp ); 151 | cvmSet( p->particles, 8, p_id, state.heightp ); 152 | cvmSet( p->particles, 9, p_id, state.anglep ); 153 | } 154 | 155 | /*************************** Particle Filter Configuration *********************************/ 156 | 157 | /** 158 | * Configuration of Particle filter 159 | */ 160 | void cvParticleStateConfig( CvParticle* p, CvSize imsize, CvParticleState& std ) 161 | { 162 | // config dynamics model 163 | CvMat dynamicsmat = cvMat( p->num_states, p->num_states, CV_64FC1, dynamics ); 164 | 165 | // config random noise standard deviation 166 | CvRNG rng = cvRNG( time( NULL ) ); 167 | double stdarr[] = { 168 | std.x, 169 | std.y, 170 | std.width, 171 | std.height, 172 | std.angle, 173 | 0, 174 | 0, 175 | 0, 176 | 0, 177 | 0 178 | }; 179 | CvMat stdmat = cvMat( p->num_states, 1, CV_64FC1, stdarr ); 180 | 181 | // config minimum and maximum values of states 182 | // lowerbound, upperbound, circular flag (useful for degree) 183 | // lowerbound == upperbound to express no bounding 184 | double boundarr[] = { 185 | 0, imsize.width - 1, false, 186 | 0, imsize.height - 1, false, 187 | 1, imsize.width, false, 188 | 1, imsize.height, false, 189 | 0, 360, true, 190 | 0, 0, 0, 191 | 0, 0, 0, 192 | 0, 0, 0, 193 | 0, 0, 0, 194 | 0, 0, 0 195 | }; 196 | CvMat boundmat = cvMat( p->num_states, 3, CV_64FC1, boundarr ); 197 | cvParticleSetDynamics( p, &dynamicsmat ); 198 | cvParticleSetNoise( p, rng, &stdmat ); 199 | cvParticleSetBound( p, &boundmat ); 200 | } 201 | 202 | /** 203 | * @todo 204 | * CvParticle does not support this type of bounding currently 205 | * Call after transition 206 | */ 207 | void cvParticleStateAdditionalBound( CvParticle* p, CvSize imsize ) 208 | { 209 | for( int np = 0; np < p->num_particles; np++ ) 210 | { 211 | double x = cvmGet( p->particles, 0, np ); 212 | double y = cvmGet( p->particles, 1, np ); 213 | double width = cvmGet( p->particles, 2, np ); 214 | double height = cvmGet( p->particles, 3, np ); 215 | width = min( width, imsize.width - x ); // another state x is used 216 | height = min( height, imsize.height - y ); // another state y is used 217 | cvmSet( p->particles, 2, np, width ); 218 | cvmSet( p->particles, 3, np, height ); 219 | } 220 | } 221 | 222 | /***************************** Utility Functions ****************************************/ 223 | 224 | /** 225 | * Draw a particle or particles by rectangle on given image 226 | * 227 | * @param particle 228 | * @param img image to be drawn 229 | * @param color color of rectangle 230 | * @param [pid = -1] particle id. If -1, all particles are drawn 231 | */ 232 | void cvParticleStateDraw( const CvParticle* p, IplImage* img, CvScalar color, int pid ) 233 | { 234 | if( pid == -1 ) { // draw all particles 235 | int i; 236 | for( i = 0; i < p->num_particles; i++ ) { 237 | cvParticleStateDraw( p, img, color, i ); 238 | } 239 | } else { 240 | CvRect32f rect32f; 241 | CvBox32f box32f; 242 | CvParticleState s = cvParticleStateGet( p, pid ); 243 | box32f = cvBox32f( s.x, s.y, s.width, s.height, s.angle ); 244 | rect32f = cvRect32fFromBox32f( box32f ); 245 | cvDrawRectangle( img, rect32f, cvPoint2D32f(0,0), color ); 246 | } 247 | } 248 | 249 | void cvParticleStatePrint( const CvParticleState& state ) 250 | { 251 | printf( "x :%f ", state.x ); 252 | printf( "y :%f ", state.y ); 253 | printf( "width :%f ", state.width ); 254 | printf( "height :%f ", state.height ); 255 | printf( "angle :%f\n", state.angle ); 256 | printf( "xp:%f ", state.xp ); 257 | printf( "yp:%f ", state.yp ); 258 | printf( "widthp:%f ", state.widthp ); 259 | printf( "heightp:%f ", state.heightp ); 260 | printf( "anglep:%f\n", state.anglep ); 261 | fflush( stdout ); 262 | } 263 | 264 | #endif 265 | 266 | -------------------------------------------------------------------------------- /cmake/FindTBB.cmake: -------------------------------------------------------------------------------- 1 | # Locate Intel Threading Building Blocks include paths and libraries 2 | # FindTBB.cmake can be found at https://code.google.com/p/findtbb/ 3 | # Written by Hannes Hofmann 4 | # Improvements by Gino van den Bergen , 5 | # Florian Uhlig , 6 | # Jiri Marsik 7 | 8 | # The MIT License 9 | # 10 | # Copyright (c) 2011 Hannes Hofmann 11 | # 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy 13 | # of this software and associated documentation files (the "Software"), to deal 14 | # in the Software without restriction, including without limitation the rights 15 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | # copies of the Software, and to permit persons to whom the Software is 17 | # furnished to do so, subject to the following conditions: 18 | # 19 | # The above copyright notice and this permission notice shall be included in 20 | # all copies or substantial portions of the Software. 21 | # 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | # THE SOFTWARE. 29 | 30 | # GvdB: This module uses the environment variable TBB_ARCH_PLATFORM which defines architecture and compiler. 31 | # e.g. "ia32/vc8" or "em64t/cc4.1.0_libc2.4_kernel2.6.16.21" 32 | # TBB_ARCH_PLATFORM is set by the build script tbbvars[.bat|.sh|.csh], which can be found 33 | # in the TBB installation directory (TBB_INSTALL_DIR). 34 | # 35 | # GvdB: Mac OS X distribution places libraries directly in lib directory. 36 | # 37 | # For backwards compatibility, you may explicitely set the CMake variables TBB_ARCHITECTURE and TBB_COMPILER. 38 | # TBB_ARCHITECTURE [ ia32 | em64t | itanium ] 39 | # which architecture to use 40 | # TBB_COMPILER e.g. vc9 or cc3.2.3_libc2.3.2_kernel2.4.21 or cc4.0.1_os10.4.9 41 | # which compiler to use (detected automatically on Windows) 42 | 43 | # This module respects 44 | # TBB_INSTALL_DIR or $ENV{TBB21_INSTALL_DIR} or $ENV{TBB_INSTALL_DIR} 45 | 46 | # This module defines 47 | # TBB_INCLUDE_DIRS, where to find task_scheduler_init.h, etc. 48 | # TBB_LIBRARY_DIRS, where to find libtbb, libtbbmalloc 49 | # TBB_DEBUG_LIBRARY_DIRS, where to find libtbb_debug, libtbbmalloc_debug 50 | # TBB_INSTALL_DIR, the base TBB install directory 51 | # TBB_LIBRARIES, the libraries to link against to use TBB. 52 | # TBB_DEBUG_LIBRARIES, the libraries to link against to use TBB with debug symbols. 53 | # TBB_FOUND, If false, don't try to use TBB. 54 | # TBB_INTERFACE_VERSION, as defined in tbb/tbb_stddef.h 55 | 56 | 57 | if (WIN32) 58 | # has em64t/vc8 em64t/vc9 59 | # has ia32/vc7.1 ia32/vc8 ia32/vc9 60 | set(_TBB_DEFAULT_INSTALL_DIR "C:/Program Files/Intel/TBB" "C:/Program Files (x86)/Intel/TBB") 61 | set(_TBB_LIB_NAME "tbb") 62 | set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc") 63 | set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug") 64 | set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug") 65 | if (MSVC71) 66 | set (_TBB_COMPILER "vc7.1") 67 | endif(MSVC71) 68 | if (MSVC80) 69 | set(_TBB_COMPILER "vc8") 70 | endif(MSVC80) 71 | if (MSVC90) 72 | set(_TBB_COMPILER "vc9") 73 | endif(MSVC90) 74 | if(MSVC10) 75 | set(_TBB_COMPILER "vc10") 76 | endif(MSVC10) 77 | # Todo: add other Windows compilers such as ICL. 78 | set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE}) 79 | endif (WIN32) 80 | 81 | if (UNIX) 82 | if (APPLE) 83 | # MAC 84 | set(_TBB_DEFAULT_INSTALL_DIR "/Library/Frameworks/Intel_TBB.framework/Versions") 85 | # libs: libtbb.dylib, libtbbmalloc.dylib, *_debug 86 | set(_TBB_LIB_NAME "tbb") 87 | set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc") 88 | set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug") 89 | set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug") 90 | # default flavor on apple: ia32/cc4.0.1_os10.4.9 91 | # Jiri: There is no reason to presume there is only one flavor and 92 | # that user's setting of variables should be ignored. 93 | if(NOT TBB_COMPILER) 94 | set(_TBB_COMPILER "cc4.0.1_os10.4.9") 95 | elseif (NOT TBB_COMPILER) 96 | set(_TBB_COMPILER ${TBB_COMPILER}) 97 | endif(NOT TBB_COMPILER) 98 | if(NOT TBB_ARCHITECTURE) 99 | set(_TBB_ARCHITECTURE "ia32") 100 | elseif(NOT TBB_ARCHITECTURE) 101 | set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE}) 102 | endif(NOT TBB_ARCHITECTURE) 103 | else (APPLE) 104 | # LINUX 105 | set(_TBB_DEFAULT_INSTALL_DIR "/opt/intel/tbb" "/usr/local/include" "/usr/include") 106 | set(_TBB_LIB_NAME "tbb") 107 | set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc") 108 | set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug") 109 | set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug") 110 | # has em64t/cc3.2.3_libc2.3.2_kernel2.4.21 em64t/cc3.3.3_libc2.3.3_kernel2.6.5 em64t/cc3.4.3_libc2.3.4_kernel2.6.9 em64t/cc4.1.0_libc2.4_kernel2.6.16.21 111 | # has ia32/* 112 | # has itanium/* 113 | set(_TBB_COMPILER ${TBB_COMPILER}) 114 | set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE}) 115 | endif (APPLE) 116 | endif (UNIX) 117 | 118 | if (CMAKE_SYSTEM MATCHES "SunOS.*") 119 | # SUN 120 | # not yet supported 121 | # has em64t/cc3.4.3_kernel5.10 122 | # has ia32/* 123 | endif (CMAKE_SYSTEM MATCHES "SunOS.*") 124 | 125 | 126 | #-- Clear the public variables 127 | set (TBB_FOUND "NO") 128 | 129 | 130 | #-- Find TBB install dir and set ${_TBB_INSTALL_DIR} and cached ${TBB_INSTALL_DIR} 131 | # first: use CMake variable TBB_INSTALL_DIR 132 | if (TBB_INSTALL_DIR) 133 | set (_TBB_INSTALL_DIR ${TBB_INSTALL_DIR}) 134 | endif (TBB_INSTALL_DIR) 135 | # second: use environment variable 136 | if (NOT _TBB_INSTALL_DIR) 137 | if (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "") 138 | set (_TBB_INSTALL_DIR $ENV{TBB_INSTALL_DIR}) 139 | endif (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "") 140 | # Intel recommends setting TBB21_INSTALL_DIR 141 | if (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "") 142 | set (_TBB_INSTALL_DIR $ENV{TBB21_INSTALL_DIR}) 143 | endif (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "") 144 | if (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "") 145 | set (_TBB_INSTALL_DIR $ENV{TBB22_INSTALL_DIR}) 146 | endif (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "") 147 | if (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "") 148 | set (_TBB_INSTALL_DIR $ENV{TBB30_INSTALL_DIR}) 149 | endif (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "") 150 | endif (NOT _TBB_INSTALL_DIR) 151 | # third: try to find path automatically 152 | if (NOT _TBB_INSTALL_DIR) 153 | if (_TBB_DEFAULT_INSTALL_DIR) 154 | set (_TBB_INSTALL_DIR ${_TBB_DEFAULT_INSTALL_DIR}) 155 | endif (_TBB_DEFAULT_INSTALL_DIR) 156 | endif (NOT _TBB_INSTALL_DIR) 157 | # sanity check 158 | if (NOT _TBB_INSTALL_DIR) 159 | message ("ERROR: Unable to find Intel TBB install directory. ${_TBB_INSTALL_DIR}") 160 | else (NOT _TBB_INSTALL_DIR) 161 | # finally: set the cached CMake variable TBB_INSTALL_DIR 162 | if (NOT TBB_INSTALL_DIR) 163 | set (TBB_INSTALL_DIR ${_TBB_INSTALL_DIR} CACHE PATH "Intel TBB install directory") 164 | mark_as_advanced(TBB_INSTALL_DIR) 165 | endif (NOT TBB_INSTALL_DIR) 166 | 167 | 168 | #-- A macro to rewrite the paths of the library. This is necessary, because 169 | # find_library() always found the em64t/vc9 version of the TBB libs 170 | macro(TBB_CORRECT_LIB_DIR var_name) 171 | # if (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t") 172 | string(REPLACE em64t "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}}) 173 | # endif (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t") 174 | string(REPLACE ia32 "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}}) 175 | string(REPLACE vc7.1 "${_TBB_COMPILER}" ${var_name} ${${var_name}}) 176 | string(REPLACE vc8 "${_TBB_COMPILER}" ${var_name} ${${var_name}}) 177 | string(REPLACE vc9 "${_TBB_COMPILER}" ${var_name} ${${var_name}}) 178 | string(REPLACE vc10 "${_TBB_COMPILER}" ${var_name} ${${var_name}}) 179 | endmacro(TBB_CORRECT_LIB_DIR var_content) 180 | 181 | 182 | #-- Look for include directory and set ${TBB_INCLUDE_DIR} 183 | set (TBB_INC_SEARCH_DIR ${_TBB_INSTALL_DIR}/include) 184 | # Jiri: tbbvars now sets the CPATH environment variable to the directory 185 | # containing the headers. 186 | find_path(TBB_INCLUDE_DIR 187 | tbb/task_scheduler_init.h 188 | PATHS ${TBB_INC_SEARCH_DIR} ENV CPATH 189 | ) 190 | mark_as_advanced(TBB_INCLUDE_DIR) 191 | 192 | 193 | #-- Look for libraries 194 | # GvdB: $ENV{TBB_ARCH_PLATFORM} is set by the build script tbbvars[.bat|.sh|.csh] 195 | if (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "") 196 | set (_TBB_LIBRARY_DIR 197 | ${_TBB_INSTALL_DIR}/lib/$ENV{TBB_ARCH_PLATFORM} 198 | ${_TBB_INSTALL_DIR}/$ENV{TBB_ARCH_PLATFORM}/lib 199 | ) 200 | endif (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "") 201 | # Jiri: This block isn't mutually exclusive with the previous one 202 | # (hence no else), instead I test if the user really specified 203 | # the variables in question. 204 | if ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL "")) 205 | # HH: deprecated 206 | message(STATUS "[Warning] FindTBB.cmake: The use of TBB_ARCHITECTURE and TBB_COMPILER is deprecated and may not be supported in future versions. Please set \$ENV{TBB_ARCH_PLATFORM} (using tbbvars.[bat|csh|sh]).") 207 | # Jiri: It doesn't hurt to look in more places, so I store the hints from 208 | # ENV{TBB_ARCH_PLATFORM} and the TBB_ARCHITECTURE and TBB_COMPILER 209 | # variables and search them both. 210 | set (_TBB_LIBRARY_DIR "${_TBB_INSTALL_DIR}/${_TBB_ARCHITECTURE}/${_TBB_COMPILER}/lib" ${_TBB_LIBRARY_DIR}) 211 | endif ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL "")) 212 | 213 | # GvdB: Mac OS X distribution places libraries directly in lib directory. 214 | list(APPEND _TBB_LIBRARY_DIR ${_TBB_INSTALL_DIR}/lib) 215 | 216 | # Jiri: No reason not to check the default paths. From recent versions, 217 | # tbbvars has started exporting the LIBRARY_PATH and LD_LIBRARY_PATH 218 | # variables, which now point to the directories of the lib files. 219 | # It all makes more sense to use the ${_TBB_LIBRARY_DIR} as a HINTS 220 | # argument instead of the implicit PATHS as it isn't hard-coded 221 | # but computed by system introspection. Searching the LIBRARY_PATH 222 | # and LD_LIBRARY_PATH environment variables is now even more important 223 | # that tbbvars doesn't export TBB_ARCH_PLATFORM and it facilitates 224 | # the use of TBB built from sources. 225 | find_library(TBB_LIBRARY ${_TBB_LIB_NAME} HINTS ${_TBB_LIBRARY_DIR} 226 | PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH) 227 | find_library(TBB_MALLOC_LIBRARY ${_TBB_LIB_MALLOC_NAME} HINTS ${_TBB_LIBRARY_DIR} 228 | PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH) 229 | 230 | #Extract path from TBB_LIBRARY name 231 | get_filename_component(TBB_LIBRARY_DIR ${TBB_LIBRARY} PATH) 232 | 233 | #TBB_CORRECT_LIB_DIR(TBB_LIBRARY) 234 | #TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY) 235 | mark_as_advanced(TBB_LIBRARY TBB_MALLOC_LIBRARY) 236 | 237 | #-- Look for debug libraries 238 | # Jiri: Changed the same way as for the release libraries. 239 | find_library(TBB_LIBRARY_DEBUG ${_TBB_LIB_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR} 240 | PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH) 241 | find_library(TBB_MALLOC_LIBRARY_DEBUG ${_TBB_LIB_MALLOC_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR} 242 | PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH) 243 | 244 | # Jiri: Self-built TBB stores the debug libraries in a separate directory. 245 | # Extract path from TBB_LIBRARY_DEBUG name 246 | get_filename_component(TBB_LIBRARY_DEBUG_DIR ${TBB_LIBRARY_DEBUG} PATH) 247 | 248 | #TBB_CORRECT_LIB_DIR(TBB_LIBRARY_DEBUG) 249 | #TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY_DEBUG) 250 | mark_as_advanced(TBB_LIBRARY_DEBUG TBB_MALLOC_LIBRARY_DEBUG) 251 | 252 | 253 | if (TBB_INCLUDE_DIR) 254 | if (TBB_LIBRARY) 255 | set (TBB_FOUND "YES") 256 | set (TBB_LIBRARIES ${TBB_LIBRARY} ${TBB_MALLOC_LIBRARY} ${TBB_LIBRARIES}) 257 | set (TBB_DEBUG_LIBRARIES ${TBB_LIBRARY_DEBUG} ${TBB_MALLOC_LIBRARY_DEBUG} ${TBB_DEBUG_LIBRARIES}) 258 | set (TBB_INCLUDE_DIRS ${TBB_INCLUDE_DIR} CACHE PATH "TBB include directory" FORCE) 259 | set (TBB_LIBRARY_DIRS ${TBB_LIBRARY_DIR} CACHE PATH "TBB library directory" FORCE) 260 | # Jiri: Self-built TBB stores the debug libraries in a separate directory. 261 | set (TBB_DEBUG_LIBRARY_DIRS ${TBB_LIBRARY_DEBUG_DIR} CACHE PATH "TBB debug library directory" FORCE) 262 | mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARY_DIRS TBB_DEBUG_LIBRARY_DIRS TBB_LIBRARIES TBB_DEBUG_LIBRARIES) 263 | message(STATUS "Found Intel TBB") 264 | endif (TBB_LIBRARY) 265 | endif (TBB_INCLUDE_DIR) 266 | 267 | if (NOT TBB_FOUND) 268 | message("ERROR: Intel TBB NOT found!") 269 | message(STATUS "Looked for Threading Building Blocks in ${_TBB_INSTALL_DIR}") 270 | # do only throw fatal, if this pkg is REQUIRED 271 | if (TBB_FIND_REQUIRED) 272 | message(FATAL_ERROR "Could NOT find TBB library.") 273 | endif (TBB_FIND_REQUIRED) 274 | endif (NOT TBB_FOUND) 275 | 276 | endif (NOT _TBB_INSTALL_DIR) 277 | 278 | if (TBB_FOUND) 279 | set(TBB_INTERFACE_VERSION 0) 280 | FILE(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _TBB_VERSION_CONTENTS) 281 | STRING(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}") 282 | set(TBB_INTERFACE_VERSION "${TBB_INTERFACE_VERSION}") 283 | endif (TBB_FOUND) 284 | --------------------------------------------------------------------------------