├── .DS_Store ├── LICENSE ├── OpenCV IP Video Cam.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── Mark.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── Mark.xcuserdatad │ └── xcschemes │ ├── OpenCV IP Video Cam.xcscheme │ └── xcschememanagement.plist ├── OpenCV IP Video Cam └── main.cpp └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/markuscraig/opencv-rtsp-motion-detection/af7a3558d9c251b1a809140397ef09338e8cb010/.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mark Craig 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OpenCV IP Video Cam.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4D70541F1B5C24DD005D4182 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D70541E1B5C24DD005D4182 /* main.cpp */; }; 11 | 4D7054461B5D9F83005D4182 /* libopencv_core.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7054411B5D9F83005D4182 /* libopencv_core.dylib */; }; 12 | 4D7054471B5D9F83005D4182 /* libopencv_gpu.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7054421B5D9F83005D4182 /* libopencv_gpu.dylib */; }; 13 | 4D7054481B5D9F83005D4182 /* libopencv_highgui.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7054431B5D9F83005D4182 /* libopencv_highgui.dylib */; }; 14 | 4D7054491B5D9F83005D4182 /* libopencv_imgproc.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7054441B5D9F83005D4182 /* libopencv_imgproc.dylib */; }; 15 | 4D70544A1B5D9F83005D4182 /* libopencv_video.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7054451B5D9F83005D4182 /* libopencv_video.dylib */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXCopyFilesBuildPhase section */ 19 | 4D7054191B5C24DD005D4182 /* CopyFiles */ = { 20 | isa = PBXCopyFilesBuildPhase; 21 | buildActionMask = 2147483647; 22 | dstPath = /usr/share/man/man1/; 23 | dstSubfolderSpec = 0; 24 | files = ( 25 | ); 26 | runOnlyForDeploymentPostprocessing = 1; 27 | }; 28 | /* End PBXCopyFilesBuildPhase section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 4D70541B1B5C24DD005D4182 /* OpenCV IP Video Cam */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "OpenCV IP Video Cam"; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 4D70541E1B5C24DD005D4182 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 33 | 4D7054411B5D9F83005D4182 /* libopencv_core.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libopencv_core.dylib; path = ../../../../../../../usr/local/lib/libopencv_core.dylib; sourceTree = ""; }; 34 | 4D7054421B5D9F83005D4182 /* libopencv_gpu.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libopencv_gpu.dylib; path = ../../../../../../../usr/local/lib/libopencv_gpu.dylib; sourceTree = ""; }; 35 | 4D7054431B5D9F83005D4182 /* libopencv_highgui.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libopencv_highgui.dylib; path = ../../../../../../../usr/local/lib/libopencv_highgui.dylib; sourceTree = ""; }; 36 | 4D7054441B5D9F83005D4182 /* libopencv_imgproc.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libopencv_imgproc.dylib; path = ../../../../../../../usr/local/lib/libopencv_imgproc.dylib; sourceTree = ""; }; 37 | 4D7054451B5D9F83005D4182 /* libopencv_video.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libopencv_video.dylib; path = ../../../../../../../usr/local/lib/libopencv_video.dylib; sourceTree = ""; }; 38 | /* End PBXFileReference section */ 39 | 40 | /* Begin PBXFrameworksBuildPhase section */ 41 | 4D7054181B5C24DD005D4182 /* Frameworks */ = { 42 | isa = PBXFrameworksBuildPhase; 43 | buildActionMask = 2147483647; 44 | files = ( 45 | 4D7054471B5D9F83005D4182 /* libopencv_gpu.dylib in Frameworks */, 46 | 4D7054461B5D9F83005D4182 /* libopencv_core.dylib in Frameworks */, 47 | 4D7054481B5D9F83005D4182 /* libopencv_highgui.dylib in Frameworks */, 48 | 4D70544A1B5D9F83005D4182 /* libopencv_video.dylib in Frameworks */, 49 | 4D7054491B5D9F83005D4182 /* libopencv_imgproc.dylib in Frameworks */, 50 | ); 51 | runOnlyForDeploymentPostprocessing = 0; 52 | }; 53 | /* End PBXFrameworksBuildPhase section */ 54 | 55 | /* Begin PBXGroup section */ 56 | 4D7054121B5C24DD005D4182 = { 57 | isa = PBXGroup; 58 | children = ( 59 | 4D7054411B5D9F83005D4182 /* libopencv_core.dylib */, 60 | 4D7054421B5D9F83005D4182 /* libopencv_gpu.dylib */, 61 | 4D7054431B5D9F83005D4182 /* libopencv_highgui.dylib */, 62 | 4D7054441B5D9F83005D4182 /* libopencv_imgproc.dylib */, 63 | 4D7054451B5D9F83005D4182 /* libopencv_video.dylib */, 64 | 4D70541D1B5C24DD005D4182 /* OpenCV IP Video Cam */, 65 | 4D70541C1B5C24DD005D4182 /* Products */, 66 | ); 67 | sourceTree = ""; 68 | }; 69 | 4D70541C1B5C24DD005D4182 /* Products */ = { 70 | isa = PBXGroup; 71 | children = ( 72 | 4D70541B1B5C24DD005D4182 /* OpenCV IP Video Cam */, 73 | ); 74 | name = Products; 75 | sourceTree = ""; 76 | }; 77 | 4D70541D1B5C24DD005D4182 /* OpenCV IP Video Cam */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 4D70541E1B5C24DD005D4182 /* main.cpp */, 81 | ); 82 | path = "OpenCV IP Video Cam"; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 4D70541A1B5C24DD005D4182 /* OpenCV IP Video Cam */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 4D7054221B5C24DD005D4182 /* Build configuration list for PBXNativeTarget "OpenCV IP Video Cam" */; 91 | buildPhases = ( 92 | 4D7054171B5C24DD005D4182 /* Sources */, 93 | 4D7054181B5C24DD005D4182 /* Frameworks */, 94 | 4D7054191B5C24DD005D4182 /* CopyFiles */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = "OpenCV IP Video Cam"; 101 | productName = "OpenCV IP Video Cam"; 102 | productReference = 4D70541B1B5C24DD005D4182 /* OpenCV IP Video Cam */; 103 | productType = "com.apple.product-type.tool"; 104 | }; 105 | /* End PBXNativeTarget section */ 106 | 107 | /* Begin PBXProject section */ 108 | 4D7054131B5C24DD005D4182 /* Project object */ = { 109 | isa = PBXProject; 110 | attributes = { 111 | LastUpgradeCheck = 0640; 112 | ORGANIZATIONNAME = "Mark Craig"; 113 | TargetAttributes = { 114 | 4D70541A1B5C24DD005D4182 = { 115 | CreatedOnToolsVersion = 6.4; 116 | }; 117 | }; 118 | }; 119 | buildConfigurationList = 4D7054161B5C24DD005D4182 /* Build configuration list for PBXProject "OpenCV IP Video Cam" */; 120 | compatibilityVersion = "Xcode 3.2"; 121 | developmentRegion = English; 122 | hasScannedForEncodings = 0; 123 | knownRegions = ( 124 | en, 125 | ); 126 | mainGroup = 4D7054121B5C24DD005D4182; 127 | productRefGroup = 4D70541C1B5C24DD005D4182 /* Products */; 128 | projectDirPath = ""; 129 | projectRoot = ""; 130 | targets = ( 131 | 4D70541A1B5C24DD005D4182 /* OpenCV IP Video Cam */, 132 | ); 133 | }; 134 | /* End PBXProject section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | 4D7054171B5C24DD005D4182 /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | 4D70541F1B5C24DD005D4182 /* main.cpp in Sources */, 142 | ); 143 | runOnlyForDeploymentPostprocessing = 0; 144 | }; 145 | /* End PBXSourcesBuildPhase section */ 146 | 147 | /* Begin XCBuildConfiguration section */ 148 | 4D7054201B5C24DD005D4182 /* Debug */ = { 149 | isa = XCBuildConfiguration; 150 | buildSettings = { 151 | ALWAYS_SEARCH_USER_PATHS = NO; 152 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 153 | CLANG_CXX_LIBRARY = "libc++"; 154 | CLANG_ENABLE_MODULES = YES; 155 | CLANG_ENABLE_OBJC_ARC = YES; 156 | CLANG_WARN_BOOL_CONVERSION = YES; 157 | CLANG_WARN_CONSTANT_CONVERSION = YES; 158 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 159 | CLANG_WARN_EMPTY_BODY = YES; 160 | CLANG_WARN_ENUM_CONVERSION = YES; 161 | CLANG_WARN_INT_CONVERSION = YES; 162 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 163 | CLANG_WARN_UNREACHABLE_CODE = YES; 164 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 165 | COPY_PHASE_STRIP = NO; 166 | DEBUG_INFORMATION_FORMAT = dwarf; 167 | ENABLE_STRICT_OBJC_MSGSEND = YES; 168 | GCC_C_LANGUAGE_STANDARD = gnu99; 169 | GCC_DYNAMIC_NO_PIC = NO; 170 | GCC_NO_COMMON_BLOCKS = YES; 171 | GCC_OPTIMIZATION_LEVEL = 0; 172 | GCC_PREPROCESSOR_DEFINITIONS = ( 173 | "DEBUG=1", 174 | "$(inherited)", 175 | ); 176 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 177 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 178 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 179 | GCC_WARN_UNDECLARED_SELECTOR = YES; 180 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 181 | GCC_WARN_UNUSED_FUNCTION = YES; 182 | GCC_WARN_UNUSED_VARIABLE = YES; 183 | MACOSX_DEPLOYMENT_TARGET = 10.10; 184 | MTL_ENABLE_DEBUG_INFO = YES; 185 | ONLY_ACTIVE_ARCH = YES; 186 | SDKROOT = macosx; 187 | }; 188 | name = Debug; 189 | }; 190 | 4D7054211B5C24DD005D4182 /* Release */ = { 191 | isa = XCBuildConfiguration; 192 | buildSettings = { 193 | ALWAYS_SEARCH_USER_PATHS = NO; 194 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 195 | CLANG_CXX_LIBRARY = "libc++"; 196 | CLANG_ENABLE_MODULES = YES; 197 | CLANG_ENABLE_OBJC_ARC = YES; 198 | CLANG_WARN_BOOL_CONVERSION = YES; 199 | CLANG_WARN_CONSTANT_CONVERSION = YES; 200 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 201 | CLANG_WARN_EMPTY_BODY = YES; 202 | CLANG_WARN_ENUM_CONVERSION = YES; 203 | CLANG_WARN_INT_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN_UNREACHABLE_CODE = YES; 206 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 207 | COPY_PHASE_STRIP = NO; 208 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 209 | ENABLE_NS_ASSERTIONS = NO; 210 | ENABLE_STRICT_OBJC_MSGSEND = YES; 211 | GCC_C_LANGUAGE_STANDARD = gnu99; 212 | GCC_NO_COMMON_BLOCKS = YES; 213 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 214 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 215 | GCC_WARN_UNDECLARED_SELECTOR = YES; 216 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 217 | GCC_WARN_UNUSED_FUNCTION = YES; 218 | GCC_WARN_UNUSED_VARIABLE = YES; 219 | MACOSX_DEPLOYMENT_TARGET = 10.10; 220 | MTL_ENABLE_DEBUG_INFO = NO; 221 | SDKROOT = macosx; 222 | }; 223 | name = Release; 224 | }; 225 | 4D7054231B5C24DD005D4182 /* Debug */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ALWAYS_SEARCH_USER_PATHS = YES; 229 | FRAMEWORK_SEARCH_PATHS = /usr/local/lib; 230 | HEADER_SEARCH_PATHS = ( 231 | "$(inherited)", 232 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 233 | /usr/local/include, 234 | ); 235 | LIBRARY_SEARCH_PATHS = /usr/local/lib; 236 | PRODUCT_NAME = "$(TARGET_NAME)"; 237 | }; 238 | name = Debug; 239 | }; 240 | 4D7054241B5C24DD005D4182 /* Release */ = { 241 | isa = XCBuildConfiguration; 242 | buildSettings = { 243 | ALWAYS_SEARCH_USER_PATHS = YES; 244 | FRAMEWORK_SEARCH_PATHS = /usr/local/lib; 245 | HEADER_SEARCH_PATHS = ( 246 | "$(inherited)", 247 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 248 | /usr/local/include, 249 | ); 250 | LIBRARY_SEARCH_PATHS = /usr/local/lib; 251 | PRODUCT_NAME = "$(TARGET_NAME)"; 252 | }; 253 | name = Release; 254 | }; 255 | /* End XCBuildConfiguration section */ 256 | 257 | /* Begin XCConfigurationList section */ 258 | 4D7054161B5C24DD005D4182 /* Build configuration list for PBXProject "OpenCV IP Video Cam" */ = { 259 | isa = XCConfigurationList; 260 | buildConfigurations = ( 261 | 4D7054201B5C24DD005D4182 /* Debug */, 262 | 4D7054211B5C24DD005D4182 /* Release */, 263 | ); 264 | defaultConfigurationIsVisible = 0; 265 | defaultConfigurationName = Release; 266 | }; 267 | 4D7054221B5C24DD005D4182 /* Build configuration list for PBXNativeTarget "OpenCV IP Video Cam" */ = { 268 | isa = XCConfigurationList; 269 | buildConfigurations = ( 270 | 4D7054231B5C24DD005D4182 /* Debug */, 271 | 4D7054241B5C24DD005D4182 /* Release */, 272 | ); 273 | defaultConfigurationIsVisible = 0; 274 | }; 275 | /* End XCConfigurationList section */ 276 | }; 277 | rootObject = 4D7054131B5C24DD005D4182 /* Project object */; 278 | } 279 | -------------------------------------------------------------------------------- /OpenCV IP Video Cam.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /OpenCV IP Video Cam.xcodeproj/project.xcworkspace/xcuserdata/Mark.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/markuscraig/opencv-rtsp-motion-detection/af7a3558d9c251b1a809140397ef09338e8cb010/OpenCV IP Video Cam.xcodeproj/project.xcworkspace/xcuserdata/Mark.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /OpenCV IP Video Cam.xcodeproj/xcuserdata/Mark.xcuserdatad/xcschemes/OpenCV IP Video Cam.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 51 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 70 | 72 | 78 | 79 | 80 | 81 | 83 | 84 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /OpenCV IP Video Cam.xcodeproj/xcuserdata/Mark.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | OpenCV IP Video Cam.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4D70541A1B5C24DD005D4182 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /OpenCV IP Video Cam/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // OpenCV IP Video Cam 4 | // 5 | // Created by Mark Craig on 7/19/15. 6 | // Copyright (c) 2015 Mark Craig. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "opencv2/opencv.hpp" 11 | 12 | using namespace cv; 13 | using namespace std; 14 | 15 | int main(int argc, const char * argv[]) { 16 | std::cout << "Starting...\n"; 17 | 18 | /* 19 | VideoCapture cap(0); // open the default camera 20 | if(!cap.isOpened()) // check if we succeeded 21 | return -1; 22 | 23 | Mat edges; 24 | namedWindow("edges",1); 25 | for(;;) 26 | { 27 | Mat frame; 28 | cap >> frame; // get a new frame from camera 29 | cvtColor(frame, edges, CV_BGR2GRAY); 30 | GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); 31 | Canny(edges, edges, 0, 30, 3); 32 | imshow("edges", edges); 33 | if(waitKey(30) >= 0) break; 34 | } 35 | 36 | // the camera will be deinitialized automatically in VideoCapture destructor 37 | return 0; 38 | */ 39 | 40 | 41 | cv::namedWindow("Annotated Frame"); 42 | cv::namedWindow("Contour Delta"); 43 | //cv::namedWindow("Edges"); 44 | 45 | cv::VideoCapture capture("rtsp://admin:Appo7ite@192.168.0.201:554/Streaming/Channels/1"); 46 | 47 | int frameIndex = 0; 48 | Mat lastFrame; 49 | cv::VideoWriter writer; 50 | 51 | while ( capture.isOpened() ) // check !! 52 | { 53 | cv::Mat frame; 54 | if ( ! capture.read(frame) ) // another check !! 55 | break; 56 | 57 | #if 1 58 | Mat grayFrame, dilatedFrame, edges, deltaFrame, deltaCopyFrame; 59 | 60 | // scale down image 61 | cv::resize(frame, frame, Size(0,0), 0.33, 0.33); 62 | 63 | // convert to grayscale 64 | cvtColor(frame, grayFrame, CV_BGR2GRAY); 65 | 66 | // add blur 67 | GaussianBlur(grayFrame, grayFrame, Size(21,21), 0); 68 | 69 | if (frameIndex == 0) { 70 | frameIndex++; 71 | 72 | // position the windows 73 | moveWindow("Annotated Frame", 0, 0); 74 | moveWindow("Contour Delta", 0, grayFrame.size().height); 75 | 76 | // initialize the video writer 77 | std::string filename = "/Users/Mark/Desktop/capture.avi"; 78 | //int fcc = CV_FOURCC('D', 'I', 'V', '3'); 79 | int fcc = capture.get(CV_CAP_PROP_FOURCC); 80 | //int fcc = CV_FOURCC('H', '2', '6', '4'); 81 | //int fps = 20; 82 | int fps = capture.get(CV_CAP_PROP_FPS); 83 | //Size frameSize(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); 84 | Size frameSize(grayFrame.size().width, grayFrame.size().height); 85 | writer = VideoWriter(filename, fcc, fps, frameSize); 86 | 87 | // manually transcode with 'ffmpeg -i ~/Desktop/capture.avi -vcodec libx264 ~/Desktop/capture.mp4' 88 | 89 | std::cout << "FRAME SIZE = " << grayFrame.size().width << " x " << grayFrame.size().height << "\n"; 90 | 91 | // check if video writer is open 92 | if (!writer.isOpened()) { 93 | std::cout << "ERROR OPENING VIDEO FILE FOR WRITE" << std::endl; 94 | return -1; 95 | } 96 | 97 | // initialize the last reference frame 98 | lastFrame = grayFrame; 99 | continue; 100 | } else if ((frameIndex % 50) == 0) { 101 | frameIndex = 0; 102 | // update the last reference frame 103 | //lastFrame = grayFrame; 104 | } 105 | frameIndex++; 106 | 107 | 108 | // create difference frame 109 | cv::absdiff(lastFrame, grayFrame, deltaFrame); 110 | cv::threshold(deltaFrame, deltaFrame, 50, 255, cv::THRESH_BINARY); 111 | 112 | // dilate to fill-in holes and find contours 113 | int iterations = 2; 114 | cv::dilate(deltaFrame, deltaFrame, Mat(), Point(-1,-1), iterations); 115 | 116 | /* 117 | // find the contours 118 | vector> contours; 119 | vector hierarchy; 120 | deltaFrame.copyTo(deltaCopyFrame); 121 | cv::findContours(deltaCopyFrame, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); 122 | for (int i=0; i < contours.size(); i++) { 123 | // ignore contour if smaller than min area 124 | if (contourArea(contours[i]) < 100) { 125 | continue; 126 | } 127 | 128 | Rect boundRect = boundingRect(Mat(contours[i])); 129 | 130 | Scalar color = Scalar(255, 0, 0); 131 | rectangle(frame, boundRect.tl(), boundRect.br(), color, 2, 8, 0); 132 | } 133 | */ 134 | 135 | /// Approximate contours to polygons + get bounding rects and circles 136 | vector> contours; 137 | vector hierarchy; 138 | //deltaFrame.copyTo(deltaCopyFrame); 139 | cv::findContours(deltaFrame, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0)); 140 | vector > contours_poly( contours.size() ); 141 | vector boundRect( contours.size() ); 142 | vectorcenter( contours.size() ); 143 | vectorradius( contours.size() ); 144 | 145 | for( int i = 0; i < contours.size(); i++ ) 146 | { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); 147 | boundRect[i] = boundingRect( Mat(contours_poly[i]) ); 148 | minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] ); 149 | } 150 | 151 | 152 | /// Draw polygonal contour + bonding rects + circles 153 | //Mat drawing = Mat::zeros( deltaFrame.size(), CV_8UC3 ); 154 | for( int i = 0; i< contours.size(); i++ ) 155 | { 156 | Scalar color = Scalar(255, 0, 0); 157 | drawContours( frame, contours_poly, i, color, 1, 8, vector(), 0, Point() ); 158 | rectangle( frame, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 ); 159 | circle( frame, center[i], (int)radius[i], color, 2, 8, 0 ); 160 | } 161 | 162 | 163 | 164 | // find edges with canny 165 | //Canny(blurFrame, edges, 0, 30, 3); 166 | 167 | imshow("Annotated Frame", frame); 168 | //imshow("Edges", edges); 169 | imshow("Contour Delta", deltaFrame); 170 | #else 171 | imshow("Frame", frame); 172 | #endif 173 | 174 | // write the frame to the video file 175 | //std::cout << "WRITING FRAME..."; 176 | writer.write(frame); 177 | 178 | // wait for escape (needed to exit and properly write the video file) 179 | switch(waitKey(1)) { 180 | case 27: 181 | capture.release(); 182 | writer.release(); 183 | return 0; 184 | } 185 | } 186 | return 0; 187 | 188 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | 3 | Basic example of using OpenCV 2.4 to access an RTSP H.264 video stream from an IP video camera and performing real-time motion detection and object tracking. 4 | 5 | # Installing OpenCV on OSX 6 | 7 | 1. Install Homebrew if not already installed 8 | 9 | [Click here for directions](http://brew.sh/) 10 | 11 | 2. Install OpenCV using... 12 | 13 | $ brew tap homebrew/science 14 | $ brew install opencv --with-ffmpeg 15 | 16 | # Demo Video 17 | 18 | [![IMAGE ALT TEXT HERE](http://img.youtube.com/vi/uOWWz_5TU30/0.jpg)](http://www.youtube.com/watch?v=uOWWz_5TU30) 19 | 20 | --------------------------------------------------------------------------------