Class MatOps

java.lang.Object
io.github.jspinak.brobot.imageUtils.MatOps

public class MatOps extends Object
Comprehensive utility class for OpenCV Mat operations in Brobot.

MatOps provides an extensive collection of utility methods for working with OpenCV Mat objects (matrix representations of images). These operations are fundamental to Brobot's computer vision capabilities, supporting everything from basic matrix manipulation to complex image analysis. The class bridges JavaCV and OpenCV APIs, provides debugging tools, and implements common patterns used throughout the framework.

Core operation categories:

  • Data Access: Get/put values at specific positions with type safety
  • Debugging: Print matrix contents with intelligent formatting
  • Conversion: Transform between JavaCV/OpenCV formats and color spaces
  • Creation: Factory methods for creating Mats with specific properties
  • Analysis: Min/max values, statistics, and content inspection
  • Manipulation: Channel operations, ROI extraction, aggregation

Debugging features:

  • Partial matrix printing to avoid overwhelming output
  • Consecutive zero compression for sparse matrices
  • Multi-channel support with per-channel display
  • Dimension and type information display
  • Min/max value reporting for quick data validation

Type handling:

  • Automatic indexer selection based on Mat type
  • Support for 8-bit, 32-bit integer, and 32-bit float types
  • Safe conversions between different Mat representations
  • BufferedImage to Mat conversion for Java integration

Common usage patterns:

 // Debug print a portion of a Mat
 MatOps.printPartOfMat(mat, 10, 10, "Match scores");
 
 // Create a Mat with specific values
 Mat mask = MatOps.makeMat(100, 100, CV_8UC1, 255);
 
 // Convert color spaces
 Mat hsv = MatOps.BGRtoHSV(bgrImage);
 
 // Get min/max values
 double[] minMax = MatOps.getMinMaxOfFirstChannel(mat);
 

Channel operations:

  • Extract individual channels from multi-channel Mats
  • Find min/max values across channels
  • Merge single-channel Mats into multi-channel
  • Per-cell operations across multiple Mats

Factory methods:

  • Create Mats with uniform values
  • 3x3 kernel creation for morphological operations
  • Multi-channel Mat creation with per-channel values
  • Size-based and dimension-based constructors

Safety features:

  • Null checks and empty Mat handling
  • Bounds checking for ROI operations
  • Type compatibility verification
  • Optional return types for operations that may fail

Performance considerations:

  • Direct indexer access for efficient pixel operations
  • Minimal copying through careful use of references
  • Batch operations for multiple Mats
  • Lazy evaluation where possible

In the model-based approach, MatOps serves as the foundation for all image processing operations. It provides the low-level tools needed to implement pattern matching, color analysis, motion detection, and other computer vision algorithms that enable Brobot to "see" and understand GUI elements.

