matlab延时函数怎么写_论文里的一个别人写的MATLAB函数,

这是一个用于从bmp图像中提取平面坐标的MATLAB M函数。该函数可以处理二维或三维图像,使用Canny边缘检测,并将结果保存为CSV文件。用户可以根据图像的实际宽度和高度比例以及读取和写入目录来调用函数。
摘要由CSDN通过智能技术生成

Supplementary Material S2. M-function in Matlab for extracting the planar coordinates from a bmp image and its usage

The following codes in shade should be saved in a text file named ‘updprofile’ with a default extension name ‘txt’, and then change the extension name of this text file from ‘txt’ to ‘m’. If the user is familiar with M-file in Matlab, the following codes in shade can be directly saved as an M-file in Matlab. The function can be used in Matlab (version ≥ R2009a).

%%%% Development time: Early July, 2014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%% Function: To produce the planar coordinates from a bmp image %%%%

%%%% e-mail: peijianshi@gmail.com (Dr. Peijian Shi) %%%%%%%%%%%%%%%%%%%

%%%% There are four parameters that must be necessarily input %%%%%%%%%

%%%% The input order of these four parameters CANNOT be changed %%%%%%%

%%%% x_ratio denotes the ratio of image width to its real width %%%%%%%

%%%% y_ratio denotes the ratio of image height to its real height %%%%%

%%%% read_dir denotes the directory where the image is stored %%%%%%%%%

%%%% The image type has been actually designated in read_dir %%%%%%%%%%

%%%% write_dir denotes the directory where the data will be stored %%%%

%%%% The stored data type should be designated in write_dir %%%%%%%%%%%

%%%% e.g., profile(1, 1, 'c:/SF.bmp', 'c:/edge_data.csv') %%%%%%%%%%

%%%% Reference: Xue SF. 2009. J. Hengshui Univ. 11(4), 25-27 %%%%%%%%%%

%%%% The function has been largely improved relative to Xue's %%%%%%%%%

function shape = updprofile(str, x_ratio, y_ratio, read_dir, write_dir)

% str = {'e:/PShi/SF.bmp', 'e:/edge_data.csv'};

if nargin < 5, write_dir = str(2); end

if nargin < 4, read_dir = str(1); end

if nargin < 3, y_ratio = 1; end

if nargin < 2, x_ratio = 1; end

c = imread( char(strcat(read_dir)) );

INFO = imfinfo( char(strcat(read_dir)) );

%%%% To obtain the information of image resolutions (in pixel per cm) %%%%%%

DPI.h = INFO.HorzResolution/100;

DPI.v = INFO.VertResolution/100;

%%%% To decide whether the image is planar or three-dimensional %%%%

len.value = length(size(c));

%%%% If it is three-dimensional, we transform it into be planar %%%%

if len.value > 2

c = rgb2gray(c);

end

b = edge(flipud(c),'canny');

[u, v] = find(b);

xp = v;

yp = u;

x0 = mean([min(xp), max(xp)]);

y0 = mean([min(yp), max(yp)]);

xp1 = xp - x0;

yp1 = yp - y0;

[cita, r] = cart2pol(xp1, yp1);

q = sortrows([cita, r]);

cita = q(:, 1);

r = q(:,2);

[x, y] = pol2cart(cita, r);

x = x + x0;

y = y + y0;

%%%% A is the real height of the image in pixel %%%%%

%%%% B is the real width of the image in pixel %%%%

%%%% P is the real height of the image in cm %%%%%%%%

%%%% Q is the real width of the image in cm %%%%%%%

[A B] = size(c);

P = A / DPI.h;

Q = B / DPI.v;

x = x/B*Q / x_ratio;

y = y/A*P / y_ratio;

figure(1)

plot(x, y, 'k-', 'LineWidth', 3)

xlim([min(x) max(x)])

ylim([min(y) max(y)])

%%%% adds a text of image number %%%%%%%%%%%%%%%%%%%%%%%

half = max([(max(x)-min(x))/2 (max(y)-min(y))/2]);

xcen = (max(x)+min(x))/2;

ycen = (max(y)+min(y))/2;

xlow = xcen - half;

xup = xcen + half;

ylow = ycen - half;

yup = ycen + half;

xtext=xlow+(xup-xlow)/20;

ytext=yup-(yup-ylow)/20;

char1 = char(strcat(read_dir));

ind = strfind(char1, '.');

char2 = char1(13:(ind-1));

text(xtext, ytext, char2, 'FontSize',14, 'Color', 'b')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

axis equal

z = [x y];

csvwrite( char(strcat(write_dir)), z);

