作业要求:
运用图像处理的方法去除上图中背景黑色沁墨印记。
作业初步思路:
将图片简单转化成二值图片
代码如下
i = imread('original.jpg');
gy_i = im2bw(i);
imshow(gy_i)
得到结果如下:
可见仍存在一些未去除的污点,于是思考新步骤如下
新步骤:
通过遍历矩阵,遍历所有像素值=0的像素以及其周围八个像素点,如果周围八个像素点仅有一至两个是黑点,则把其变为白点,代码如下:
row = [-1,0,1];
column = [-1,0,1];
for i = 2:m-1
for j = 2:n-1
if gy_i(i,j) == 0
count = 0;
for r = 1:3
for c = 1:3
if gy_i(i+row(r),j+column(c)) == 0
![count = count + 1;
end
end]
end
if count >=1 && count <=3
gy_i(i,j)=1;
end
end
end
end
figure;imshow(gy_i)
得到的图像如下
相对而言,在纵坐标左上角,以及坐标轴的右上角,比之前稍微好了一些,对其进行重复操作,发现效果并不是特别理想,因此思考采取其他方法加以改进。
新步骤:
通过遍历矩阵,遍历所有像素值=0的像素并找出以及其为重心,以2k+1为边长的正方形,将其包围,如果在正方形的边中没有像素值=1的点,则将该点的像素值变为0,经过几次测试,发现当l=4的时候,该方法的效果较好,代码如下:
new_img = ones(m,n);
l = 4;
column = [-l:l];
for i = l+1:m-l-1
for j = l+1:n-l-1
if gy_i(i,j) == 0
count = 0;
for r = 1:2*l+1
if gy_i(i+l,j+column(c)) == 0
count = count + 1;
end
if gy_i(i-1,j+column(c)) == 0
count = count + 1;
end
if gy_i(i+column(c),j+1) == 0
count = count + 1;
end
if gy_i(i+column(c),j-1) == 0
count = count + 1;
end
if count ~= 0
new_img(i,j)=0;
end
end
end
end
end
figure;imshow(new_img)
得到的图像如下:
可见除了中间的一个较大的点之外,其他的小点基本清除完成。
反思与不足之处:
- 仍然有部分噪声未去除
- 在去除噪声的过程中,也去除了一些必要的点
- 运用了过多的for循环,可能计算速度会比较慢
- 可能可以考虑更多的类似聚类的方法,得以更好的解决
完全代码如下:
i = imread('original.jpg');
gy_i = im2bw(i);
imshow(gy_i)
[m,n] = size(gy_i);
k = 1;
row = [-k:k];
column = [-k:k];
counts = 0;
for i = 2:m-1
for j = 2:n-1
if gy_i(i,j) == 0
count = 0;
for r = 1:3
for c = 1:3
if gy_i(i+row(r),j+column(c)) == 0
count = count + 1;
end
end
end
if count >=1 && count <=3
gy_i(i,j)=1;
counts = counts +1
end
end
end
end
figure;imshow(gy_i)
new_img = ones(m,n);
l = 4;
column = [-l:l];
for i = l+1:m-l-1
for j = l+1:n-l-1
if gy_i(i,j) == 0
count = 0;
for r = 1:2*l+1
if gy_i(i+l,j+column(c)) == 0
count = count + 1;
end
if gy_i(i-1,j+column(c)) == 0
count = count + 1;
end
if gy_i(i+column(c),j+1) == 0
count = count + 1;
end
if gy_i(i+column(c),j-1) == 0
count = count + 1;
end
if count ~= 0
new_img(i,j)=0;
end
end
end
end
end
figure;imshow(new_img)
最后
以上就是外向小伙最近收集整理的关于数字图像处理作业一—— remove the background and enhance the image的全部内容,更多相关数字图像处理作业一——内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复