Since:
1.0
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Optional<org.bytedeco.opencv.opencv_core.Mat>
    applyIfOk(org.bytedeco.opencv.opencv_core.Mat mat, org.bytedeco.opencv.opencv_core.Rect roi)
     
    static org.bytedeco.opencv.opencv_core.Mat
    BGRtoHSV(org.bytedeco.opencv.opencv_core.Mat bgr)
    Returns a new HSV Mat.
    static Optional<org.bytedeco.opencv.opencv_core.Mat>
     
    static org.bytedeco.opencv.opencv_core.Mat
    convertToJavaCVmat(org.opencv.core.Mat mat)
     
    static org.opencv.core.Mat
    convertToOpenCVmat(org.bytedeco.opencv.opencv_core.Mat mat)
     
    static boolean
    firstChannelContains(org.bytedeco.opencv.opencv_core.Mat mat, int x)
     
    static double
    getDouble(int row, int col, int channel, org.bytedeco.opencv.opencv_core.Mat mat)
    In a multi-dimensional array, such as a 3D matrix or tensor, the indices i,j,k typically represent the indices along the first, second, and third dimensions, respectively.
    static double[]
    getDoubleColumn(int col, org.bytedeco.opencv.opencv_core.Mat mat)
     
    static double[]
    getDoubleRow(int row, org.bytedeco.opencv.opencv_core.Mat mat)
     
    static org.bytedeco.opencv.opencv_core.Mat
    getFirstChannel(org.bytedeco.opencv.opencv_core.Mat mat)
     
    static org.bytedeco.opencv.opencv_core.Mat
    getGrayscale(org.bytedeco.opencv.opencv_core.Mat mat)
     
    static double[]
    getMinMaxOfFirstChannel(org.bytedeco.opencv.opencv_core.Mat mat)
    minMaxLoc requires a single channel array.
    static org.bytedeco.opencv.opencv_core.Mat
    getMinOrMaxPerCellAcrossChannels(org.bytedeco.opencv.opencv_core.Mat mat, int operation)
    Gets the minimum or maximum value in a cell position across channels.
    static org.bytedeco.opencv.opencv_core.Mat
    getNewMatWithPerCellMinsOrMaxes(List<org.bytedeco.opencv.opencv_core.Mat> mats, int operation)
     
    static void
    info(org.bytedeco.opencv.opencv_core.Mat mat, String... strings)
     
    static void
    info(org.bytedeco.opencv.opencv_core.MatVector matVector, String... strings)
     
    static org.bytedeco.opencv.opencv_core.Mat
    Makes a 3x3, 1-channel Mat with random values (0-255).
    static org.bytedeco.opencv.opencv_core.Mat
    make3x3Mat(short[] values)
    Cell values are inserted by row from left to right (the 4th value goes in row 1, column 0).
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(int rows, int cols, int type, double value)
     
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(int rows, int cols, int type, double channel1, double channel2, double channel3)
     
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(short... values)
     
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double value)
     
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double[] channels)
    Makes either a one channel or 3 channel Mat.
    static org.bytedeco.opencv.opencv_core.Mat
    makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double channel1, double channel2, double channel3)
     
    static boolean
    matsDontMatch(int minMats, List<org.bytedeco.opencv.opencv_core.Mat> mats)
     
    static void
    printDimensions(org.bytedeco.opencv.opencv_core.Mat mat)
     
    static void
    printDimensions(org.bytedeco.opencv.opencv_core.Mat mat, String title)
     
    static void
    printDimensions(org.opencv.core.Mat mat)
     
    static void
    printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols)
     
    static void
    printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, int channels)
     
    static void
    printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, int channels, String title)
    Prints part of the matrix, starting from the top left corner.
    static void
    printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, String title)
     
    static void
    putInt(org.bytedeco.opencv.opencv_core.Mat mat, int row, int col, short... values)
     
    static org.bytedeco.opencv.opencv_core.Mat
    toGrayscale(org.bytedeco.opencv.opencv_core.Mat bgr)
    Returns a new grayscale Mat.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MatOps

      public MatOps()
  • Method Details

    • printPartOfMat

      public static void printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, String title)
    • printPartOfMat

      public static void printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols)
    • printPartOfMat

      public static void printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, int channels)
    • putInt

      public static void putInt(org.bytedeco.opencv.opencv_core.Mat mat, int row, int col, short... values)
    • getDouble

      public static double getDouble(int row, int col, int channel, org.bytedeco.opencv.opencv_core.Mat mat)
      In a multi-dimensional array, such as a 3D matrix or tensor, the indices i,j,k typically represent the indices along the first, second, and third dimensions, respectively. The variable i is likely used to refer to the row index.
      Parameters:
      row - i in JavaCV
      col - j in JavaCV
      channel - k in JavaCV
      mat - the mat to analyze
      Returns:
      the value at row, col, channel
    • getDoubleRow

      public static double[] getDoubleRow(int row, org.bytedeco.opencv.opencv_core.Mat mat)
    • getDoubleColumn

      public static double[] getDoubleColumn(int col, org.bytedeco.opencv.opencv_core.Mat mat)
    • printPartOfMat

      public static void printPartOfMat(org.bytedeco.opencv.opencv_core.Mat mat, int rows, int cols, int channels, String title)
      Prints part of the matrix, starting from the top left corner. Consecutive 0 values will be printed as ... The first channel is printed first and its 2d form is maintained.
      Parameters:
      mat - matrix to print
      rows - number of rows to print
      cols - number of columns to print
      channels - number of channels to print
      title - title of the printed matrix
    • convertToOpenCVmat

      public static org.opencv.core.Mat convertToOpenCVmat(org.bytedeco.opencv.opencv_core.Mat mat)
    • convertToJavaCVmat

      public static org.bytedeco.opencv.opencv_core.Mat convertToJavaCVmat(org.opencv.core.Mat mat)
    • printDimensions

      public static void printDimensions(org.opencv.core.Mat mat)
    • printDimensions

      public static void printDimensions(org.bytedeco.opencv.opencv_core.Mat mat)
    • printDimensions

      public static void printDimensions(org.bytedeco.opencv.opencv_core.Mat mat, String title)
    • info

      public static void info(org.bytedeco.opencv.opencv_core.Mat mat, String... strings)
    • info

      public static void info(org.bytedeco.opencv.opencv_core.MatVector matVector, String... strings)
    • getMinMaxOfFirstChannel

      public static double[] getMinMaxOfFirstChannel(org.bytedeco.opencv.opencv_core.Mat mat)
      minMaxLoc requires a single channel array. This method uses the first channel if the Mat is a multi-channel array. miniMaxLoc does not consider 0 as a minimum but this function does.
    • getFirstChannel

      public static org.bytedeco.opencv.opencv_core.Mat getFirstChannel(org.bytedeco.opencv.opencv_core.Mat mat)
    • firstChannelContains

      public static boolean firstChannelContains(org.bytedeco.opencv.opencv_core.Mat mat, int x)
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double value)
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(int rows, int cols, int type, double value)
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double channel1, double channel2, double channel3)
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(org.bytedeco.opencv.opencv_core.Size size, int type, double[] channels)
      Makes either a one channel or 3 channel Mat. If another number of channels is provided, an empty Mat is returned.
      Parameters:
      size - the size of the Mat
      type - the type of the Mat
      channels - the values of the channels
      Returns:
      the Mat
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(int rows, int cols, int type, double channel1, double channel2, double channel3)
    • make3x3Mat

      public static org.bytedeco.opencv.opencv_core.Mat make3x3Mat()
      Makes a 3x3, 1-channel Mat with random values (0-255).
      Returns:
      a 3x3 Mat with random values.
    • make3x3Mat

      public static org.bytedeco.opencv.opencv_core.Mat make3x3Mat(short[] values)
      Cell values are inserted by row from left to right (the 4th value goes in row 1, column 0). If the array of values is less than 9, the value 0 is placed in the remaining cells.
      Parameters:
      values - the cell values for a 3x3 Mat
      Returns:
      the new Mat
    • makeMat

      public static org.bytedeco.opencv.opencv_core.Mat makeMat(short... values)
    • getGrayscale

      public static org.bytedeco.opencv.opencv_core.Mat getGrayscale(org.bytedeco.opencv.opencv_core.Mat mat)
    • matsDontMatch

      public static boolean matsDontMatch(int minMats, List<org.bytedeco.opencv.opencv_core.Mat> mats)
    • getNewMatWithPerCellMinsOrMaxes

      public static org.bytedeco.opencv.opencv_core.Mat getNewMatWithPerCellMinsOrMaxes(List<org.bytedeco.opencv.opencv_core.Mat> mats, int operation)
    • getMinOrMaxPerCellAcrossChannels

      public static org.bytedeco.opencv.opencv_core.Mat getMinOrMaxPerCellAcrossChannels(org.bytedeco.opencv.opencv_core.Mat mat, int operation)
      Gets the minimum or maximum value in a cell position across channels.
      Parameters:
      mat - multi-channel Mat
      operation - minimum or maximum
      Returns:
      a one-channel Mat
    • applyIfOk

      public static Optional<org.bytedeco.opencv.opencv_core.Mat> applyIfOk(org.bytedeco.opencv.opencv_core.Mat mat, org.bytedeco.opencv.opencv_core.Rect roi)
    • bufferedImageToMat

      public static Optional<org.bytedeco.opencv.opencv_core.Mat> bufferedImageToMat(BufferedImage bufferedImage)
    • toGrayscale

      public static org.bytedeco.opencv.opencv_core.Mat toGrayscale(org.bytedeco.opencv.opencv_core.Mat bgr)
      Returns a new grayscale Mat. Doesn't change the original BGR Mat.
      Parameters:
      bgr - the BGR Mat to convert.
      Returns:
      a new grayscale Mat.
    • BGRtoHSV

      public static org.bytedeco.opencv.opencv_core.Mat BGRtoHSV(org.bytedeco.opencv.opencv_core.Mat bgr)
      Returns a new HSV Mat. Doesn't change the original BGR Mat.
      Parameters:
      bgr - the BGR Mat to convert.
      Returns:
      a new HSV Mat.