Matlab语法[C++解释]
Matlab syntax [explanation in C++]
我需要将代码从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
相关文章:
- 请解释"函数1(p1,p2,p3);"的输出
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 被解释为低级别const的const对象的地址
- 计算每个节点的树高,帮助我解释这个代码解决方案
- MSVC将仅移动结构参数解释为指针
- 内联程序集printf将整数解释为地址
- 有人能解释一下为什么下界是这样工作的吗C++的
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 有人能为我解释一下C++代码吗
- 你能解释一下什么运行时错误是如何解决它的吗?
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 有人可以向我解释为什么控制台输出 0 吗?
- 有人可以解释一下这段代码如何能够反转字符串
- 在 C++ 中解释多维向量的语句时感到困惑
- 解释一下 for (char c : str) 的作用?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 隐式重新解释引用时强制转换,没有警告/错误
- 有人可以在 c++ 中解释数组的这种输入
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?