### Installation Directory Setup Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Configures installation directories, ensuring that support files are placed next to the executable for in-place execution. This is particularly important for Visual Studio builds. ```cmake set(BUILD_BUNDLE_DIR "$") set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1) if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}") ``` -------------------------------- ### Installation Bundle Directory Setup Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Configures the installation prefix to create a relocatable bundle in the build directory. It also ensures the build bundle directory is clean before installation. ```cmake # === Installation === # By default, "installing" just makes a relocatable bundle in the build # directory. set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() # Start with a clean build bundle directory every time. install(CODE " file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") " COMPONENT Runtime) set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Sets the minimum CMake version and defines the project name and languages. This is a standard starting point for most CMake projects. ```cmake cmake_minimum_required(VERSION 3.13) project(runner LANGUAGES CXX) ``` -------------------------------- ### Installing Application Executable Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs the application executable to the root of the installation prefix. This makes the main application binary available. ```cmake install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime) ``` -------------------------------- ### Defining Installation Directories Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Defines the installation directories for data and libraries within the application bundle. These paths are used during the installation process. ```cmake set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") ``` -------------------------------- ### Install Native Assets Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Installs native assets provided by the build.dart script from all packages into the 'lib' subdirectory of the installation bundle. ```cmake # Copy the native assets provided by the build.dart from all packages. set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") install(DIRECTORY "${NATIVE_ASSETS_DIR}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Setting Installation Prefix for Bundle Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Configures the installation prefix to point to the build bundle directory. This ensures that the 'install' command creates a relocatable application bundle. ```cmake set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() ``` -------------------------------- ### Installing Flutter Library Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs the main Flutter library file to the library directory of the bundle. This is a core component for running the Flutter application. ```cmake install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Installing Bundled Plugin Libraries Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Iterates through a list of bundled plugin libraries and installs each one to the library directory of the bundle. This ensures all necessary plugin code is included. ```cmake foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES}) install(FILES "${bundled_library}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endforeach(bundled_library) ``` -------------------------------- ### Cleaning Build Bundle Directory on Install Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs a code block that recursively removes the build bundle directory before installation. This ensures a clean installation environment. ```cmake install(CODE " file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") " COMPONENT Runtime) ``` -------------------------------- ### Project and CMake Version Setup Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Sets the minimum required CMake version and the project name. It's essential for ensuring compatibility with the CMake version used. ```cmake cmake_minimum_required(VERSION 3.14) project(opencv_dart_example LANGUAGES CXX) ``` -------------------------------- ### Cross-Building System Root Setup Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Configures CMake for cross-building by setting the system root and adjusting search path modes when FLUTTER_TARGET_PLATFORM_SYSROOT is defined. ```cmake # Root filesystem for cross-building. if(FLUTTER_TARGET_PLATFORM_SYSROOT) set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) endif() ``` -------------------------------- ### Install AOT Library Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Installs the Ahead-Of-Time (AOT) compilation library for Profile and Release builds. This library is used for performance optimizations. ```cmake install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" CONFIGURATIONS Profile;Release COMPONENT Runtime) ``` -------------------------------- ### Install Bundled Plugin Libraries Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Installs any bundled native libraries from plugins into the application bundle. This ensures that plugins have their required dependencies available at runtime. ```cmake if(PLUGIN_BUNDLED_LIBRARIES) install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endif() ``` -------------------------------- ### Installing Native Assets Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs native assets provided by packages into the library directory of the bundle. These assets are often required by plugins. ```cmake set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/") install(DIRECTORY "${NATIVE_ASSETS_DIR}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Runtime Output Directory Configuration Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Sets the runtime output directory for the executable to a subdirectory within the build directory. This is done to ensure the executable only launches correctly when part of the installed bundle, due to resource location requirements. ```cmake # Only the install-generated bundle's copy of the executable will launch # correctly, since the resources must in the right relative locations. To avoid # people trying to run the unbundled copy, put it in a subdirectory instead of # the default top-level location. set_target_properties(${BINARY_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run" ) ``` -------------------------------- ### Re-copying Flutter Assets on Install Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs code to ensure the Flutter assets directory is completely re-copied on each install. This prevents stale assets from being included in the bundle. ```cmake set(FLUTTER_ASSET_DIR_NAME "flutter_assets") install(CODE " file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") " COMPONENT Runtime) install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Installing ICU Data File Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs the ICU data file to the data directory of the bundle. This file is necessary for internationalization support. ```cmake install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Install Native Assets Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Installs native assets provided by the build.dart script into the application bundle. These assets are typically required by native code or plugins. ```cmake set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/") install(DIRECTORY "${NATIVE_ASSETS_DIR}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) ``` -------------------------------- ### Installing AOT Library for Non-Debug Builds Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Installs the Ahead-Of-Time (AOT) compiled library to the bundle's library directory, but only for non-Debug build types. This is used for performance optimization. ```cmake if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endif() ``` -------------------------------- ### Construct and Access OpenCV Dart Mat Objects Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates various ways to create `Mat` objects, including from zeros, ones, identity matrices, lists, and scalars. Shows how to access properties like rows, columns, channels, type, shape, and element size. Includes examples of typed and generic element access using `at`/`set` and specific typed methods. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { // --- Construction --- final zeros = cv.Mat.zeros(4, 4, cv.MatType.CV_8UC1); final ones = cv.Mat.ones(3, 3, cv.MatType.CV_32FC1); final eye = cv.Mat.eye(3, 3, cv.MatType.CV_64FC1); // From flat list (copies data twice; use fromVec for performance) final fromList = cv.Mat.fromList(2, 3, cv.MatType.CV_8UC1, [0,1,2,3,4,5]); // From 2D / 3D list final from2D = cv.Mat.from2DList([[1.0,2.0],[3.0,4.0]], cv.MatType.CV_64FC1); final from3D = cv.Mat.from3DList([[[255,0,0],[0,255,0]]], cv.MatType.CV_8UC3); // Scalar-filled final blue = cv.Mat.fromScalar(100, 100, cv.MatType.CV_8UC3, cv.Scalar(255,0,0,0)); // Random normal / uniform final randN = cv.Mat.randn(3, 3, cv.MatType.CV_32FC1); final randU = cv.Mat.randu(3, 3, cv.MatType.CV_8UC1, low: cv.Scalar.all(0), high: cv.Scalar.all(256)); // --- Properties --- print(zeros.rows); // 4 print(zeros.cols); // 4 print(zeros.channels); // 1 print(zeros.type); // MatType(CV_8UC1) print(zeros.shape); // [4, 4, 1] print(zeros.isEmpty); // false print(zeros.elemSize); // 1 // --- Element access --- final m = cv.Mat.fromList(2, 2, cv.MatType.CV_32FC1, [1.0, 2.0, 3.0, 4.0]); print(m.atF32(0, i1: 1)); // 2.0 — typed accessor print(m.at(0, 1)); // 2.0 — generic accessor m.setF32(0, 3.14, i1: 1); m.set(0, 1, 99.0); // Vec accessors for multi-channel final bgr = cv.Mat.fromScalar(1, 1, cv.MatType.CV_8UC3, cv.Scalar(10,20,30,0)); final pixel = bgr.at(0, 0); print('${pixel.val1} ${pixel.val2} ${pixel.val3}'); // 10 20 30 bgr.set(0, 0, cv.Vec3b(0, 128, 255)); // Pixel iteration final mat8u = cv.Mat.ones(2, 2, cv.MatType.CV_8UC3); mat8u.forEachPixel((row, col, pixel) { pixel[0] = row * 10; // modifies in-place }); // --- Arithmetic --- final a = cv.Mat.ones(3, 3, cv.MatType.CV_32FC1).multiply(2.0); final b = cv.Mat.ones(3, 3, cv.MatType.CV_32FC1); final sum = a.add(b); // element-wise final diff = a.subtract(b); final scaled = a.multiply(3.0); // in-place variant a.multiply(3.0, inplace: true); // --- Shape manipulation --- final col0 = m.col(0); // column slice (shared memory) final row0 = m.row(0); // row slice final rng = cv.Mat.fromRange(m, 0, 1); // row range final t = m.t(); // transpose final reshaped = m.reshape(1, 4); // 2×2 → 4×1 // --- Conversion --- final fp32 = m.convertTo(cv.MatType.CV_64FC1, alpha: 2.0, beta: 1.0); final copy = m.clone(); // --- Serialization --- print(m.toList()); // List> print(bgr.toList3D()); // List>> print(m.toFmtString()); // NumPy-style string // --- Cleanup (optional; GC handles it automatically) --- zeros.dispose(); } ``` -------------------------------- ### Basic Usage: Import and Get OpenCV Version (Dart) Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates how to import all modules from dartcv4 and retrieve the OpenCV version. Requires Dart SDK >= 3.10. ```dart // Single import brings in all modules import 'package:dartcv4/dartcv.dart' as cv; void main() { print(cv.openCvVersion()); // e.g. "4.13.0" } ``` -------------------------------- ### Set Flutter Library and Headers Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/flutter/CMakeLists.txt Defines the Flutter library path and lists header files. These are published to the parent scope for use in the installation step. ```cmake set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) set(AOT_LIBRARY "${PROJECT_DIR}/build/windows/app.so" PARENT_SCOPE) list(APPEND FLUTTER_LIBRARY_HEADERS "flutter_export.h" "flutter_windows.h" "flutter_messenger.h" "flutter_plugin_registrar.h" "flutter_texture_registrar.h" ) list(TRANSFORM FLUTTER_LIBRARY_HEADERS PREPEND "${EPHEMERAL_DIR}/") ``` -------------------------------- ### Feature Detection and Matching with ORB, SIFT, AKAZE in Dart Source: https://context7.com/rainyl/opencv_dart/llms.txt Illustrates feature detection and matching using various algorithms like ORB, SIFT, and AKAZE. It shows how to create detectors, compute keypoints and descriptors, and match features using BFMatcher and knnMatch. Requires image files 'img1.jpg' and 'img2.jpg'. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { final img1 = cv.imread('img1.jpg', flags: cv.IMREAD_GRAYSCALE); final img2 = cv.imread('img2.jpg', flags: cv.IMREAD_GRAYSCALE); // --- ORB --- final orb = cv.ORB.create(); final (kp1, desc1) = orb.detectAndCompute(img1, cv.Mat.empty()); final (kp2, desc2) = orb.detectAndCompute(img2, cv.Mat.empty()); print('ORB keypoints: ${kp1.length}, ${kp2.length}'); // --- Brute-Force Matching --- final bf = cv.BFMatcher.create(normType: cv.NORM_HAMMING); final matches = bf.match(desc1, desc2); // Sort by distance final sorted = matches.toList()..sort((a, b) => a.distance.compareTo(b.distance)); final good = sorted.take(50).toList(); print('Best match distance: ${good.first.distance}'); // --- kNN Matching + Lowe ratio test --- final knnMatches = bf.knnMatch(desc1, desc2, 2); final ratioGood = knnMatches.toList() .where((m) => m.length == 2 && m[0].distance < 0.75 * m[1].distance) .map((m) => m[0]) .toList(); print('Good matches after ratio test: ${ratioGood.length}'); // Draw matches final matchImg = cv.Mat.empty(); cv.drawMatches(img1, kp1, img2, kp2, ratioGood.cvd, matchImg); cv.imwrite('matches.jpg', matchImg); // --- SIFT --- final sift = cv.SIFT.create(); final (siftKp, siftDesc) = sift.detectAndCompute(img1, cv.Mat.empty()); print('SIFT keypoints: ${siftKp.length}'); // --- AKAZE --- final akaze = cv.AKAZE.create(); final (akazeKp, _) = akaze.detectAndCompute(img1, cv.Mat.empty()); orb.dispose(); sift.dispose(); } ``` -------------------------------- ### Display Windows and User Interaction with OpenCV Dart Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates creating a window, displaying an image, handling key presses, and managing window properties like position and size. Ensure the image file exists and is accessible. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { final img = cv.imread('photo.jpg'); // Create window and show image cv.namedWindow('Preview', cv.WINDOW_AUTOSIZE); cv.imshow('Preview', img); // Wait indefinitely or for a keypress final key = cv.waitKey(0); // 0 = indefinite; >0 = milliseconds print('Key pressed: $key'); // Window position and size cv.moveWindow('Preview', 100, 100); cv.resizeWindow('Preview', 800, 600); final rect = cv.getWindowImageRect('Preview'); print('Image rect: $rect'); cv.destroyWindow('Preview'); cv.destroyAllWindows(); } ``` -------------------------------- ### Find System Dependencies with PkgConfig Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/flutter/CMakeLists.txt Uses PkgConfig to find and check for required system libraries like GTK, GLIB, and GIO, making their include paths and link flags available for the build. ```cmake find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) ``` -------------------------------- ### System Dependencies: PkgConfig and GTK Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Finds and checks for the PkgConfig tool and the GTK 3.0 library, making GTK's imported target available for linking. ```cmake # System-level dependencies. find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) ``` -------------------------------- ### Link Dependencies and Include Directories Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/runner/CMakeLists.txt Links necessary libraries (flutter, flutter_wrapper_app, dwmapi.lib) and adds the project's source directory to the include paths. Add any other application-specific dependencies here. ```cmake target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app) ``` ```cmake target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib") ``` ```cmake target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Apply Standard Build Settings Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/runner/CMakeLists.txt Applies a predefined set of standard build settings to the specified target. This can be customized or removed if the application requires unique build configurations. ```cmake apply_standard_settings(${BINARY_NAME}) ``` -------------------------------- ### Apply Standard Build Settings Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/windows/runner/CMakeLists.txt Applies a predefined set of standard build settings to the application target. This can be removed if custom build settings are required. ```cmake # Apply the standard set of build settings. This can be removed for applications # that need different build settings. apply_standard_settings(${BINARY_NAME}) ``` -------------------------------- ### Link Libraries and Include Directories Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/windows/runner/CMakeLists.txt Specifies the libraries and include directories required for the application. Add any application-specific dependencies here. ```cmake # Add dependency libraries and include directories. Add any application-specific # dependencies here. target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app) target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib") target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Runtime Path Configuration Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Configures the runtime search path for libraries, ensuring bundled libraries in the 'lib/' directory relative to the binary can be found at runtime. ```cmake # Load bundled libraries from the lib/ directory relative to the binary. set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") ``` -------------------------------- ### Custom Command for Flutter Tool Backend Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/flutter/CMakeLists.txt Sets up a custom command to execute the Flutter tool backend script. This command is designed to run every time by using a dummy output file '_phony_' to ensure the Flutter library and headers are generated. ```cmake add_custom_command( OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/_phony_ COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" ${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE} VERBATIM ) ``` -------------------------------- ### Configuring Runtime Library Path Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Sets the RPATH to include the 'lib' directory relative to the binary. This ensures that bundled libraries are found at runtime. ```cmake set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") ``` -------------------------------- ### Cross-Building System Root Configuration Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Configures the system root and search paths for cross-building. This is used when compiling for a different architecture or environment. ```cmake if(FLUTTER_TARGET_PLATFORM_SYSROOT) set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT}) set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) endif() ``` -------------------------------- ### Finding PkgConfig and GTK Dependencies Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Finds the PkgConfig module and checks for the GTK+ 3.0 library. These are system-level dependencies required for the application's UI. ```cmake find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) ``` -------------------------------- ### Read and Write Images with OpenCV Dart Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates synchronous and asynchronous methods for reading images from disk, writing images to disk with specified parameters (like JPEG quality), encoding images to memory buffers, and decoding images from memory buffers. Ensure image files exist at the specified paths. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() async { // --- Synchronous --- final img = cv.imread('photo.jpg', flags: cv.IMREAD_COLOR); if (img.isEmpty) throw Exception('Could not load image'); print('${img.rows} × ${img.cols} channels=${img.channels}'); // Write with JPEG quality final params = [cv.IMWRITE_JPEG_QUALITY, 95].i32; final ok = cv.imwrite('output.jpg', img, params: params); print('Saved: $ok'); // Encode to memory buffer (no disk I/O) final (success, buffer) = cv.imencode('.png', img); print('PNG bytes: ${buffer.length}'); // Decode from memory buffer final decoded = cv.imdecode(buffer, cv.IMREAD_COLOR); // Count pages in multi-page TIFF final pages = cv.imcount('multipage.tiff'); print('Pages: $pages'); // --- Asynchronous versions --- final imgAsync = await cv.imreadAsync('photo.jpg', flags: cv.IMREAD_GRAYSCALE); final okAsync = await cv.imwriteAsync('gray.png', imgAsync); final (s, buf) = await cv.imencodeAsync('.jpg', imgAsync); final dec = await cv.imdecodeAsync(buf, cv.IMREAD_COLOR); imgAsync.dispose(); } ``` -------------------------------- ### Read, Convert, and Write Image in Dart Source: https://github.com/rainyl/opencv_dart/blob/main/README.md Demonstrates basic image manipulation in pure Dart using OpenCV bindings. Requires the 'dartcv4' package and an image file at 'test/images/lenna.png'. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { final img = cv.imread("test/images/lenna.png", flags: cv.IMREAD_COLOR); final gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY); print("${img.rows}, ${img.cols}"); cv.imwrite("test_cvtcolor.png", gray); } ``` -------------------------------- ### Define Executable Target and Source Files Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/windows/runner/CMakeLists.txt Defines the main executable for the Windows runner and lists all necessary source files. Ensure BINARY_NAME is consistent with the top-level CMakeLists.txt for `flutter run` to function correctly. ```cmake cmake_minimum_required(VERSION 3.14) project(runner LANGUAGES CXX) # Define the application target. To change its name, change BINARY_NAME in the # top-level CMakeLists.txt, not the value here, or `flutter run` will no longer # work. # # Any new source files that you add to the application should be added here. add_executable(${BINARY_NAME} WIN32 "flutter_window.cpp" "main.cpp" "utils.cpp" "win32_window.cpp" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" "Runner.rc" "runner.exe.manifest" ) ``` -------------------------------- ### Configure Flutter Interface Library Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/flutter/CMakeLists.txt Creates an INTERFACE library for Flutter, specifying include directories and linking against the Flutter library. It also adds a dependency on flutter_assemble. ```cmake add_library(flutter INTERFACE) target_include_directories(flutter INTERFACE "${EPHEMERAL_DIR}" ) target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}.lib") add_dependencies(flutter flutter_assemble) ``` -------------------------------- ### Project and Executable Naming Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Sets the minimum CMake version, project name, executable name, and application identifier. Change BINARY_NAME to alter the executable's on-disk name. ```cmake cmake_minimum_required(VERSION 3.10) project(runner LANGUAGES CXX) # The name of the executable created for the application. Change this to change # the on-disk name of your application. set(BINARY_NAME "opencv_dart_example") # The unique GTK application identifier for this application. See: # https://wiki.gnome.org/HowDoI/ChooseApplicationID set(APPLICATION_ID "dev.rainyl.opencv_dart") ``` -------------------------------- ### Profile Build Mode Settings Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Sets linker and compiler flags for the Profile build mode to match those of the Release build mode. This ensures consistent performance characteristics between Profile and Release builds. ```cmake set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}") set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}") ``` -------------------------------- ### Enabling Modern CMake Behaviors Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Explicitly opts into modern CMake behaviors to avoid warnings with recent CMake versions. This ensures compatibility and adherence to current CMake best practices. ```cmake cmake_policy(SET CMP0063 NEW) ``` -------------------------------- ### Background Subtraction and Optical Flow with DartCV Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates background subtraction using MOG2 and KNN, and calculates sparse optical flow with Lucas-Kanade and dense optical flow with Farneback. Ensure video and image files are accessible. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { // --- Background subtraction --- final mog2 = cv.BackgroundSubtractorMOG2.create( history: 500, varThreshold: 16, detectShadows: true); final knn = cv.BackgroundSubtractorKNN.create( history: 500, dist2Threshold: 400, detectShadows: true); final cap = cv.VideoCapture.fromFile('video.mp4'); while (true) { final (ok, frame) = cap.read(); if (!ok) break; final fgMask = mog2.apply(frame); cv.imshow('FG Mask', fgMask); if (cv.waitKey(30) >= 0) break; } cap.release(); mog2.dispose(); // --- Lucas-Kanade sparse optical flow --- final gray1 = cv.imread('frame1.jpg', flags: cv.IMREAD_GRAYSCALE); final gray2 = cv.imread('frame2.jpg', flags: cv.IMREAD_GRAYSCALE); final corners = cv.goodFeaturesToTrack(gray1, 100, 0.01, 10); final prevPts = cv.Mat.fromVec(corners); final (nextPts, status, err) = cv.calcOpticalFlowPyrLK( gray1, gray2, prevPts, cv.Mat.empty(), winSize: (15, 15), maxLevel: 2, criteria: (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 10, 0.03), ); // --- Farneback dense optical flow --- final flow = cv.calcOpticalFlowFarneback( gray1, gray2, cv.Mat.empty(), 0.5, 3, 15, 3, 5, 1.2, 0, ); print('Flow shape: ${flow.shape}'); } ``` -------------------------------- ### Define Application Target CMakeLists.txt Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/runner/CMakeLists.txt Defines the main executable target for the application. Ensure BINARY_NAME is consistent with the top-level CMakeLists.txt for `flutter run` to function correctly. Add any new source files to this list. ```cmake cmake_minimum_required(VERSION 3.13) project(runner LANGUAGES CXX) # Define the application target. To change its name, change BINARY_NAME in the # top-level CMakeLists.txt, not the value here, or `flutter run` will no longer # work. # # Any new source files that you add to the application should be added here. add_executable(${BINARY_NAME} "main.cc" "my_application.cc" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" ) ``` -------------------------------- ### Computational Photography with DartCV Source: https://context7.com/rainyl/opencv_dart/llms.txt Illustrates HDR merging using MergeMertens, non-local means denoising, and inpainting. Ensure input image files are available. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { // --- HDR with MergeMertens (exposure fusion) --- final exposures = [ cv.imread('exp1.jpg'), cv.imread('exp2.jpg'), cv.imread('exp3.jpg'), ]; final mertens = cv.MergeMertens.create( contrastWeight: 1.0, saturationWeight: 1.0, exposureWeight: 0.0); final hdr = mertens.process(exposures.cvd); // Convert 32F to 8U for saving final hdr8u = cv.Mat.empty(); cv.normalize(hdr, hdr8u, alpha: 0, beta: 255, normType: cv.NORM_MINMAX); hdr8u.convertTo(cv.MatType.CV_8UC3, inplace: true); cv.imwrite('hdr_fused.jpg', hdr8u); // --- Non-local means denoising --- final noisy = cv.imread('noisy.jpg'); final denoised = cv.fastNlMeansDenoisingColored( noisy, h: 10, hColor: 10, templateWindowSize: 7, searchWindowSize: 21); cv.imwrite('denoised.jpg', denoised); // --- Inpainting --- final damaged = cv.imread('damaged.jpg'); final mask = cv.imread('mask.png', flags: cv.IMREAD_GRAYSCALE); final restored = cv.inpaint(damaged, mask, 3.0, cv.INPAINT_TELEA); cv.imwrite('restored.jpg', restored); for (final e in exposures) e.dispose(); mertens.dispose(); } ``` -------------------------------- ### Include Generated Plugin Rules Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Includes the CMake script that defines build rules for generated plugins, allowing them to be compiled and linked into the application. ```cmake # Generated plugin build rules, which manage building the plugins and adding # them to the application. include(flutter/generated_plugins.cmake) ``` -------------------------------- ### Define Static Wrapper Library for Plugins Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/windows/flutter/CMakeLists.txt Creates a static library target for the C++ wrapper used by plugins, linking against the Flutter library and setting visibility. ```cmake list(APPEND CPP_WRAPPER_SOURCES_CORE "core_implementations.cc" "standard_codec.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_CORE PREPEND "${WRAPPER_ROOT}/" ) list(APPEND CPP_WRAPPER_SOURCES_PLUGIN "plugin_registrar.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_PLUGIN PREPEND "${WRAPPER_ROOT}/" ) add_library(flutter_wrapper_plugin STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ) apply_standard_settings(flutter_wrapper_plugin) set_target_properties(flutter_wrapper_plugin PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(flutter_wrapper_plugin PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_libraries(flutter_wrapper_plugin PUBLIC flutter) target_include_directories(flutter_wrapper_plugin PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_plugin flutter_assemble) ``` -------------------------------- ### Flutter Tool Backend Custom Command Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/windows/flutter/CMakeLists.txt Defines a custom command to execute the Flutter tool backend script, generating necessary output files for the build. ```cmake set(PHONY_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/_phony_") set_source_files_properties("${PHONY_OUTPUT}" PROPERTIES SYMBOLIC TRUE) add_custom_command( OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ${PHONY_OUTPUT} COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" ${FLUTTER_TARGET_PLATFORM} $ VERBATIM ) add_custom_target(flutter_assemble DEPENDS "${FLUTTER_LIBRARY}" ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ) ``` -------------------------------- ### Setting Runtime Output Directory Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Configures the runtime output directory for the executable to a specific subdirectory. This is done to prevent users from accidentally running the unbundled copy of the executable. ```cmake set_target_properties(${BINARY_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run") ``` -------------------------------- ### Find Chessboard Corners and Camera Calibration in Dart Source: https://context7.com/rainyl/opencv_dart/llms.txt Demonstrates finding chessboard corners for camera calibration and pose estimation. Requires an image file with a chessboard. The `findChessboardCorners` function detects corners, `cornerSubPix` refines their accuracy, and `drawChessboardCorners` visualizes them. Camera calibration and pose estimation functions like `solvePnP` are also shown. ```dart import 'package:dartcv4/dartcv.dart' as cv; void main() { // --- Find chessboard corners --- final img = cv.imread('chessboard.jpg', flags: cv.IMREAD_GRAYSCALE); final patternSize = (9, 6); // inner corners final (found, corners) = cv.findChessboardCorners(img, patternSize); if (!found) throw Exception('Chessboard not found'); // Refine to sub-pixel accuracy cv.cornerSubPix( img, corners, (11, 11), (-1, -1), (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001), ); // Draw corners final imgColor = cv.cvtColor(img, cv.COLOR_GRAY2BGR); cv.drawChessboardCorners(imgColor, patternSize, corners, found); // --- Camera calibration --- // (Usually done with many images; here shown with one for illustration) final objPoints = []; final imgPoints = []; // populate objPoints with 3D world coords and imgPoints with 2D image coords... // --- Pose estimation with known intrinsics --- final cameraMatrix = cv.Mat.fromList(3, 3, cv.MatType.CV_64FC1, [800, 0, 320, 0, 800, 240, 0, 0, 1]); final distCoeffs = cv.Mat.zeros(1, 5, cv.MatType.CV_64FC1); final objectPts = cv.VecPoint3f.fromList([ cv.Point3f(0,0,0), cv.Point3f(1,0,0), cv.Point3f(1,1,0), cv.Point3f(0,1,0), ]); final imagePts = cv.VecPoint2f.fromList([ cv.Point2f(100,200), cv.Point2f(300,200), cv.Point2f(300,400), cv.Point2f(100,400), ]); final (ok, rvec, tvec) = cv.solvePnP( objectPts, imagePts, cameraMatrix, distCoeffs); print('PnP solved: $ok, rvec shape=${rvec.shape}'); // Project 3D points final (projected, _) = cv.projectPoints( objectPts, rvec, tvec, cameraMatrix, distCoeffs); print('Projected ${projected.length} points'); // --- Stereo rectification --- // cv.stereoCalibrate, cv.stereoRectify, cv.computeDisparity, etc. } ``` -------------------------------- ### Link Dependency Libraries CMakeLists.txt Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/runner/CMakeLists.txt Links necessary dependency libraries to the application target. Add any additional application-specific dependencies here. ```cmake # Add dependency libraries. Add any application-specific dependencies here. target_link_libraries(${BINARY_NAME} PRIVATE flutter) target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) ``` -------------------------------- ### Add Preprocessor Definitions CMakeLists.txt Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/runner/CMakeLists.txt Adds preprocessor definitions for the application ID. This is useful for embedding application-specific information into the build. ```cmake # Add preprocessor definitions for the application ID. add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") ``` -------------------------------- ### Application Target Definition Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Defines the main executable target for the application, listing its source files. Any new source files should be added here. ```cmake # Define the application target. To change its name, change BINARY_NAME above, # not the value here, or `flutter run` will no longer work. # # Any new source files that you add to the application should be added here. add_executable(${BINARY_NAME} "main.cc" "my_application.cc" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" ) ``` -------------------------------- ### Setting Executable and Application IDs Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/CMakeLists.txt Defines the on-disk name of the application executable and its unique GTK application identifier. These are crucial for application registration and identification on Linux. ```cmake set(BINARY_NAME "dartcv_example") set(APPLICATION_ID "dev.rainyl.dartcv_example") ``` -------------------------------- ### Flutter Integration Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/CMakeLists.txt Includes the Flutter managed directory and the runner subdirectory to integrate Flutter's build system. This is essential for building Flutter applications. ```cmake set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") add_subdirectory(${FLUTTER_MANAGED_DIR}) ``` ```cmake add_subdirectory("runner") ``` -------------------------------- ### Define List Prepend Function for CMake < 3.10 Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/flutter/CMakeLists.txt Defines a custom function 'list_prepend' to add a prefix to each element of a list, as the 'list(TRANSFORM ... PREPEND ...)' command is not available in CMake version 3.10. ```cmake function(list_prepend LIST_NAME PREFIX) set(NEW_LIST "") foreach(element ${${LIST_NAME}}) list(APPEND NEW_LIST "${PREFIX}${element}") endforeach(element) set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) endfunction() ``` -------------------------------- ### Set Include Directories CMakeLists.txt Source: https://github.com/rainyl/opencv_dart/blob/main/packages/dartcv/example/flutter/linux/runner/CMakeLists.txt Sets the include directories for the application target. This allows the compiler to find header files in the specified directory. ```cmake target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Configure OpenCV Modules in pubspec.yaml (Pure Dart) Source: https://context7.com/rainyl/opencv_dart/llms.txt Specify which OpenCV modules to include during compilation for pure Dart projects. This helps manage binary size. ```yaml # pubspec.yaml (pure Dart) dependencies: dartcv4: ^2.2.2 # Optional: configure which OpenCV modules to compile in hooks: user_defines: dartcv4: include_modules: - imgcodecs - imgproc - dnn - videoio - objdetect - calib3d - features2d - photo - stitching - aruco # exclude_modules: # - contrib ``` -------------------------------- ### Define Flutter Wrapper Plugin Library Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/windows/flutter/CMakeLists.txt Builds a static library for the Flutter C++ wrapper used by plugins. It includes core and plugin-specific sources, applies standard settings, and links against the Flutter library. ```cmake list(APPEND CPP_WRAPPER_SOURCES_CORE "core_implementations.cc" "standard_codec.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_CORE PREPEND "${WRAPPER_ROOT}/") list(APPEND CPP_WRAPPER_SOURCES_PLUGIN "plugin_registrar.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_PLUGIN PREPEND "${WRAPPER_ROOT}/") add_library(flutter_wrapper_plugin STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ) apply_standard_settings(flutter_wrapper_plugin) set_target_properties(flutter_wrapper_plugin PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(flutter_wrapper_plugin PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_libraries(flutter_wrapper_plugin PUBLIC flutter) target_include_directories(flutter_wrapper_plugin PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_plugin flutter_assemble) ``` -------------------------------- ### Flutter Managed Directory and Subdirectory Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Sets the path to the Flutter managed directory and includes it as a subdirectory, essential for Flutter's build system integration. ```cmake # Flutter library and tool build rules. set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") add_subdirectory(${FLUTTER_MANAGED_DIR}) ``` -------------------------------- ### Application ID Definition Source: https://github.com/rainyl/opencv_dart/blob/main/packages/opencv_dart/example/linux/CMakeLists.txt Adds a preprocessor definition for APPLICATION_ID, making the application identifier available to the C++ code. ```cmake add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") ```