matlab生成,用matlab生成样本

(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:\program\matlab projects\traffic collection\20120905\11\';

strNegPath = 'D:\program\matlab projects\traffic collection\20120905\pos_vehicle\pos_vehicle_11\';

fid=fopen([strPath,'detectionGt.txt']);

if fid<=0

disp('cannot open the file');

return;

end

fid2=fopen('D:\program\matlab projects\traffic collection\20120905\pos_vehicle\pos_vehicle_11\info.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:\program\matlab projects\traffic collection\20120823\two2\';

%strNegPath = 'D:\program\matlab projects\traffic collection\test_smple\';

strPath = 'D:\program\matlab projects\Lib行人数据库\20121019\20121019173447164\';

strNegPath ='D:\program\matlab projects\Lib行人数据库\neg_lib_1019\';

fid=fopen([strPath,'3.txt']);

if fid<=0

disp('cannot open the file');

return;

end

fid2=fopen('D:\program\matlab projects\Lib行人数据库\neg_lib_1019\info.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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值