for i=1:max_g sprintf('迭代次数%d',i) %分类 L1=zeros(1,size(P,1));%建立一个1行12列的数组 for j=1:size(P,1) d=zeros(1,k);%不同类簇的个数 for m=1:k d(m)=norm([P(j,:)-temp1a(m,:)]);%计算欧氏距离,norm表示范数,P(j,:)和temp1a(m,:)是两点坐标,差即为两点之间的距离向量,然后计算该向量的范数,即可得到欧几里得距离。 end [~,index]=min(d);%把欧式距离更新为当前的最小值 L1(j)=index; end LL1(i,:)=L1;%将L1向量赋值给LL1矩阵的第i行 temp2a=temp1a; for j=1:k T=find(L1==j);%j=3 L1中所有等于j的元素的下标放入T中储存 if size(T,2)~=0 temp2a(j,:)=sum(P(T,:))/size(T,2);%将P第T列的平均值,存储到temp2a矩阵的第j行中。 else temp2a(j,:)=[min_x+(max_x-min_x)*rand,min_y+(max_y-min_y)*rand];%生成二维随机坐标点。rand函数生成一个0到1之间的随机数,将其乘以对应轴的范围再加上最小值。 end end if i==1 temp1a=temp2a; %temp2a 赋值给 temp1a else if sum(sum(temp2a-temp1a))==0 %当 i>1时,判断temp2a和temp1a的差别是否为零,如果是,则跳出循环。第一个sum生成3*1矩阵,第二个sum生成一个数 break else temp1a=temp2a; %将temp2a赋值给temp1a end end end

时间: 2024-04-25 18:28:01 浏览: 9
这段代码是 K-means 聚类算法的实现,其中 max_g 是最大迭代次数,P 是数据集,k 是要聚成几类,temp1a 是初始的聚类中心点。在算法中,首先将数据集中每个点归到距离最近的聚类中心所在的类别中,然后根据每个类别中的数据点重新计算聚类中心点,直到聚类中心点不再变化或者达到最大迭代次数为止。最后,LL1 矩阵中的每一行表示每一次迭代后数据集中每个点所属的类别。
相关问题

% 输出每个人的任务分配 for i = 1:person_num fprintf('第%d个人的任务分配:\n', i); disp(find(assignment_matrix(:, i))); end

如果您想将输出的任务编号写入表格中,您可以将其输出到一个CSV文件中。可以使用MATLAB的csvwrite命令将数据写入到CSV文件中。以下是一个示例代码,可以将每个人的任务分配写入到名为“assignment.csv”的CSV文件中。 ``` % 定义CSV文件名 filename = 'assignment.csv'; % 打开CSV文件进行写入 fid = fopen(filename, 'w'); % 循环遍历每个人的任务分配 for i = 1:person_num % 获取当前人的任务编号 task_ids = find(assignment_matrix(:, i)); % 将任务编号转换为字符串 task_ids_str = sprintf('%d,', task_ids); task_ids_str = task_ids_str(1:end-1); % 去除最后一个逗号 % 将任务编号写入到CSV文件中 fprintf(fid, 'Person %d,%s\n', i, task_ids_str); end % 关闭CSV文件 fclose(fid); % 输出写入完成的消息 fprintf('任务分配已写入到文件 %s 中。\n', filename); ``` 请注意,上述代码中的“assignment_matrix”是您自己定义的一个变量,用于存储任务分配结果的矩阵。在使用上述代码之前,请确保您已经定义了此变量,并已经将任务分配结果存储在其中。

