User Tools

Site Tools


guides:opencv

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:opencv [2024/01/30 02:11] – created discordguides:opencv [2024/02/02 04:22] (current) discord
Line 1: Line 1:
-Instructions for integrating OpenCV with Folk.+//Original Author: Jacob Haip. 
 +Contributions welcome!// 
 + 
 +Instructions for integrating OpenCV with Folk. This guide involves a manual compilation set outside of folk, but the creation of the wrapper files and the compilation (via an TCL "exec" call) could also be moved within a folk program to completely encapsulate the code.
  
 1. Install OpenCV 1. Install OpenCV
Line 5: Line 8:
    sudo apt-get install -y libopencv-dev    sudo apt-get install -y libopencv-dev
  
-2. Create a C wrapper for the OpenCV functions you want to use+2. Create a C wrapper for the OpenCV functions you want to use. I saved these files in a new ~/folk/play/opencv folder.
  
 OpenCVWrapper.h OpenCVWrapper.h
Line 96: Line 99:
 } }
 </code> </code>
 +
 +3. Compile the wrapper
 +
 +   g++ -fPIC -shared OpenCVWrapper.cpp -o libopencvwrapper.so `pkg-config --cflags --libs opencv4`
 +   
 +4. Use the compiled libopencvwrapper.so and .h file in your folk C code
 +
 +<code tcl>
 +set cc [c create]
 +defineImageType $cc
 +$cc cflags -I$::env(HOME)/folk/play/opencv
 +
 +$cc include <stdio.h>
 +$cc include "OpenCVWrapper.h"
 +
 +if {[namespace exists ::Heap]} {
 +    $cc import ::Heap::cc folkHeapAlloc as folkHeapAlloc
 +    $cc import ::Heap::cc folkHeapFree as folkHeapFree
 +} else {
 +    $cc code { #define folkHeapAlloc malloc }
 +    $cc code { #define folkHeapFree free }
 +}
 +
 +$cc proc opencvAdaptiveThreshold {image_t img} image_t {
 +    cv_image_t im2 = (cv_image_t) { .width = img.width, .height = img.height, .components = img.components, .bytesPerRow = img.bytesPerRow, .data = img.data };
 +    cv_image_t r = myAdaptiveThreshold(im2);
 +    image_t ret = (image_t) { .width = r.width, .height = r.height, .components = r.components, .bytesPerRow = r.bytesPerRow, .data = folkHeapAlloc(r.bytesPerRow * r.height) };
 +    memcpy(ret.data, r.data, r.bytesPerRow * r.height);
 +    return ret;
 +}
 +
 +c loadlib $::env(HOME)/folk/play/opencv/libopencvwrapper.so
 +$cc compile
 +
 +When $this has region /r/ {
 +    Claim $this' has region [region move $r down 110%]
 +}
 +
 +set ::opencvOldFrames [list]
 +When $this' has camera slice /slice/ & $this has region /r/ {
 +    set grayImg [opencvAdaptiveThreshold $slice]
 +    set center [region centroid $r]
 +    Wish to draw an image with center $center image $grayImg radians 0 scale 2
 +
 +    lappend ::opencvOldFrames $grayImg
 +    if {[llength $::opencvOldFrames] >= 10} {
 +        set ::opencvOldFrames [lassign $::opencvOldFrames oldestFrame]
 +        image freeImage $oldestFrame
 +    }
 +}
 +</code>
 +
 +{{:guides:opencv-threshold-example.jpeg?400|}}
guides/opencv.1706580716.txt.gz · Last modified: 2024/01/30 02:11 by discord

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki