What is the .mat format file? And convert png, jpg, bmp, gif, tiff, psd and other format images to .mat format (with code)

The input requirements of many deep learning networks are in .mat format. Of course, the code of the input data can also be modified directly, such as using OpenCV to read images, etc. However, some networks are more troublesome to modify, and .mat data has many advantages, so some It is best to use the default .mat format data on the Internet

1. .mat format file

1.1 What is the .mat format file?

.mat files are MATLAB's standard format for data storage. This file is a standard binary file that can also be saved and loaded in ASCII format. The convenience of the .mat file is that it can be saved together with the variable name of the data, and there is no need to control the storage format of the data. MATLAB will automatically save and distinguish the content we store.

For example, if you want to easily save and load data in MATLAB, you can use the save command to generate .mat files. Here is a specific example:

% 创建一个3行3列的矩阵A并保存为.mat文件
A = magic(3);
save("Test.mat","A")

If you want to open the .mat file, you can use the load command. For example:

% 直接导入数据:两种方法
load("Test.mat")
load Test.mat

1.2 Why do some deep learning networks require .mat data?

.matFiles provide a convenient and efficient way to store and share data for deep learning.

Deep learning networks may require the use of data in .mat file format for the following reasons:

1.2.1 Data organization

.matFile is the standard format for data storage in MATLAB and can store many types of data, including matrices, images, audio, text, etc. Files in this format can organize multiple related data sets together for batch processing and analysis.

1.2.2 Data preprocessing

In deep learning, data preprocessing is an important step. .mat The file can store preprocessed data, such as normalized, standardized, feature extraction and other processed data. In this way, the deep learning network can directly use the preprocessed data without having to perform these processing steps.

1.2.3 Compatibility

Many deep learning tools and libraries, such as TensorFlow, Keras, PyTorch, etc., can read data in .mat files. This means that no matter which deep learning tool or library you use, you can easily use the data in the .mat file.

1.2.4 Data sharing

.mat files are a universal data format that can be shared across different platforms and tools. This means that if your deep learning network needs to use other people's data, or you want to share your data with others, .mat files are a good choice.

2. Common image formats

2.1 .jpeg

  • JPEG: Also called JPG or JPE format, it is the most commonly used file format. It is especially suitable for use on the Internet and can compress images into a small storage space.

2.2 .png

  • PNG: PNG images support transparent backgrounds and are suitable for making images with transparent backgrounds, such as logos, etc.

2.3 .gif

  • GIF: The biggest feature of the GIF format is that it supports dynamic pictures and supports transparent backgrounds. Most of the animations and emoticons on the Internet are in GIF format.

2.4 .psd

  • PSD: PSD format is the default storage format of Photoshop. It is suitable for storing source documents and working files, and is more convenient to modify.

2.5 .tiff

  • TIFF: TIFF format, also called or TIF format, can support different color modes, paths, transparency, and channels, and is the most commonly used format for printing documents.

2.6 .bmp

  • BMP: The BMP format is a standard image file format in the Windows operating system and is supported by a variety of Windows applications.

    .bmp format images are usually larger than other format images because it does not use any compression.

    Since the .bmp format image is not compressed, it maintains the original quality of the image. However, formats such as .jpg may suffer from loss of image quality due to the use of lossy compression.

    The .bmp format image is a bitmap image that records the color information of each pixel in the image.

2.7 .pcx

  • PCX: PCX is a raster image file format used as the native file format for the PC Paintbrush application. PCX files are smaller in size because they are compressed using RLE encoding.

2.8 .tga

  • TGA: TGA is an image file format developed by the American company Truevision for its display card. TGA files support a 256-color palette or full 24-bit RGB, and the image size is up to 64K*64K pixels.

2.8 .exif

  • EXIF: EXIF ​​is an image file format. In fact, the Exif format inserts digital photo information in the header of the JPEG format, including aperture, shutter, and white when shooting. Various shooting conditions such as balance, ISO, focal length, date and time, as well as camera brand, model, color coding, sound recorded during shooting, GPS global positioning system data, thumbnails, etc.

2.10 .fpx

  • FPX: FPX is an image format with multiple resolutions, that is, images are stored as a series of high and low resolutions. The advantage of this format is that when the image is enlarged, it remains Image quality can be maintained.

2.11 .svg

  • SVG: The SVG format file is the abbreviation of Scalable Vector Graphics file and is a standard graphics file type used for rendering two-dimensional images on the Internet. Unlike other popular image file formats, SVG format files store images as vectors, which are graphics based on mathematical formulas composed of points, lines, curves, and shapes.

2.12 .CR3

  • CR3: .CR3 files are the file format for raw images captured by Canon cameras. This is Canon's latest raw format, and most of its professional gear launched after 2018 supports it. The CR3 format is most useful for professional photo editors, allowing them to obtain unprocessed images. They can then use this raw information to style or otherwise manipulate the photo however they want. Currently, only three cameras, Canon EOS M50, EOS R, and EOS RP, support the CR3 recording format.

3. Convert .mat format code

3.1 Modify parameters

To use the code specifically, scholars only need to modify the following three places. The example I gave is to convert .png format pictures to .mat format. If you want to convert pictures in other formats, you only need to change the input_type to jpg or bmp. Just wait:

Insert image description here

3.2 Code

# 转换单个文件夹中的所有图片为mat格式
import sys
import cv2
import os
import numpy as np
from scipy.io import savemat
import time

def findFiles(root_dir, filter_type, reverse=False):

    print("Finding files ends with \'" + filter_type + "\' ...")
    separator = os.path.sep
    paths = []
    names = []
    files = []
    for parent, dirname, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith(filter_type):
                paths.append(parent + separator)
                names.append(filename)
    for i in range(paths.__len__()):
        files.append(paths[i] + names[i])
    print(names.__len__().__str__() + " files have been found.")
    paths.sort()
    names.sort()
    files.sort()
    if reverse:
        paths.reverse()
        names.reverse()
        files.reverse()
    return paths, names, files


def cvtImgs2MatAndSave(img_dir, file_type, img_key_name, out_path):
    paths, names, files = findFiles(img_dir, file_type)

    imgs = []
    for i in range(len(files)):
        tmp_img = cv2.imread(files[i])
        imgs.append(tmp_img)

    img_width = imgs[0].shape[1]
    img_height = imgs[0].shape[0]
    num_channel = imgs[0].shape[2]
    num_imgs = len(imgs)
    print("Target shape:[", num_imgs, img_height, img_width, num_channel, "]")
    img_mat = np.zeros([num_imgs, img_height, img_width, num_channel], np.uint8)

    for i in range(len(imgs)):
        img_mat[i, :, :, :] = imgs[i]

    img_dict = {
    
    img_key_name: img_mat,
                '__header__': 'Matlab MAT-file, Created by Xuhui Zhao on ' + time.ctime(),
                '__version__': '1.0',
                '__globals__': ''}

    savemat(out_path, img_dict)
    return img_dict

if __name__ == '__main__':
    input_img_dir = "TestImages/SIDD/Visible_Images"  # input影像块所在文件夹
    input_type = "png"  # input影像块文件类型
    out_dir = "TestImages/SIDD/mat"  # mat文件输出文件夹

    # 文件默认名如下(与SIDD一致)
    out_input_mat_name = out_dir + "/BenchmarkNoisyBlocksSrgb.mat"

    # 如果设置的文件名没有后缀名,再加上
    if not (out_input_mat_name.__contains__(".mat") or out_input_mat_name.__contains__(".MAT")):
        out_input_mat_name = out_input_mat_name + ".mat"

    # mat文件的key name(与SIDD一致)
    input_key_name = "BenchmarkNoisyBlocksSrgb"

    # 将多个影像文件转换成Mat文件 validation-input
    cvtImgs2MatAndSave(input_img_dir, input_type, input_key_name, out_input_mat_name)

3.3 Conversion results

Insert image description here

3.4 Data in .mat file

The data in the .mat file is as follows

Insert image description here

4. Summary

The above describes what a .mat format file is and why deep learning network training uses .mat format data. It also popularizes some common image formats and attaches png, jpg, bmp, gif, tiff, psd, etc. Detailed code for converting format pictures into .mat format.

I hope it can help you. It’s not easy to summarize. Thank you for your support!

Guess you like

Origin blog.csdn.net/qq_40280673/article/details/134673874