% 指定包含SEM图像的目录 image_dir = 'D:\MATLAB\R2018a\bin\灰岩12个\样7\500X\'; % 从目录中读取图像文件名列表 image_files = dir(fullfile(image_dir, '*.tiff')); % K-均值聚类的参数 num_clusters = 3; % 簇数(可以更改此值) max_iterations = 100; % 最大迭代次数(可以更改此值) % 初始化矩阵以存储群集映像和群集中心 num_images = numel(image_files); % 计算图像文件数 clustered_images = cell(1, num_images); cluster_centers_all = cell(1, num_images); % 循环浏览每个图像文件 for i = 1:num_images % 读取当前图像并规范化 image_path = fullfile(image_dir, image_files(i).name); image_data = double(imread(image_path))/ 255; % 执行K-means聚类 [cluster_indices, cluster_centers] = kmeans(reshape(image_data,[],size(image_data,3)), num_clusters,'MaxIter',max_iterations); % 将聚集的数据重新整形为图像维度 clustered_images{i} = reshape(cluster_indices, size(image_data,1),size(image_data,2)); % 将聚类图像转换成彩色图像 RGB = zeros(size(image_data)); for j = 1:num_clusters RGB(:,:,j) = (clustered_images{i} == j); end RGB = bsxfun(@times, RGB, reshape(cluster_centers, 1,1,[])); clustered_images{i} = RGB; % 保存聚类后的图像到文件夹 [pathstr, name, ext] = fileparts(image_path); imwrite(uint8(RGB*255), fullfile(pathstr, [name '_clustered' ext])); end % 显示原始图像和群集图像 for i = 1:num_images figure; subplot(1, num_clusters + 1, 1); imshow(imread(fullfile(image_dir, image_files(i).name))); title('Original Image'); for j = 1:num_clusters subplot(1, num_clusters + 1, j + 1); imshow(clustered_images{i}); title(sprintf('Cluster %d', j)); end end % 计算孔隙率 porosity = zeros(1, num_images); for i = 1:num_images % 统计原始图像中的像素数 img_pixels = numel(imread(fullfile(image_dir, image_files(i).name))); % 统计聚类图像中标记为第一个簇的像素数 cluster_pixels = sum(sum(clustered_images{i}(:,:,1) > 0)); % 计算孔隙率 porosity(i)=(1 - (cluster_pixels / img_pixels))*100; end % 显示计算后的孔隙率 for i = 1:num_images fprintf('Image %d: Porosity = %f\n', i, porosity(i)); end

这是一段 MATLAB 代码,对一组 SEM 图像执行了 K-means 聚类,并计算了孔隙率。其中,通过指定包含 SEM 图像的目录,使用 `dir` 函数读取图像文件名列表,然后使用 `imread` 函数读取图像数据并规范化。接着,使用 `kmeans` 函数对图像进行 K-means 聚类,将聚类图像转换成彩色图像,并保存聚类后的图像到文件夹。最后,计算每个图像的孔隙率,并输出计算结果。

相关推荐

优化这段代码 if(i > num - 1) break; isDraw = (not == 2 && (draw == i+1)); if (DBData[i] != RT1064_YS_YYDD && DBData[i] != RT1064KZZ_FC_CHZ ) LCD_DisString((i%LISTOFFSET)+1, 0, gRunPara.gap_ActionDZInfo[gapid][DBData[i]].szName); if (DBData[i] == RT1064KZZ_FC_CHZ) LCD_DisString((i%LISTOFFSET)+1, 0, (char *)"大电流闭锁重合闸"); if (DBData[i] == RT1064_YS_YYDD) LCD_DisString((i%LISTOFFSET)+1, 0, (char *)"重合闸检有压时间"); val = get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i]); if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) ); } else if(DBData[i] == RT1064KZZ_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) ); } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) ); } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , val); LCD_DisString((i%LISTOFFSET)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); if (isDraw == 1) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }

