moon run src/demo/object_counting
moon run src/demo/edge_detection
moon run src/demo/document_enhancement
At the time of writing, the repository has been verified with moon 0.1.20260713.
Basic Usage
let rgb = try! @image.rgb(4, 4)
let gray = try! @ops.grayscale(rgb)
let binary = try! @ops.otsu_threshold(gray)
let adaptive = try! @ops.adaptive_threshold_mean(gray, 5, 7)
let denoised = try! @filter.median_blur(gray, radius=1)
let blurred = try! @filter.gaussian_blur(gray, radius=1)
let resized = try! @image.resize_nearest(gray, 8, 8)
let smooth_resized = try! @image.resize_bilinear(gray, 8, 8)
let rotated = try! @image.rotate90_cw(gray)
let equalized = try! @histogram.equalize_histogram(gray)
let edges = try! @edge.gradient_magnitude(blurred)
let canny = try! @edge.canny_edges(blurred, 48, 96)
let lines = try! @edge.hough_lines(canny, vote_threshold=4, max_lines=8)
let blobs = try! @components.connected_components(binary, min_area=4)
let distances = try! @components.distance_transform_manhattan(binary)
let contours = try! @components.find_contours(binary)
let first_kind = @components.contour_kind(contours[0])
let first_parent = @components.contour_parent_index(contours[0])
let simplified = @components.approx_contour(contours[0], 1.0)
let hull = @components.convex_hull(contours[0])
let rect = @components.min_area_rect(contours[0])
let circularity = @components.contour_circularity(contours[0])
let solidity = @components.contour_solidity(contours[0])
let best_match = try! @match.match_template_sad(gray, gray)
ignore(adaptive)
ignore(denoised)
ignore(resized)
ignore(smooth_resized)
ignore(rotated)
ignore(equalized)
ignore(edges)
ignore(canny)
ignore(lines)
ignore(blobs)
ignore(distances)
ignore(contours)
ignore(first_kind)
ignore(first_parent)
ignore(simplified)
ignore(hull)
ignore(rect)
ignore(circularity)
ignore(solidity)
ignore(best_match)
find_contours returns ordered boundary walks for binary foreground regions, along with area, perimeter, bounding-box, outer/hole kind, and optional parent-contour metadata.
In v1.3, contours are returned per traced boundary instead of per connected foreground region.
In v1.4, the same contour objects feed shape-analysis helpers for approximation, convex hulls, rotated rectangles, and lightweight descriptors.
Export a grayscale image as PNG bytes:
let png = @export.encode_gray_png(binary)
ignore(png)
Build an SVG overlay for detected components:
let overlay = @export.svg_bounding_boxes(
binary.width(),
binary.height(),
blobs,
)
ignore(overlay)
Decode a PNG file into RgbImage bytes first:
let input = try! @image.rgb_from_png_bytes(png_bytes)
ignore(input)
The v1.5 review report includes the default F1-oriented result, a recall-oriented parameter set, and a precision-oriented parameter set for each image. The review dashboard also writes direct tuning views: review_focus.csv groups each image into clean-match, under-detected, over-detected, or mixed miss/noise buckets; param_summary.csv ranks probe presets across the selected image set; review_category_summary.csv summarizes where the current detector still needs tuning.
Version Stages
v1.0
Focused on the first complete visual-analysis chain: image containers, thresholding, filtering, Sobel edges, binary morphology, connected components, and export.
v1.1
Focused on robustness improvements: Otsu thresholding, adaptive thresholding, median blur, and gray-image transforms.
v1.2
Focuses on edge and contour analysis: Canny edges, contour extraction, contour statistics, and upgraded edge/counting demos.
v1.3
Refines contour semantics into outer/hole-aware hierarchy output, with deterministic parent-child relationships for nested structures.
v1.4
Extends the contour layer into lightweight shape analysis with contour approximation, convex hull extraction, minimum-area rotated rectangles, and descriptor helpers.
v1.5
Focuses on the labeled bacteria-review path: high-recall probe routes, native executable reuse during batch review, quick sample filtering, and multi-mode reporting by F1, recall, and precision.
v2.0
Expands the lightweight imgproc layer with histograms, histogram equalization, extended morphology, distance transform, Hough line detection, grayscale template matching, and bilinear resize.
v1.0 vs v1.1 vs v1.2 vs v1.3 vs v1.4 vs v1.5 vs v2.0
The bundled demo assets are kept stable so v1.0, v1.1, v1.2, v1.3, and v1.4 remain directly comparable. v1.5 adds a labeled-review workflow for local annotated bacteria images, so its review metrics depend on the dataset selected by the script’s InputRoot parameter. v2.0 expands the reusable algorithm layer while preserving the existing demo commands.
Object counting:
v1.0 used threshold(120) and detected 5 objects on the bundled asset.
v1.1 uses median_blur(radius=1) -> otsu_threshold and also detects 5 objects, while removing the fixed threshold constant from the counting path.
v1.2 keeps the v1.1 binary path and adds ordered contour tracing plus a rendered contour mask for the same binary image.
v1.3 keeps the v1.1 binary path, reports outer contours and hole contours separately, and labels the rendered contour mask as hierarchy-aware output.
v1.4 keeps the v1.3 hierarchy output and adds shape-summary metrics for contour approximation, convex hulls, rotated rectangles, and solidity-style compactness.
Edge detection:
v1.1 exported the Sobel gradient magnitude edge map.
v1.2 keeps that output and adds a binary Canny edge map for direct comparison.
Document enhancement:
v1.0 used a fixed threshold after brightness and contrast adjustment.
v1.1 preserves the v1.0 output in document_enhancement_output_v1_0.png and writes the optimized median_blur -> adaptive_threshold_mean result to document_enhancement_output.png.
v2.0 adds document_enhancement_equalized_v2_0.png for histogram-equalized contrast preview and document_enhancement_resized_v2_0.png for bilinear-resized output.
Bacteria labeled review:
v1.5 keeps all core library APIs unchanged and extends the demo layer with batch review, per-preset scoring, best-alignment overlays, label and size summaries, and mode-level comparison across F1-oriented, recall-oriented, and precision-oriented selections.
v2.0 does not change the bacteria-review semantics; it keeps that workflow focused on reporting and validation.
Use mode_summary.csv for the shortest top-level comparison, review_focus.csv for deciding which images need manual inspection, param_summary.csv for preset-level tuning, summary_readable.csv for per-image details, and each sample’s preset_scores.csv for parameter-level debugging.
Final Verification Checklist
Run the core checks:
moon check -d
moon check --warn-list +73
moon build
moon test
moon fmt --check
moon info
Run the bundled visual demos:
moon run src/demo/object_counting
moon run src/demo/edge_detection
moon run src/demo/document_enhancement
Expected bundled demo reports:
examples/output/object_counting_report.html
examples/output/edge_detection_report.html
examples/output/document_enhancement_report.html
Run the bacteria review workflow against local labeled data:
Use -ForceRerun only when regenerating the full labeled review from the source images.
Testing Scope
Current tests cover:
image container invariants
gray-image geometric transforms
histogram analysis and histogram equalization
PNG decode adaptation to RgbImage
grayscale, global thresholding, Otsu thresholding, and adaptive thresholding
filtering, border handling, and median blur behavior
Sobel and Canny edge behavior
Hough line detection behavior
binary morphology behavior and derived morphology operators
connected components, contour hierarchy, and contour statistics
binary distance transform behavior
contour approximation, convex hulls, rotated rectangles, and shape descriptors
grayscale template matching
SVG/HTML export rendering
PNG signature generation
Notes
The project intentionally focuses on the algorithm layer. It does not provide GUI features, video processing, OpenCV bindings, or machine learning integration.
PNG decode and export are implemented locally from vendored subsets adapted from mizchi/image and mizchi/zlib, because the current upstream registry dependency graph is not compatible with the local MoonBit toolchain used for this repository.
Third-party vendored attribution and Apache-2.0 license notices are documented in THIRD_PARTY_NOTICES.md.
License
MoonVision is released under the MIT license. See LICENSE for the full text.
MoonVision
MoonVision is a MoonBit-native lightweight image processing and basic computer vision library.
The current
v2.0line includes the core lightweight image-processing library:GrayImage,RgbImageRgbImagethrough a vendored adapter layerThe
v1.5bacteria-review workflow extends the demo layer with:src/demo/bacteria_probeModule Layout
Quick Start
Install or update the MoonBit toolchain first:
Install MoonVision in another MoonBit project:
Import the packages you need from your package’s
moon.pkg:Check the project:
Run the bundled demos:
At the time of writing, the repository has been verified with
moon 0.1.20260713.Basic Usage
find_contoursreturns ordered boundary walks for binary foreground regions, along with area, perimeter, bounding-box, outer/hole kind, and optional parent-contour metadata. Inv1.3, contours are returned per traced boundary instead of per connected foreground region. Inv1.4, the same contour objects feed shape-analysis helpers for approximation, convex hulls, rotated rectangles, and lightweight descriptors.Export a grayscale image as PNG bytes:
Build an SVG overlay for detected components:
Decode a PNG file into
RgbImagebytes first:Demos
Demo input assets live in
examples/assets/.All demo outputs are written to
examples/output/.Object counting:
Outputs:
examples/output/object_counting_input.pngexamples/output/object_counting_binary_v1_0.pngexamples/output/object_counting_binary.pngexamples/output/object_counting_contours.pngexamples/output/object_counting_overlay.svgexamples/output/object_counting_report.htmlEdge detection:
Outputs:
examples/output/edge_detection_input.pngexamples/output/edge_detection_edges.pngexamples/output/edge_detection_canny.pngexamples/output/edge_detection_report.htmlDocument enhancement:
Outputs:
examples/output/document_enhancement_input.pngexamples/output/document_enhancement_output_v1_0.pngexamples/output/document_enhancement_equalized_v2_0.pngexamples/output/document_enhancement_output.pngexamples/output/document_enhancement_resized_v2_0.pngexamples/output/document_enhancement_report.htmlBacteria labeled review:
For quick validation during parameter tuning, restrict the run to specific sample folders:
Exclude duplicated
renamefolders when checking the original reviewed images:Or cap the number of reviewed images:
Outputs:
examples/output/bacteria_labeled_review/index.htmlexamples/output/bacteria_labeled_review/summary_readable.csvexamples/output/bacteria_labeled_review/review_focus.csvexamples/output/bacteria_labeled_review/param_summary.csvexamples/output/bacteria_labeled_review/review_category_summary.csvexamples/output/bacteria_labeled_review/mode_summary.csvexamples/output/bacteria_labeled_review/label_summary.csvexamples/output/bacteria_labeled_review/size_summary.csvThe
v1.5review report includes the default F1-oriented result, a recall-oriented parameter set, and a precision-oriented parameter set for each image. The review dashboard also writes direct tuning views:review_focus.csvgroups each image into clean-match, under-detected, over-detected, or mixed miss/noise buckets;param_summary.csvranks probe presets across the selected image set;review_category_summary.csvsummarizes where the current detector still needs tuning.Version Stages
v1.0Focused on the first complete visual-analysis chain: image containers, thresholding, filtering, Sobel edges, binary morphology, connected components, and export.v1.1Focused on robustness improvements: Otsu thresholding, adaptive thresholding, median blur, and gray-image transforms.v1.2Focuses on edge and contour analysis: Canny edges, contour extraction, contour statistics, and upgraded edge/counting demos.v1.3Refines contour semantics into outer/hole-aware hierarchy output, with deterministic parent-child relationships for nested structures.v1.4Extends the contour layer into lightweight shape analysis with contour approximation, convex hull extraction, minimum-area rotated rectangles, and descriptor helpers.v1.5Focuses on the labeled bacteria-review path: high-recall probe routes, native executable reuse during batch review, quick sample filtering, and multi-mode reporting by F1, recall, and precision.v2.0Expands the lightweightimgproclayer with histograms, histogram equalization, extended morphology, distance transform, Hough line detection, grayscale template matching, and bilinear resize.v1.0 vs v1.1 vs v1.2 vs v1.3 vs v1.4 vs v1.5 vs v2.0
The bundled demo assets are kept stable so
v1.0,v1.1,v1.2,v1.3, andv1.4remain directly comparable.v1.5adds a labeled-review workflow for local annotated bacteria images, so its review metrics depend on the dataset selected by the script’sInputRootparameter.v2.0expands the reusable algorithm layer while preserving the existing demo commands.v1.0usedthreshold(120)and detected5objects on the bundled asset.v1.1usesmedian_blur(radius=1) -> otsu_thresholdand also detects5objects, while removing the fixed threshold constant from the counting path.v1.2keeps thev1.1binary path and adds ordered contour tracing plus a rendered contour mask for the same binary image.v1.3keeps thev1.1binary path, reports outer contours and hole contours separately, and labels the rendered contour mask as hierarchy-aware output.v1.4keeps thev1.3hierarchy output and adds shape-summary metrics for contour approximation, convex hulls, rotated rectangles, and solidity-style compactness.v1.1exported the Sobel gradient magnitude edge map.v1.2keeps that output and adds a binaryCannyedge map for direct comparison.v1.0used a fixed threshold after brightness and contrast adjustment.v1.1preserves thev1.0output indocument_enhancement_output_v1_0.pngand writes the optimizedmedian_blur -> adaptive_threshold_meanresult todocument_enhancement_output.png.v2.0addsdocument_enhancement_equalized_v2_0.pngfor histogram-equalized contrast preview anddocument_enhancement_resized_v2_0.pngfor bilinear-resized output.v1.5keeps all core library APIs unchanged and extends the demo layer with batch review, per-preset scoring, best-alignment overlays, label and size summaries, and mode-level comparison across F1-oriented, recall-oriented, and precision-oriented selections.v2.0does not change the bacteria-review semantics; it keeps that workflow focused on reporting and validation.v1.5 Review Validation
Quick script validation without a full batch run:
Original-image-only validation:
Small smoke run:
Full labeled review:
Use
mode_summary.csvfor the shortest top-level comparison,review_focus.csvfor deciding which images need manual inspection,param_summary.csvfor preset-level tuning,summary_readable.csvfor per-image details, and each sample’spreset_scores.csvfor parameter-level debugging.Final Verification Checklist
Run the core checks:
Run the bundled visual demos:
Expected bundled demo reports:
examples/output/object_counting_report.htmlexamples/output/edge_detection_report.htmlexamples/output/document_enhancement_report.htmlRun the bacteria review workflow against local labeled data:
Use
-ForceRerunonly when regenerating the full labeled review from the source images.Testing Scope
Current tests cover:
RgbImageNotes
mizchi/imageandmizchi/zlib, because the current upstream registry dependency graph is not compatible with the local MoonBit toolchain used for this repository.License
MoonVision is released under the
MITlicense. See LICENSE for the full text.