Matlab语法[C++解释]

Matlab syntax [explanation in C++]

本文关键字:解释 C++ 语法 Matlab      更新时间:2023-10-16

我需要将代码从Matlab移植到C++。然而,我在语法上有很多困惑,谷歌搜索对我没有帮助

以下是读取图像文件的代码片段。请解释一下[height width]VolData(:, :, i)在C++术语中是如何使用的。

%% read images
clc
clear all
cd ('..STLBP_Matlabtest'); % please replace "..." by your images path
a = dir('*.jpg'); % directory of images, ".jpg" can be changed, for example, ".bmp" if you use
for i = 1 : length(a)
    ImgName = getfield(a, {i}, 'name');
    Imgdat = imread(ImgName);
    if size(Imgdat, 3) == 3 % if color images, convert it to gray
        Imgdat = rgb2gray(Imgdat);
    end
    [height width] = size(Imgdat);
    if i == 1
        VolData = zeros(height, width, length(a));
    end
    VolData(:, :, i) = Imgdat;
end
cd ..

事先非常感谢。

代码只需从文件夹中读取一堆JPEG图像(大小相同),将其转换为灰度,然后将图像沿第三维度堆叠,即可构建我认为是体积数据阵列的图像。该阵列的大小为高×宽×N,其中N是图像的数量。

在C/C++中,您只需分配一个该大小的缓冲区数组,然后逐个复制每个图像的像素(按行主顺序,因为这是C中的约定)。你可以用memcpy这样的东西有效地做到这一点。

这段代码似乎正在将一些图像转换为灰度。这里有一些内联注释,但请注意,这在语法上是不正确的。您将需要一些图像处理库,最有可能加载jpegs并获得将更改语法的原始数据。

a = dir('*.jpg'); % directory of images, ".jpg" can be changed, for example, ".bmp" if you use
%% we are going to loop through all of the images in this directory
for i = 1 : length(a)
    %% give me the name of the current file to process
    ImgName = getfield(a, {i}, 'name');

    %% give me the image data of the first file to process
    Imgdat = imread(ImgName);
    %% if this is an rgb image convert it to gray scale
    if size(Imgdat, 3) == 3 % if color images, convert it to gray
        Imgdat = rgb2gray(Imgdat);
    end
    %% this is a tuple. in c++ there are no tuples
    %% so it would look like this
    %% height = size(ImgData).height;
    %% width = size(ImgData).with;
    [height width] = size(Imgdat);
    %% if this is the very first object create a 3 dimensional array large enough
    %% to hold all of the images, think of it as a stack of length(a) images
    %% of a constant width and height
    if i == 1
        VolData = zeros(height, width, length(a));
    end

    %% copy all of the image data into the vol data field.
    %% this is a vector function. it's really not something in the c++ syntax
    %% in c++ this would likely be nested loops or memcopy as Amro says
    %%  int fileNumb = fileCounter++;
    %%  for (int i =0 ; i < height;i++)
    %%    for (int j=0; j < width; j++)
    %%        somearry[i][j][fileNum] = ImgData[i][j];
    VolData(:, :, i) = Imgdat;
end