filename = 'lowshiyan.xlsx'; sheet = 1; [num,txt,raw] = xlsread(filename, sheet); % 添加标签 G = num(:,1); P = num(:,2); T = num(:,3); M = num(:,4); F = num(:,5); Ta = num(:,6); num_images = size(num, 1); image_size = [10, 10]; data_images = zeros([image_size, num_images]); for k = 1:num_images num_elements = numel(num(k,1:5)); num_rows = ceil(num_elements/image_size(1)); image_matrix = reshape(num(k,1:5), num_rows, [])'; % 转置后再reshape resized_image_matrix = imresize([image_matrix, zeros(5, 1)], [10, 2]); % 在右边添加空列将大小从5x1扩展到5x2 resized_image_matrix = resized_image_matrix(:, 1:end-1); % 删除添加的空列 Ta_matrix = Ta(k); % 取第六列数据作为输出数据 image_10by10 = imresize(resized_image_matrix, [10, 10]); % 将大小调整为10x10 data_images(:,:,k) = mat2gray(image_10by10); Ta_images(k) = Ta_matrix; % 存储输出数据 end % 保存输入数据 if ~exist('input_images', 'dir') mkdir('input_images'); % 创建新的文件夹用于存储图像 end for k = 1:num_images input_filename = sprintf('input_images/%d.jpg', k); imwrite(data_images(:,:,k), input_filename, 'jpg'); end % 保存输出数据 if ~exist('output_data', 'dir') mkdir('output_data') % 创建新的文件夹用于存储输出数据 end for k = 1:num_images output_filename = sprintf('output_data/%d.txt', k); dlmwrite(output_filename, Ta_images(k), 'precision', '%.6f'); end % 创建ImageDatastore对象 imds = imageDatastore('input_images', 'FileExtensions', '.jpg', 'LabelSource', 'foldernames'); imds.ReadFcn = @(filename)imresize(imread(filename), [32, 32]); % 调整图像大小为32x32 % 添加输出数据 outputds = tabularTextDatastore('output_data/*.txt', 'ReadVariableNames', false); imds = combine(imds, outputds); % 划分训练集和测试集 [trainImds, testImds] = splitEachLabel(imds, 0.8, 'randomized');检查对函数 'splitEachLabel' 的调用中是否存在不正确的参数数据类型或缺少参数。怎么修改,请给出修改后代码

优化这段代码 if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) ); } else if(DBData[i] == RT1064KZZ_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) ); } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) ); } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , val); LCD_DisString((i%LISTOFFSET)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); if (isDraw == 1) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); } else { sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%LISTOFFSET)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"HZ"); } else LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"V"); if (isDraw == 1)len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }

将下面这段代码改用python写出来: clear all; close all; fdir = '../dataset/iso/saii/'; %Reconstruction parameters depth_start = 710; depth_end = 720; depth_step = 1; pitch = 12; sensor_sizex = 24; focal_length = 8; lens_x = 4; lens_y = 4; %% import elemental image infile=[fdir '11.bmp']; outfile=[fdir, 'EIRC/']; mkdir(outfile); original_ei=uint8(imread(infile)); [v,h,d]=size(original_ei); %eny = v/lens_y; enx = h/lens_x; % Calculate real focal length %f_ratio=36/sensor_sizex; sensor_sizey = sensor_sizex * (v/h); %focal_length = focal_length*f_ratio; EI = zeros(v, h, d, lens_x * lens_y,'uint8'); for y = 1:lens_y for x = 1:lens_x temp=imread([fdir num2str(y),num2str(x),'.bmp']); EI(:, :, :, x + (y-1) * lens_y) = temp; end end %Reconstruction [EIy, EIx, Color] = size(EI(:,:,:,1)); %% EI_VCR time=[]; for Zr = depth_start:depth_step:depth_end tic; Shx = 8*round((EIx*pitch*focal_length)/(sensor_sizex*Zr)); Shy = 8*round((EIy*pitch*focal_length)/(sensor_sizey*Zr)); Img = (double(zeros(EIy+(lens_y-1)*Shy,EIx+(lens_x-1)*Shx, Color))); Intensity = (uint16(zeros(EIy+(lens_y-1)*Shy,EIx+(lens_x-1)*Shx, Color))); for y=1:lens_y for x=1:lens_x Img((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) = Img((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) + im2double(EI(:,:,:,x+(y-1)*lens_y)); Intensity((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) = Intensity((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) + uint16(ones(EIy,EIx,Color)); end end elapse=toc time=[time elapse]; display(['--------------- Z = ', num2str(Zr), ' is processed ---------------']); Fname = sprintf('EIRC/%dmm.png',Zr); imwrite(Img./double(Intensity), [fdir Fname]); end csvwrite([fdir 'EIRC/time.csv'],time);

优化这段代码 if(i >= COMMS_NET_TOTALSUM) break; memset(szVal, 0, sizeof(szVal)); sprintf(szVal, "%s", gRunPara.COMMS_NetInfo[netid][i].szName); LCD_DisString((i%9)+1, 0, szVal); if( ((i >= COM_NET_PCL) && (i <= COM_NET_DIR)) || ((i >= COM_NET_YXM) && (i <= COM_NET_SNTP)) || (i == COM_NET_SYNCTM)) { if( i == COM_NET_PCL ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if(((i > COM_NET_PCL) && (i < COM_NET_DIR)) || i == COM_NET_SNTP ) { unsigned char bytesforIP[4]; if (i == COM_NET_IP) { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(buf,"%d%d%d.%d%d%d.%d%d%d.%d%d%d",bytesforIP[0]/100,bytesforIP[0]%100/10,bytesforIP[0]%10,bytesforIP[1]/100,bytesforIP[1]%100/10,bytesforIP[1]%10, bytesforIP[2]/100,bytesforIP[2]%100/10,bytesforIP[2]%10,bytesforIP[3]/100,bytesforIP[3]%100/10,bytesforIP[3]%10); LCD_DisString((i%9)+1, 10, buf); len = strlen(buf); if (not == 2) Lcd_IP_Not(netid,i,j,len,buf); } else { if (i == COM_NET_SNTP ) { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 14, szVal); } else { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 10, szVal); } } } else if( i == COM_NET_DIR ) { LCD_DisString((i%9)+1, 16, (char *)gcszStateName1[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YXM || i == COM_NET_YKM) { LCD_DisString((i%9)+1, 16, (char *)gcszPoint[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YCM ) { LCD_DisString((i%9)+1, 16, (char *)gcszData[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_JM ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_AREA ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_SYNCTM ) { LCD_DisString((i%9)+1, 18, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } } else { if (i >= 12 && i <= 14) { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 16, szVal); } else { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 18, szVal); } }

优化这段代码 if(i >= COMMS_NET_TOTALSUM) break; memset(szVal, 0, sizeof(szVal)); sprintf(szVal, "%s", gRunPara.COMMS_NetInfo[netid][i].szName); LCD_DisString((i%9)+1, 0, szVal); if( ((i >= COM_NET_PCL) && (i <= COM_NET_DIR)) || ((i >= COM_NET_YXM) && (i <= COM_NET_SNTP)) || (i == COM_NET_SYNCTM)) { if( i == COM_NET_PCL ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if(((i > COM_NET_PCL) && (i < COM_NET_DIR)) || i == COM_NET_SNTP ) { unsigned char bytesforIP[4]; if (i == COM_NET_IP) { *(float*)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(buf,"%d%d%d.%d%d%d.%d%d%d.%d%d%d",bytesforIP[0]/100,bytesforIP[0]%100/10,bytesforIP[0]%10,bytesforIP[1]/100,bytesforIP[1]%100/10,bytesforIP[1]%10, bytesforIP[2]/100,bytesforIP[2]%100/10,bytesforIP[2]%10,bytesforIP[3]/100,bytesforIP[3]%100/10,bytesforIP[3]%10); LCD_DisString((i%9)+1, 10, buf); len = strlen(buf); if (not == 2) Lcd_IP_Not(netid,i,j,len,buf); } else { if (i == COM_NET_SNTP ) { *(float*)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 14, szVal); } else { *(float*)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 10, szVal); } } } else if( i == COM_NET_DIR ) { LCD_DisString((i%9)+1, 16, (char *)gcszStateName1[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YXM || i == COM_NET_YKM) { LCD_DisString((i%9)+1, 16, (char *)gcszPoint[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YCM ) { LCD_DisString((i%9)+1, 16, (char *)gcszData[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_JM ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_AREA ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_SYNCTM ) { LCD_DisString((i%9)+1, 18, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } } else { if (i >= 12 && i <= 14) { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 16, szVal); } else { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 18, szVal); } }

% 读取图片文件夹中的所有图片 img_folder = 'C:\Users\15225\Desktop\keti_matlab\Pending images/'; img_files = dir(fullfile(img_folder, '*.bmp')); for i = 1:length(img_files) % 读取图片 img = imread(fullfile(img_folder, img_files(i).name)); % 灰度化 gray_img = im2gray(img); % 阈值分割-亮度大于该值的设置为1(亮点) 反之为0(暗点) threshold = 240; bw_img = gray_img > threshold; % 去除小的连通域-像素个数大于该值的会被计算标记 反之不计算标记 bw_img = bwareaopen(bw_img, 750); % 填充连通域内部空洞 bw_img = imfill(bw_img, 'holes'); % 获取连通域属性-获取二值图像中所有连通域的重心坐标 CC = bwconncomp(bw_img); stats = regionprops(CC, 'Centroid'); % 在原图上绘制标记点和序号 figure; imshow(img); hold on; markers = struct('index', {}, 'position', {}); for j = 1:length(stats) x = stats(j).Centroid(1); y = stats(j).Centroid(2); % 绘制红色圆点大小为 - 10 宽度为 - 2 plot(x, y, 'ro', 'MarkerSize', 10, 'LineWidth', 2); % 在标记点旁边添加序号文本 text(x+10, y+10, num2str(j), 'Color', 'r'); % 存储序号和位置信息到结构体数组 markers(j).index = j; markers(j).position = [x, y]; end % 保存 二值化 图片 result_folder = 'C:\Users\15225\Desktop\keti_matlab\results\'; bw_result_file = fullfile(result_folder, sprintf('bw_result_%d.bmp', i)); imwrite(bw_img, bw_result_file); fprintf('Extracted markers saved to file: %s\n', bw_result_file); % 保存 重心标记 图片 result_file = fullfile(result_folder, sprintf('result_%d.bmp', i)); saveas(gcf, result_file); fprintf('Extracted markers saved to file: %s\n', result_file); % 保存 重心坐标 到文件 result_txt_file = fullfile(result_folder, sprintf('result_%d.txt', i)); fid = fopen(result_txt_file, 'w'); for j = 1:length(markers) fprintf(fid, 'Marker #%d: (%.6g, %.6g)\n', markers(j).index, markers(j).position); end fclose(fid); end 添加代码需求,保存重心在世界坐标系下的坐标

最新推荐

recommend-type

单片机C语言Proteus仿真实例可演奏的电子琴

单片机C语言Proteus仿真实例可演奏的电子琴提取方式是百度网盘分享地址
recommend-type

电力概预算软件.zip

电力概预算软件
recommend-type

setuptools-64.0.0.tar.gz

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

爱你老妈(HTML文件)母亲节快乐

母亲节祝福html源码 很简单的代码,随机生成背景
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依