【问题标题】:Matlab syntax [explanation in C++]Matlab语法【C++解释】
【发布时间】:2014-12-08 19:10:15
【问题描述】:

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

以下是读取图像文件的代码sn-p。请解释一下 [height width]VolData(:, :, i) 在 C++ 术语中的用途和方式。

%% read images
clc
clear all
cd ('..\STLBP_Matlab\test\'); % 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 ..

非常感谢您。

【问题讨论】:

    标签: c++ image matlab matrix porting


    【解决方案1】:

    代码只是从一个文件夹中读取一堆 JPEG 图像(大小都相同),将它们转换为灰度图像,然后沿三维堆叠图像以构建我认为的体积数据数组。这个数组的大小是高×宽×N,其中 N 是图像的数量。

    在 C/C++ 中,您只需分配一个该大小的缓冲区数组,然后一个接一个地复制每个图像的像素(按行优先顺序,因为这是 C 中的约定)。您可以使用memcpy 之类的方式有效地做到这一点。

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

      • 嗯,c++11 确实支持tuples。仍然只有一行,所以无论如何按照自己的方式做可能会更简单。无论如何很好的解释。
      • @madmik3:顺便说一句,MATLAB 中也没有元组(我认为您将它与 Python 混淆了)。相反,MATLAB 可以自然地从一个函数返回多个变量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2013-03-19
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多