Haskell Bindings
Haskell bindings for FlatCV are available on
Hackage as the flatcv package.
Installation
Using cabal:
cabal install flatcv
Using stack:
stack install flatcv
Usage
import FlatCV
import qualified Data.Vector.Storable as V
main :: IO ()
main = do
-- Assuming you have RGBA image data as a Vector Word8
let
width = 640
height = 480
-- imageData :: V.Vector Word8
-- Convert to grayscale
grayData <- grayscale width height imageData
-- Apply Gaussian blur
blurredData <- gaussianBlur width height 2.0 imageData
-- Detect edges
edges <- sobelEdgeDetection width height 4 imageData
-- Extract a document from a photo
(outW, outH, docData) <- extractDocumentAuto width height imageData
The bindings use Data.Vector.Storable for efficient memory handling
with automatic memory management via foreign pointers.
Available Functions
Color Conversion
grayscale- Convert RGBA to grayscale (4-channel output)grayscaleStretch- Grayscale with contrast stretchingrgbaToGrayscale- Convert to single-channel grayscalesingleToMultichannel- Convert grayscale to RGBA
Filtering
gaussianBlur- Apply Gaussian blur
Thresholding
otsuThreshold- Otsu's automatic thresholdingbwSmart- Smart black & white conversionapplyGlobalThreshold- Apply fixed threshold
Edge Detection
sobelEdgeDetection- Sobel edge detectionfoerstnerCorner- Foerstner corner detection
Morphological Operations
binaryDilationDisk- Binary dilationbinaryErosionDisk- Binary erosionbinaryClosingDisk- Binary closingbinaryOpeningDisk- Binary opening
Geometric Transforms
flipX,flipY- Flip horizontally/verticallytranspose,transverse- Transpose operationsrotate90CW,rotate180,rotate270CW- Rotationsresize- Resize with bilinear interpolation
Cropping & Trimming
crop- Crop rectangular regiontrim- Auto-trim borderstrimThreshold- Trim with threshold tolerance
Perspective Transform
calculatePerspectiveTransform- Calculate transform matrixapplyMatrix3x3- Apply 3x3 transformationdetectCorners- Detect document corners
Document Extraction
extractDocument- Extract with fixed output sizeextractDocumentAuto- Extract with automatic sizing
Drawing
drawCircle- Draw circle outlinedrawDisk- Draw filled circleaddBorder- Add border around image
Analysis
generateHistogram- Generate histogram visualization
Segmentation
watershedSegmentation- Watershed segmentation
Utility
getExifOrientation- Read JPEG EXIF orientationparseHexColor- Parse hex color stringconvertToBinary- Convert to binary with custom colors
Pointer-Based API
For zero-copy interop with foreign data, a pointer-based API is also available.
These functions work directly with Ptr Word8 and the caller is responsible
for memory management.
import FlatCV
import Foreign.Ptr (Ptr)
import Foreign.Marshal.Alloc (free)
import Data.Word (Word8)
processImage :: Ptr Word8 -> IO (Ptr Word8)
processImage inputPtr = do
-- Output is allocated by C and must be freed by caller
outputPtr <- grayscalePtr 640 480 inputPtr
-- ... use outputPtr ...
free outputPtr
Available pointer-based functions include:
grayscalePtr, grayscaleStretchPtr, rgbaToGrayscalePtr,
singleToMultichannelPtr, gaussianBlurPtr, otsuThresholdPtr,
bwSmartPtr, flipXPtr, flipYPtr, transposePtr,
rotate90CWPtr, rotate180Ptr, rotate270CWPtr,
calculatePerspectiveTransformPtr, applyMatrix3x3Ptr, detectCornersPtr.
Types
The library provides several data types for working with images:
Image- An image with dimensions and pixel dataPoint2D- A 2D point with x and y coordinatesCorners- Quadrilateral corners (top-left, top-right, bottom-right, bottom-left)Matrix3x3- A 3x3 transformation matrix