本人小白,自己编了个代码,能把一张图片二值化,然后读取图中黑色区域的像素点数,但我想实现批量读取,然后处理的目的,求大神帮忙
I=imread('top051.bmp');
Zmax=max(max(I));
Zmin=min(min(I));
TK=(Zmax+Zmin)/2;%初始阈值
flag=1;
[m,n]=size(I);
while(flag)
fg=0;
bg=0;
fgsum=0;
bgsum=0;
for i=1:m
for j=1:n
tmp=I(i,j);
if(tmp>=TK)
fg=fg+1;
fgsum=fgsum+double(tmp);
else
bg=bg+1;
bgsum=bgsum+double(tmp);
end
end
end
u1=fgsum/fg;
u2=bgsum/bg;
TKTmp=uint8((u1+u2)/2);
if(TKTmp==TK)
flag=0;
else
TK=TKTmp;
end
end
BCT = sum(newI (:)==0)有可能的话,请好心朋友在这个基础上做些修改,已达到批量读取图片,并批量输出黑色区域像素点数的目的,在此谢过了,图片名称是由top001、top002、top003..........top452,这样的顺序编排的,请求详细答案,本人实在小白。。。。
matlab如何批量读取图片并作分析
答案:2 悬赏:30 手机版
解决时间 2021-02-04 11:22
- 提问者网友:姑娘长的好罪过
- 2021-02-03 13:37
最佳答案
- 五星知识达人网友:一叶十三刺
- 2021-02-03 15:14
for i=1:452
if i<10
str = ['top00',num2str(i),'.bmp'];
else if i<100
str = ['top0',num2str(i),'.bmp'];
else
str = ['top',num2str(i),'.bmp'];
end
I=imread(str);
Zmax=max(max(I));
Zmin=min(min(I));
TK=(Zmax+Zmin)/2;%初始阈值
flag=1;
[m,n]=size(I);
while(flag)
fg=0;
bg=0;
fgsum=0;
bgsum=0;
for i=1:m
for j=1:n
tmp=I(i,j);
if(tmp>=TK)
fg=fg+1;
fgsum=fgsum+double(tmp);
else
bg=bg+1;
bgsum=bgsum+double(tmp);
end
end
end
u1=fgsum/fg;
u2=bgsum/bg;
TKTmp=uint8((u1+u2)/2);
if(TKTmp==TK)
flag=0;
else
TK=TKTmp;
end
end
end
if i<10
str = ['top00',num2str(i),'.bmp'];
else if i<100
str = ['top0',num2str(i),'.bmp'];
else
str = ['top',num2str(i),'.bmp'];
end
I=imread(str);
Zmax=max(max(I));
Zmin=min(min(I));
TK=(Zmax+Zmin)/2;%初始阈值
flag=1;
[m,n]=size(I);
while(flag)
fg=0;
bg=0;
fgsum=0;
bgsum=0;
for i=1:m
for j=1:n
tmp=I(i,j);
if(tmp>=TK)
fg=fg+1;
fgsum=fgsum+double(tmp);
else
bg=bg+1;
bgsum=bgsum+double(tmp);
end
end
end
u1=fgsum/fg;
u2=bgsum/bg;
TKTmp=uint8((u1+u2)/2);
if(TKTmp==TK)
flag=0;
else
TK=TKTmp;
end
end
end
全部回答
- 1楼网友:舊物识亽
- 2021-02-03 16:26
% 你的代码没有仔细看,有问题在说吧,结果在r中
n = 452;
r = zeros(n,1);
for p = 1:n
fn = ['000' num2str(p)];
fn = fn(length(fn)-1:end);
fn = ['top' fn '.bmp'];
i=imread(fn);
zmax=max(max(i));
zmin=min(min(i));
tk=(zmax+zmin)/2;%初始阈值
flag=1;
[m,n]=size(i);
while(flag)
fg=0;
bg=0;
fgsum=0;
bgsum=0;
for i=1:m
for j=1:n
tmp=i(i,j);
if(tmp>=tk)
fg=fg+1;
fgsum=fgsum+double(tmp);
else
bg=bg+1;
bgsum=bgsum+double(tmp);
end
end
end
u1=fgsum/fg;
u2=bgsum/bg;
tktmp=uint8((u1+u2)/2);
if(tktmp==tk)
flag=0;
else
tk=tktmp;
end
end
bct = sum(newi (:)==0);
r(p) = bct;
end;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