ImageJS Documentation

website·Indexed 32 days ago

https://docs.image-js.org

A versatile TypeScript library for image processing and analysis. It provides tools for image transformations, morphology (including Canny edge detection and bottom-hat), image segmentation using watershed and threshold operations, and detailed Regions of Interest (ROI) analysis such as solidity, sphericity, and convex hull.

Tokens
25.5K
Snippets
163
Records
227
Agent score
64%

What's inside ImageJS

  1. Overview of ImageJS Operations and Methods

    ImageJS provides a set of operations used to analyze image data or transition between different image data types. For example, the threshold method is used to convert an Image to a Mask, and watershed is used to create a Region of Interest (ROI) map.
  2. Overview of geometric transformation methods in image-js

    Geometric operations in image-js allow for the manipulation of an image's shape, position, and orientation by transforming pixel spatial coordinates. Most of these operations are applicable to Images but not to Masks.
  3. Analyze holes within a Region of Interest using holesInfo()

    The holesInfo() method of the ROI class provides information about the holes within a region of interest. It returns an object containing the total number of holes and their combined surface area measured in pixels. This data is useful for calculating the fill ratio and analyzing the overall form of the ROI.
    const holesInfo = roi.holesInfo();
  4. Calculate Euclidean distance between two images using the hypotenuse filter

    The hypotenuse filter calculates the Euclidean distance between two pixels from two compatible images using the formula: NewValue = sqrt(Value1^2 + Value2^2). This is primarily used for image alignment, feature matching, and identifying corresponding points between images in computer vision applications.
  5. Understand morphological operations in image-js

    Morphological operations in image-js are used to modify image pixels based on a structuring element. A structuring element is a small binary array (shape or pattern) that serves as a template to define the neighborhood of pixels to be considered during the operation. These tools are primarily used when the shapes and structures of objects within an image are the primary focus of the processing task.
  6. Generate a ROI map from a mask

    To localize and store data about regions of interest (ROI) identified in a binary mask, use the fromMask() function. This returns a RoiMapManager (or RoiMap) object.

    Configuration:

    • allowCorners (boolean): Determines if regions connected only by corners are treated as a single region or separate regions. Defaults to false.
    import fromMask from 'image-js';
    
    // Basic usage
    const roiMap = fromMask(mask);
    
    // Usage with allowCorners option
    const roiMapWithCorners = fromMask(mask, { allowCorners: true });
  7. Constraints and errors when using the `out` parameter

    The out parameter will throw an error if the provided image is incompatible with the required dimensions or color model. Additionally, some functions that change the image's fundamental properties (such as convertColor) cannot use the source image as the out target because the color model change is incompatible with the existing object.
    // Example 1: Dimension mismatch
    let image2 = new Image(1, 3, { colorModel: 'RGB' });
    let testImage = new Image(3, 3, { colorModel: 'GREY' });
    
    // Throws: `cannot use out image. Its width property must be 3. Received 1`
    testImage = testImage.invert({ out: image2 });
    
    // Example 2: Color model change incompatibility
    let image1 = testUtils.createGreyImage([[1, 1, 1]]);
    // Throws error because source cannot be target when changing color model
    image1 = image1.convertColor('RGB', { out: image1 });
  8. Apply a gradient filter for edge detection

    The gradientFilter method highlights edges and boundaries by calculating the rate of change in pixel intensities. It is a fundamental tool for edge detection, object recognition, and image segmentation. Because gradient filters are sensitive to noise, it is recommended to smooth the image (e.g., using a Gaussian blur) before application to avoid false edges.

    Compatible Image Properties:

    • bitDepth: 8 or 16
    • components: 1 (grayscale)
    • alpha: Allowed
    import type { Image } from 'image-js';
    
    export default function gradientFilter(image: Image) {
      // Image must be grayscale before applying the filter
      image = image.grey();
      return image.gradientFilter({
        kernelX: [
          [1, 0, 1],
          [0, 0, 0],
          [1, 0, 1],
        ],
      });
    }
  9. Image processing features in ImageJS

    ImageJS provides several modules for processing images:

    • Filtering: Applies kernels to modify the image.
    • Comparison: Compares two images for feature matching.
    • Geometry: Handles rotating and resizing images.
    • Morphology: Used for shape analysis and identification.
    • ROI analysis: Targets and extracts information from specific Regions of Interest.
  10. Access FeretDiameter properties

    The minDiameter and maxDiameter properties of a Feret object are themselves FeretDiameter objects. These objects provide detailed geometric data about the specific diameter measurement:
    // Example of properties available on a FeretDiameter object:
    // feret.minDiameter.length         -> number (length of the diameter)
    // feret.minDiameter.angle          -> number (angle in degrees relative to horizontal)
    // feret.minDiameter.points         -> [Point, Point] (start and end points)
    // feret.minDiameter.calliperLines  -> [[Point, Point], [Point, Point]] (the two parallel tangent lines)
  11. Calculate Feret diameters for a Region of Interest (ROI) or Mask

    Feret diameters (also known as caliper diameters) measure the minimum and maximum distances between two parallel tangents that touch the boundary of a region of interest. This is used for shape and structure analysis.

    In ImageJS, you can obtain these measurements using the feret accessor on an Roi instance or the getFeret() method on a Mask instance. Both return a Feret object containing:

    • minDiameter: The minimum diameter (type FeretDiameter).
    • maxDiameter: The maximum diameter (type FeretDiameter).
    • aspectRatio: The ratio between the maximum and minimum diameter lengths (type number).
    // Using ROI accessor
    const feret = roi.feret;
    
    // Using Mask method
    const feret = mask.getFeret();