概述
(1)生成正样本
%% 读取文件夹是需要提取样本的视频采集图片,里面的1.txt是人工标出的目标坐标
%格式如下:
img_1.jpg 2 4 527 215 524 238 535 238 537 215 4 575 218 571 238 581 238 580 218
img_2.jpg 2 4 535 217 532 236 543 237 543 218 4 486 216 483 238 496 238 497 219
img_3.jpg 2 4 496 218 494 236 506 237 503 217 4 446 217 443 241 459 240 455 219
img_4.jpg 2 4 458 218 455 238 462 239 465 219 4 411 219 405 238 417 236 417 216
img_5.jpg 2 4 420 219 421 241 430 238 428 218 4 370 219 368 241 379 241 379 219
img_6.jpg 2 4 381 216 379 236 391 236 388 217 4 329 220 329 242 342 240 340 219
img_7.jpg 2 4 340 217 340 240 354 239 351 217 4 297 220 294 239 307 241 306 223
img_8.jpg 2 4 305 217 305 238 318 239 314 219 4 281 226 277 248 290 247 290 227
img_9.jpg 1 4 275 229 272 252 283 253 286 230
……
%%下面是生成正样本
strPath = 'D:programmatlab projectstraffic collection2012090511';
strNegPath = 'D:programmatlab projectstraffic collection20120905pos_vehiclepos_vehicle_11';
fid=fopen([strPath,'detectionGt.txt']);
if fid<=0
disp('cannot open the file');
return;
end
fid2=fopen('D:programmatlab projectstraffic collection20120905pos_vehiclepos_vehicle_11info.txt','w+');
if fid2<=0
disp('cannot write the file');
return;
end
numNeg = 0;
for i=1:1000
a= fscanf(fid,'%s ',1);
b=fscanf(fid,'%d',1); % b表示图像中目标的数目
if b~=0
img = imread([strPath,a]);
sizeImg=size(img);
imgMask=zeros(sizeImg(1:2));
for j = 1:b % 将b个目标的坐标读出来
bp = fscanf(fid,'%d',1);
if bp>3 && bp<10
c=fscanf(fid,'%d',bp*2);
else
c=fscanf(fid,'%d',6*2-1);%********************************************8
end
% c = fscanf(fid,'%d',bp*2);
% c表示标定此目标的点的坐标,其中点的数目为bp个或者6个
cx=c(1:2:end);
cy=c(2:2:end);
cx_max=max(cx);
cx_min=min(cx);
cy_max=max(cy);
cy_min=min(cy);
cx_min = max(1,cx_min);
cy_min = max(1,cy_min);
% 扩展
m=0.2*(cy_max-cy_min);
n=0.2*(cx_max-cx_min);
cx_left = round(max(1,cx_min-n));
cx_right = round(min(sizeImg(2),cx_max+n));
cy_up = round(max(1,cy_min-m));
cy_down = round(min(sizeImg(1),cy_max+m));
subImg=img(cy_up:cy_down, cx_left:cx_right,:);
subImg=imresize(subImg,[64,64]);
numNeg = numNeg+1;
imwrite(subImg,[strNegPath,num2str(numNeg,'%05d'),'.bmp']);
fprintf(fid2,[num2str(numNeg,'%05d'),'.bmp','n']);
end
d=fscanf(fid,'n');
end
end
fclose(fid);
fclose(fid2);
(2)生成负样本
%strPath = 'D:programmatlab projectstraffic collection20120823two2';
%strNegPath = 'D:programmatlab projectstraffic collectiontest_smple';
strPath = 'D:programmatlab projectsLib行人数据库2012101920121019173447164';
strNegPath ='D:programmatlab projectsLib行人数据库neg_lib_1019';
fid=fopen([strPath,'3.txt']);
if fid<=0
disp('cannot open the file');
return;
end
fid2=fopen('D:programmatlab projectsLib行人数据库neg_lib_1019info.txt','w+');
if fid2<=0
disp('cannot write the file');
return;
end
numNeg =0;
for i=1:150 %150为文件夹中的图片数
a= fscanf(fid,'%s ',1);
b=fscanf(fid,'%d',1); % b表示图像中目标的数目
if b==0 % 如果图像中没有目标,则按照间隔24个像素提取负样本,其中样本的尺寸为64×64
fscanf(fid,'n');
% cut image to some 64*64 subimages and save as neg images
img = imread([strPath,a]);
imgMask=zeros(size(img));
sizeImg = size(img);
for m=1:80:sizeImg(1)-31-80
for n=1:120:sizeImg(2)-15-120
rand_inter_m = floor((rand*80)); % 随机数[1,256]
rand_inter_n = floor(rand*120);
% subImg=img( m:m+47,n:n+47,:);
subImg=img(m+rand_inter_m:m+rand_inter_m+31,n+rand_inter_n:n+rand_inter_n+15,:);
numNeg = numNeg+1;
imwrite(subImg,[strNegPath,num2str(numNeg,'%05d'),'.bmp']);
fprintf(fid2,[num2str(numNeg,'%05d'),'.bmp','n']);
end
end
else
img = imread([strPath,a]);
sizeImg=size(img);
imgMask=zeros(sizeImg(1:2));
for j = 1:b % 将b个目标的坐标读出来
bp = fscanf(fid,'%d',1);
if bp>3 && bp<10
c=fscanf(fid,'%d',bp*2);
else
c=fscanf(fid,'%d',6*2-1);
end
% c表示标定此目标的点的坐标,其中点的数目为bp个或者6个
cx=c(1:2:end);
cy=c(2:2:end);
cx_max=max(cx);
cx_min=min(cx);
cy_max=max(cy);
cy_min=min(cy);
if(cy_min == 0)
cy_min=1;
end
if(cx_min==0)
cx_min=1;
end
imgMask(cy_min:cy_max,cx_min:cx_max)=1;
end
% 将目标区域标定出来后,在剩余的部分分割负样本
for m=1:80:sizeImg(1)-31-80
for n=1:120:sizeImg(2)-15-120
rand_inter_m = floor((rand*80)); % 随机数[1,256]
rand_inter_n = floor(rand*120);
% subImgMask=imgMask(m:m+47,n:n+47);
subImgMask=imgMask(m+rand_inter_m:m+rand_inter_m+31,n+rand_inter_n:n+rand_inter_n+15);
sumObj=sum(subImgMask(:));
if sumObj==0
% subImg=img( m:m+47,n:n+47,:);
subImg=img(m+rand_inter_m:m+rand_inter_m+31,n+rand_inter_n:n+rand_inter_n+15,:);
numNeg = numNeg+1;
imwrite(subImg,[strNegPath,num2str(numNeg,'%05d'),'.bmp']);
fprintf(fid2,[num2str(numNeg,'%05d'),'.bmp','n']);
end
end
end
% c=fscanf(fid,'%d',b*6*2);
d=fscanf(fid,'n');
end
end
fclose(fid); fclose(fid2);
最后
以上就是鲜艳豌豆为你收集整理的matlab生成,用matlab生成样本的全部内容,希望文章能够帮你解决matlab生成,用matlab生成样本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复