Class MatWrapper


  • public final class MatWrapper
    extends java.lang.Object
    Wraps a GPU mat and a CPU mat and allows device memory and host memory to be used semi-transparently. A wrapper may change between wrapping an image in host memory or an image in GPU memory. A wrapper is used to minimize copies between host and device memory, which may take longer than the time savings of using a CUDA-accelerated operation.

    Data is lazily copied between host and device memory when needed. Wrappers that are only accessed from CPU operations will never have their data stored in device memory. Accessing a wrapper from CUDA land with getGpu() will first copy the existing data from host memory to device, if the wrapper was most recently accessed from CPU code (from either getCpu() or rawCpu()). This behavior also applies in the reverse, for wrappers accessed from CPU land when they have been most recently used from CUDA code.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int channels()
      Gets the number of color channels in this image.
      int cols()
      Gets the number of columns in this image.
      void copyTo​(MatWrapper wrapper)
      Copies the data of this wrapper into another.
      void copyTo​(org.bytedeco.javacpp.opencv_core.GpuMat mat)
      Copies the data of this wrapper to a mat in GPU memory.
      void copyTo​(org.bytedeco.javacpp.opencv_core.Mat mat)
      Copies the data of this wrapper to a mat in host memory.
      void create​(int rows, int cols, int type)
      Allocates new array data if needed.
      int depth()
      Gets the channel depth of this image.
      boolean empty()
      Checks if this image is empty.
      static MatWrapper emptyWrapper()
      Creates an empty wrapper.
      org.bytedeco.javacpp.opencv_core.Mat getCpu()
      Gets this mat as a mat in host memory.
      org.bytedeco.javacpp.opencv_core.GpuMat getGpu()
      Gets this mat as a mat in GPU memory.
      double highValue()
      Gets the maximum possible value able to be held as a single element in this image.
      boolean isCpu()
      Checks if this is a wrapper around a CPU mat.
      boolean isGpu()
      Checks if this is a wrapper around a GPU mat.
      MatWrapper put​(org.bytedeco.javacpp.opencv_core.Scalar value)
      Sets all or some of the array elements to the specified value.
      org.bytedeco.javacpp.opencv_core.Mat rawCpu()
      Gets the raw CPU mat.
      org.bytedeco.javacpp.opencv_core.GpuMat rawGpu()
      Gets the raw GPU mat.
      int rows()
      Gets the number of rows in this image.
      void set​(MatWrapper wrapper)
      Sets this as being backed by the given wrapper.
      void set​(org.bytedeco.javacpp.opencv_core.GpuMat mat)
      Sets this as being backed by an image in GPU memory.
      void set​(org.bytedeco.javacpp.opencv_core.Mat mat)
      Sets this as being backed by an image in host memory.
      org.bytedeco.javacpp.opencv_core.Size size()
      Gets the size (width by height) of this image.
      int type()
      Gets the type of the data format of this image.
      static MatWrapper using​(org.bytedeco.javacpp.opencv_core.Mat cpuMat, org.bytedeco.javacpp.opencv_core.GpuMat gpuMat)
      Creates a wrapper around existing mats in host and device memory.
      static MatWrapper wrap​(org.bytedeco.javacpp.opencv_core.GpuMat gpuMat)
      Creates a wrapper around an existing mat in GPU memory.
      static MatWrapper wrap​(org.bytedeco.javacpp.opencv_core.Mat cpuMat)
      Creates a wrapper around an existing mat in host memory.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • emptyWrapper

        public static MatWrapper emptyWrapper()
        Creates an empty wrapper. Both mats are empty and the wrapper is treated as a CPU mat.
      • wrap

        public static MatWrapper wrap​(org.bytedeco.javacpp.opencv_core.Mat cpuMat)
        Creates a wrapper around an existing mat in host memory.
      • wrap

        public static MatWrapper wrap​(org.bytedeco.javacpp.opencv_core.GpuMat gpuMat)
        Creates a wrapper around an existing mat in GPU memory.
      • using

        public static MatWrapper using​(org.bytedeco.javacpp.opencv_core.Mat cpuMat,
                                       org.bytedeco.javacpp.opencv_core.GpuMat gpuMat)
        Creates a wrapper around existing mats in host and device memory.
        Parameters:
        cpuMat - the mat in host memory to use
        gpuMat - the mat in device memory to use
      • isCpu

        public boolean isCpu()
        Checks if this is a wrapper around a CPU mat.
      • isGpu

        public boolean isGpu()
        Checks if this is a wrapper around a GPU mat.
      • rawCpu

        public org.bytedeco.javacpp.opencv_core.Mat rawCpu()
        Gets the raw CPU mat. This should only be used when this mat is used as a dst parameter to an OpenCV function. If you want to get the current value as a mat in host memory, use getCpu().
      • rawGpu

        public org.bytedeco.javacpp.opencv_core.GpuMat rawGpu()
        Gets the raw GPU mat. This should only be used when this mat is used as a dst parameter to an OpenCV function. If you want to get the current value as a mat in GPU memory, use getGpu().
      • getCpu

        public org.bytedeco.javacpp.opencv_core.Mat getCpu()
        Gets this mat as a mat in host memory. If this is backed by GPU memory, the device memory will be copied into the CPU mat before being returned. This copy only happens after set(GpuMat) is called, and only once between successive calls; invocations of this method after the first copy will not perform another.
      • getGpu

        public org.bytedeco.javacpp.opencv_core.GpuMat getGpu()
        Gets this mat as a mat in GPU memory. If this is backed by host memory, the host memory will be copied into the GPU mat before being returned. This copy only happens after set(Mat) is called, and only once between successive calls; invocations of this method after the first copy will not perform another.
      • set

        public void set​(org.bytedeco.javacpp.opencv_core.Mat mat)
        Sets this as being backed by an image in host memory. The data in the given mat will be copied into the internal CPU mat, but not the GPU mat until getGpu() is called. This avoids unnecessary memory copies between GPU and host memory.
      • set

        public void set​(org.bytedeco.javacpp.opencv_core.GpuMat mat)
        Sets this as being backed by an image in GPU memory. The data in the given mat will be copied into the internal GPU mat, but not the mat residing in host memory until getCpu() is called. This avoids unnecessary memory copies between GPU and host memory.
      • set

        public void set​(MatWrapper wrapper)
        Sets this as being backed by the given wrapper. This wrapper will be functionally equivalent to the one given.
      • copyTo

        public void copyTo​(org.bytedeco.javacpp.opencv_core.Mat mat)
        Copies the data of this wrapper to a mat in host memory.
      • copyTo

        public void copyTo​(org.bytedeco.javacpp.opencv_core.GpuMat mat)
        Copies the data of this wrapper to a mat in GPU memory.
      • copyTo

        public void copyTo​(MatWrapper wrapper)
        Copies the data of this wrapper into another. Equivalent to wrapper.set(this)
      • cols

        public int cols()
        Gets the number of columns in this image.
      • rows

        public int rows()
        Gets the number of rows in this image.
      • type

        public int type()
        Gets the type of the data format of this image.
      • channels

        public int channels()
        Gets the number of color channels in this image.
      • depth

        public int depth()
        Gets the channel depth of this image.
      • empty

        public boolean empty()
        Checks if this image is empty.
      • size

        public org.bytedeco.javacpp.opencv_core.Size size()
        Gets the size (width by height) of this image.
      • highValue

        public double highValue()
        Gets the maximum possible value able to be held as a single element in this image.
      • create

        public void create​(int rows,
                           int cols,
                           int type)
        Allocates new array data if needed.
        Parameters:
        rows - New number of rows.
        cols - New number of columns.
        type - New matrix type.
      • put

        public MatWrapper put​(org.bytedeco.javacpp.opencv_core.Scalar value)
        Sets all or some of the array elements to the specified value.
        Parameters:
        value - Assigned scalar converted to the actual array type.