Assume that there is folder named ‘PShi’ in e: disk, and a subfolder named ‘black_white’ in folder ‘PShi’. The suggested working directory of Matlab is ‘e:/PShi’. The scanned leaf images were saved in black−white bitmap format. The inner of a leaf edge was filled in white, and the outer of the leaf edge is filled in black (Fig. A1). Usually, 400 dpi is sufficient for the scanned leaf image if there are no small teeth around the leaf edge. All black−white images in bmp format were saved in subfolder ‘black_white’. Then we can use the following codes to extract the planar coordinates of these leaf images in bulk.

ccb6c12c5bd8d6d639d3c21800f37163.png

bc16baac262547f2580de01238c514c7.png

Fig. S1 A leaf edge image in black−white bitmap format for Indocalamus pedalis (Keng) P. C. Keng

cd('e:/PShi/black_white')

allnames = struct2cell(dir);

temp = size(allnames);

n = temp(2)-2;

cd('e:/PShi')

mkdir 'edge_data'

%%%% x_ratio denotes the ratio of image width to its real width %%%%%%%

%%%% y_ratio denotes the ratio of image height to its real height %%%%%

x_ratio = 1;

y_ratio = 1;

for i = 3:(n+2)

input1 = strcat('black_white/', allnames{1,i} );

output1 = strcat('edge_data/', allnames{1,i}, '.csv' );

string = {input1, output1};

updprofile(string, x_ratio, y_ratio);

end

If there are the second-order subfolders in the subfolder ‘black_white’, we should use the following different procedure to extract the planar coordinates of these leaf images in bulk. However, the following procedure does not allow the third-order subfolders. That mean, the black−white images were saved in the different second-order folders under the subfolder ‘black_white’. There should be not the third-order subfolders under any a second-order folder.

cd('e:/PShi')

mkdir 'edge_data'

cd('e:/PShi/black_white')

allnames = struct2cell(dir);

temp = size(allnames);

n = temp(2)-2;

for q = 3:(n+2)

cd( strcat('e:/PShi/black_white/', allnames{1, q}) );

subnames = struct2cell(dir);

temp1 = size(subnames);

n1 = temp1(2)-2;

for p = 3:(n1+2)

newname = strcat(allnames{1, q}, '-', subnames{1, p});

eval( ['!rename' 32 subnames{1,p} 32 newname] );

% movefile(subnames{1, p}, newname)

end

end

for i = 3:(n+2)

cd( strcat('e:/PShi/black_white/', allnames{1, i}) )

subnames = struct2cell(dir);

temp1 = size(subnames);

n1 = temp1(2)-2;

for j = 3:(n1+2)

input1 = strcat('black_white/', allnames{1, i}, '/', subnames{1, j} );

output1 = strcat('edge_data/', subnames{1, j}, '.csv' );

string = {input1, output1};

cd('e:/PShi')

%%%% x_ratio denotes the ratio of image width to its real width %%%%%%%

%%%% y_ratio denotes the ratio of image height to its real height %%%%%

x_ratio = 1;

y_ratio = 1;

updprofile(string, x_ratio, y_ratio);

cd( strcat('e:/PShi/black_white/', allnames{1, i}) )

end

end

After copying the above codes to the command window of Matlab, we can obtain the planar coordinates for each black−white image. These results are saved in CSV file in a new subfolder named ‘edge_data’ under folder ‘PShi’. The planer coordinates of the leaf edge in Fig. S1 were saved as the CSV file named ‘Ai-71.bmp’ (see the online Supplementary Material S3).

MATLAB中,可以使用以下几种方法来实现延时功能: 1. 使用pause函数:pause函数可以暂停程序的执行一段指定的时间。但需要注意的是,pause函数会暂停整个程序的执行,包括硬件采集部分,可能会产生冲突。因此,pause函数并不适合用于精确的延时操作。 2. 使用循环延时:可以通过编一个循环来实现延时的效果。循环中可以执行一些无关紧要的操作,以浪费一定的处理时间。这样可以达到延时的目的。但是这种方法并不精确,因为不同的电脑状态和配置可能会导致不同的延时时间。 3. 使用timer函数:timer函数可以创建一个定时触发器,可以设置定时器的触发时间和触发函数。但是,timer函数并不适合用于精确的延时操作,因为它是基于系统时间的,可能会受到系统负载和其他因素的影响。 综上所述,如果需要在MATLAB中实现精确的延时操作,可能需要自己编一个循环函数来实现。但是要注意,不同的电脑状态和配置可能会导致不同的延时时间。 #### 引用[.reference_title] - *1* *3* [MATLAB中如何用循环实现精确延时](https://blog.csdn.net/lvmeng987/article/details/44514135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [matlab延时函数怎么](https://blog.csdn.net/weixin_39949297/article/details/117096738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值