概述
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.
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延时函数怎么写_论文里的一个别人写的MATLAB函数,的全部内容,希望文章能够帮你解决matlab延时函数怎么写_论文里的一个别人写的MATLAB函数,所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复