Created
December 9, 2017 14:07
-
-
Save zYeoman/a6988a0ae8d55ddb24c6ecd310668543 to your computer and use it in GitHub Desktop.
数字图像技术第四次作业
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 读取图片 | |
img = imread('船.bmp'); | |
%% 全局阈值二值化 | |
bw = img>65; | |
figure; | |
imshow(bw); | |
%% 横向投影 | |
ys = sum(bw,2); | |
figure; | |
plot(ys); | |
%% 去掉下面的部分和宽度大于1/2的部分 | |
for i=length(ys):-1:1 | |
if ys(i) > 0 | |
ys(i) = 0; | |
else | |
break; | |
end | |
end | |
ys(ys>size(img,2)/2) = 0; | |
bw(ys==0,:)=0; | |
figure; | |
imshow(bw); | |
%% 选取最大连通分量 | |
L = bwlabel(bw); | |
areas = regionprops(L, 'area'); | |
areas = [areas.Area]; | |
index = find(areas==max(areas)); | |
mask=L==index; | |
figure; | |
tmp = img; | |
tmp(mask==0) = 0; | |
imshow(tmp); | |
%% 区域生长 | |
figure; | |
mins = [109*ones(35,1),(121:155)']; | |
thres = 6; | |
g_thres = 75; | |
while ~isempty(mins) | |
l = size(mins,1); | |
for i = 1:l | |
x = mins(i,1); | |
y = mins(i,2); | |
v = img(x,y); | |
if x==1 || y==1 || x==285 || y==378 | |
continue | |
end | |
for j=-1:1 | |
for k=-1:1 | |
if (abs(img(x+j,y+k)-v) < thres) && (mask(x+j,y+k)==0) && img(x+j,y+k)>g_thres | |
mins = [mins;x+j,y+k]; | |
mask(x+j,y+k) = 1; | |
end | |
end | |
end | |
end | |
mins(1:l,:) = []; | |
end | |
subplot(2,2,1); | |
imshow(mask); | |
subplot(2,2,2); | |
ic = imclose(mask,ones(3,3)); | |
imshow(ic); | |
subplot(2,2,3); | |
res = img; | |
res(ic==0)=0; | |
imshow(res); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 读取图片 | |
img = imread('松鼠.jpg'); | |
gray = rgb2gray(img); | |
%% 处理 | |
mask=zeros(size(gray)); | |
mins = [2*ones(1,400),226*ones(1,400),1:227,1:227;1:400,1:400,2*ones(1,227),399*ones(1,227)]'; | |
thres = 4; | |
g_thres = 0; | |
while ~isempty(mins) | |
l = size(mins,1); | |
for i = 1:l | |
x = mins(i,1); | |
y = mins(i,2); | |
v = gray(x,y); | |
if x==1 || y==1 || x==size(gray,1) || y==size(gray,2) | |
continue | |
end | |
for j=-1:1 | |
for k=-1:1 | |
if (abs(gray(x+j,y+k)-v) < thres) && (mask(x+j,y+k)==0) && gray(x+j,y+k)>g_thres | |
mins = [mins;x+j,y+k]; | |
mask(x+j,y+k) = 1; | |
end | |
end | |
end | |
end | |
mins(1:l,:) = []; | |
end | |
figure; | |
subplot(2,2,1); | |
imshow(mask); | |
subplot(2,2,2); | |
ic = imclose(mask,ones(3,3)); | |
imshow(ic); | |
subplot(2,2,3); | |
res = gray; | |
res(ic==1)=0; | |
imshow(res); | |
mask = mask==0; | |
%% 删除宽度过大部分 | |
ys = sum(mask,2); | |
figure; | |
plot(ys); | |
ys(ys>size(img,2)/2) = 0; | |
mask(ys==0,:)=0; | |
figure; | |
imshow(mask); | |
%% 选取最大连通分量 | |
tmp = imopen(mask,ones(6,3)); | |
L = bwlabel(tmp); | |
areas = regionprops(L, 'area'); | |
areas = [areas.Area]; | |
index = find(areas==max(areas)); | |
final=L==index; | |
figure; | |
tmp = gray; | |
tmp(final==0) = 0; | |
imshow(tmp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment