Usage Guide
KML_CONV only provides core operator implementations for filter2D. You need to register these core operators with open-source OpenCV by following the instructions provided in this section, and call the open-source OpenCV filter2d interfaces to perform filtering computation.
Operator Registration
- Obtain the OpenCV source code.
- Decompress the OpenCV installation package.
tar -xvf opencv-4.9.0.tar.gz
- Register the KML_CONV core operators.
- Add the kconv folder to the opencv/3rdparty directory.
cd your_path_to_opencv/opencv-4.9.0/3rdparty mkdir -p kconv
- Copy the header file and required dependencies to the opencv/3rdparty/kconv folder.
- Copy the conv header file.
cp cv.h your_path_to_opencv/opencv-4.9.0/3rdparty/kconv
- Copy the CONV, BLAS, and FFT dependencies.
cp libkconv_ext.so your_path_to_opencv/opencv-4.9.0/3rdparty/Kunpeng cp libkblas.so your_path_to_opencv/opencv-4.9.0/3rdparty/Kunpeng cp libkfftf.so your_path_to_opencv/opencv-4.9.0/3rdparty/Kunpeng cp libkffth.so your_path_to_opencv/opencv-4.9.0/3rdparty/Kunpeng
- Copy the conv header file.
- Add header files and source files to the opencv/3rdparty/Kunpeng folder.
- Add the header file kml_cv.hpp.
#ifndef KML_CV_HPP #define KML_CV_HPP #include <opencv2/core/base.hpp> #include "cv.h" #endif
- Add the source file kml_cv.cpp.
#include "kml_cv.hpp"
- Add the header file kml_cv.hpp.
- Add the CMakeLists.txt file to the opencv/3rdparty/kconv folder.
project(kml_conv_ext CXX) add_library(kml_conv_ext STATIC kml_cv.cpp) target_link_libraries(kml_conv_ext ${CMAKE_CURRENT_SOURCE_DIR}/libkconv.so ${CMAKE_CURRENT_SOURCE_DIR}/libkblas.so ${CMAKE_CURRENT_SOURCE_DIR}/libkfftf.so ${CMAKE_CURRENT_SOURCE_DIR}/libkffth.so) set(KML_HAL_VERSION "0.0.1" PARENT_SCOPE) set(KML_HAL_LIBRARIES "kml_conv_ext" PARENT_SCOPE) set(KML_HAL_HEADERS "kml_conv_ext.hpp" PARENT_SCOPE) set(KML_HAL_INCLUDE_DIRS "${CMAKE_BINARY_DIR}" PARENT_SCOPE) set_target_properties(kml_conv_ext PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(kml_conv_ext PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}) if(NOT BUILD_SHARED_LIBS) ocv_install_target(kail_dnn_ext EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev) endif() set(OPENCV_SRC_DIR "${CMAKE_SOURCE_DIR}") target_include_directories(kml_conv_ext PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${OPENCV_SRC_DIR}/modules/core/include) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/kml_cv.hpp" "${CMAKE_BINARY_DIR}/kml_cv.hpp" COPYONLY) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cv.h" "${CMAKE_BINARY_DIR}/cv.h" COPYONLY) set_target_properties(${KML_LIBRARIES} PROPERTIES OUTPUT_NAME ${KML_LIBRARIES} DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" COMPILE_PDB_NAME ${KML_LIBRARIES} COMPILE_PDB_NAME_DEBUG "${KML_LIBRARIES}${OPENCV_DEBUG_POSTFIX}" ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH} ) - Register the high-performance KML_CONV filter2D core operators.Modify lines 965 to 995 in opencv-4.9.0/CMakeLists.txt.
set(OpenCV_HAL "kml_conv_ext") foreach(hal ${OpenCV_HAL}) if(hal STREQUAL "carotene") ... elseif(hal STREQUAL "openvx") add_subdirectory(3rdparty/openvx) ocv_hal_register(OPENVX_HAL_LIBRARIES OPENVX_HAL_HEADERS OPENVX_HAL_INCLUDE_DIRS) list(APPEND OpenCV_USED_HAL "openvx (ver ${OPENVX_HAL_VERSION})") elseif(hal STREQUAL "kml_conv_ext") add_subdirectory(3rdparty/kconv) ocv_hal_register(KML_HAL_LIBRARIES KML_HEADERS KML_INCLUDE_DIRS) list(APPEND OpenCV_USED_HAL "kml_conv_ext (ver ${KML_VERSION})") else() ocv_debug_message(STATUS "OpenCV HAL: ${hal} ...") ocv_clear_vars(OpenCV_HAL_LIBRARIES OpenCV_HAL_HEADERS OpenCV_HAL_INCLUDE_DIRS) find_package(${hal} NO_MODULE QUIET) if(${hal}_FOUND) ocv_hal_register(OpenCV_HAL_LIBRARIES OpenCV_HAL_HEADERS OpenCV_HAL_INCLUDE_DIRS) list(APPEND OpenCV_USED_HAL "${hal} (ver ${${hal}_VERSION})") endif() endif()
- Add the kconv folder to the opencv/3rdparty directory.
- Build and install OpenCV.
cd your_path_to_opencv/opencv-4.9.0 mkdir -p build cd build cmake .. -DWITH_ADE=OFF -DCMAKE_INSTALL_PREFIX=your_path_to_install_opencv # Run CMake in the build folder. make -j make install # Install OpenCV to your_path_to_install_opencv.
Definition of the OpenCV filter2D Interface
Once the KML_CONV filter2D operators are registered, you can directly call the OpenCV filter2D interfaces to perform filtering computation.
OpenCV filter2D usage guide:
https://docs.opencv.org/4.9.0/d4/d86/group__imgproc__filter.html
C++ interface:
void cv::filter2D( InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor = Point(-1,-1), double delta = 0, int borderType = BORDER_DEFAULT)
Parameters
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
src |
InputArray |
Input image. |
Input |
dst |
OutputArray |
Output image. |
Output |
int |
ddepth |
Depth of the output image. |
Input |
kernel |
InputArray |
Convolution kernel. |
Input |
anchor |
Point |
Convolution kernel anchor, indicating the relative position of the anchor point in the kernel. The default value is Point(-1, -1), indicating that the anchor point is located at the center of the kernel. |
Input |
delta |
0 |
Offset value. The default value is 0. |
Input |
int |
borderType |
Border padding type. The following input parameters are supported: BORDER_CONSTANT BORDER_REPLICATE BORDER_REFLECT BORDER_REFLECT_101 BORDER_REFLECT101 BORDER_DEFAULT (default) BORDER_ISOLATED |
Input |
Once the KML_CONV operators are registered, OpenCV supports filter2D computation with CV_16F image depth. The following table lists the combinations of input and output image depths supported by the OpenCV filter2D interface.
Input Image Depth |
Output Image Depth |
|---|---|
CV_8U |
-1/CV_16S/CV_32F/CV_64F (If the output image depth is -1, the output image depth is the same as the input image depth.) |
CV_16U/CV_16S |
-1/CV_32F/CV_64F |
CV_32F |
-1/CV_32F |
CV_64F |
-1/CV_64F |
CV_16F |
-1/CV_16F |
If both input and output image depths are CV_32F or CV_16F, the high-performance KML_CONV operators are called to perform filtering computation. In other cases, the OpenCV operators are called to perform filtering computation.
Dependencies
#include <opencv2/imgproc/imgproc.hpp>
Examples
C++ interface:
cv::Mat src = (cv::Mat_<float>(6, 6) << 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36);
cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
cv::Mat dst;
cv::filter2D(src, dst, -1, kernel, cv::Point(-1, -1), 0, cv::BORDER_CONSTANT);
/*
* dst = [-4, -2, 0, 2, 4, 13;
* 13, 8, 9, 10, 11, 25;
* 25, 14, 15, 16, 17, 37;
* 37, 20, 21, 22, 23, 49;
* 49, 26, 27, 28, 29, 61;
* 98, 70, 72, 74, 76, 115]
*/