<< Datatypes | Datatypes | PtList >>

scicv >> Datatypes > Mat

# Mat

Image type

### Description

`Mat` is the equivalent in Scilab of the OpenCV type of same name Mat.

When a function outputs an image (such as imread for example), internally OpenCV allocates a Mat object, and a Scilab `Mat` object is returned, containing a reference to the underlying OpenCV Mat object.

Therefore a Scilab `Mat` object is not a Scilab matrix (it is a mlist), but it can be manipulated so. That means the following operators are (or will be) available:

- extraction: syntax is the same a the usual Scilab extraction, and returns a Scilab matrix or an hypermatrix, which type depends on the image type, see details below.
- insertion: syntax is the same a the usual Scilab insertion, and accepts a Scilab matrix in input (not yet available).
- concatenation: syntax is the same a the usual Scilab concatenation (not yet available).

To create a `Mat`, use `new_Mat()`, which has several forms:

- `new_Mat()` to create an empty `Mat` object.
- `new_Mat(<rows>, <cols>, <imageType>)` to create a `Mat` object of the given dimensions and image type specified by the constant `<imageType>` following the OpenCV convention `CV_<bit_depth>{U|S|F}C<number of channels>`.
- `new_Mat(<rows>, <cols>, <imageType>, <pixel_value>)` to create a `Mat` object of the given dimensions and image type, in which each pixel value equals `pixel_value`.

The extraction supports the following arguments:

- `(:, :)` returns the whole image as an hypermatrix it the image is a color image, or a matrix if the image is a grayscale or binary image.
- `(r1:rstep:r2, c1:cstep:c2)` returns a portion of the image, as an hypermatrix or a matrix, similarly as above.
- `(:)` returns a column oriented matrix containing the list of the pixel values.

The type returned can be `int8`, `uint8`, `int16`, `uint16`, `int32`, `uint32` or `double` depending on the bit depth and pixel type of the image.

Note: OpenCV stores the RBG channels in the different order BGR, but Scilab rearranges in the standard order.

About functions, there are many available for the `Mat` type, following is a small list:

- `Mat_empty()` to check an image is not empty. It is very useful since imread does not throw errors when it cannot find an image, but returns an empty image.
- `size()` to get the dimensions of an image, using the Scilab format, rows before columns `[<rows>, <cols>]` (note: every size argument, in every function, use this convention).
- `Mat_rows_get()` and `Mat_cols_get()` to get rows and columns of the image.
- `getImageType()` to get a string (following the OpenCV convention `CV_<bit_depth>{U|S|F}C<number of channels>`) giving the bit depth, pixel type, the number of channels of the image.

Note: functions getting as input `Mat` objects accept also Scilab matrixes, conversion to `Mat` is automatically done by Scilab.

When an image is no more used, it must be destroyed with `delete_Mat()`. Scilab does not manage the life cycle of `Mat` objects, they have to be destroyed manually.

.

### Exemples

    scicv_Init();

    img = imread(getSampleImage("lena.jpg"));

    // Get the dimensions of the image
    disp("Size:");
    disp(size(img));

    disp("Number of cols:");
    disp(Mat_cols_get(img));

    disp("Number of rows:");
    disp(Mat_rows_get(img));

    delete_Mat(img);

    scicv_Init();

    // Create an empty image
    img = new_Mat();
    disp("Empty ?");
    disp(Mat_empty(img));
    delete_Mat(img);

    // Create a color image
    img = new_Mat(300, 200, CV_8UC3);
    disp(getImageType(img));
    delete_Mat(img);

    // Create a (black) grayscale image
    img = new_Mat(300, 200, CV_8UC1, 0);
    disp(getImageType(img));
    delete_Mat(img);

    scicv_Init();

    // Create a blue color image
    blue_color = [255, 0, 0] // BGR
    img = new_Mat(10, 5, CV_8UC3, blue_color);

    // Extract the whole image
    disp(img(:,:));

    // Extract the last row
    disp(img($,:));

    // Extract the two first rows and columns
    disp(img(1:2,1:2));

    delete_Mat(img);

### See also

- Mat

Report an issue
<< Datatypes | Datatypes | PtList >>