In the field of image processing, we pay more attention to some analysis or description information about the target, such as whether there is a cat in the picture, and what kind of cat is it? Before doing this step, we need to segment the cat in the image. It can be said that image segmentation is the most basic and important step, which will affect the accuracy of subsequent recognition work.
Image segmentation is to divide the image into foreground (target) image and background image. The above picture is an example. If we want to segment the cat, we need to find the boundary of the cat. Therefore, image segmentation can also be implemented with some edge detection algorithms.
1. Image segmentation based on bimodal threshold method
This type of algorithm is only suitable for some cases where it is easy to find the threshold.
clear,clc,close all;
Image=rgb2gray(imread('lotus1.jpg'));
subplot(221),imshow(Image),title('原始图像');
%imhist(Image);
hist1=imhist(Image);
hist2=hist1;
iter=0;
while 1
[is,peak]=Bimodal(hist1);
if is==0
hist2(1)=(hist1(1)*2+hist1(2))/3;
for j=2:255
hist2(j)=(hist1(j-1)+hist1(j)+hist1(j+1))/3;
end
hist2(256)=(hist1(255)+hist1(256)*2)/3;
hist1=hist2;
iter=iter+1;
if iter>1000
break;
end
else
break;
end
end
[trough,pos]=min(hist1(peak(1):peak(2)));
thresh=pos+peak(1);
subplot(222),stem(1:256,hist1,'Marker','none');
hold on
stem([thresh,thresh],[0,trough],'Linewidth',2);
hold off
result=zeros(size(Image));
result(Image>thresh)=1;
subplot(223),imshow(result),title('基于双峰直方图的阈值化');
imwrite(result,'bilotus1.jpg');
function [is,peak]=Bimodal(histgram)
count=0;
for j=2:255
if histgram(j-1)<histgram(j) && histgram(j+1)<histgram(j)
count=count+1;
peak(count)=j;
if count>2
is=0;
return;
end
end
end
if count==2
is=1;
else
is=0;
end
end
2. OSTU method to binarize images
Image=rgb2gray(imread('lotus1.jpg'));
subplot(121),imshow(Image),title('原始图像');
T=graythresh(Image);
result=im2bw(Image,T);
subplot(122),imshow(result),title('OTSU方法二值化图像 ');
3. Edge-based image segmentation
Please refer to the matlab experiment of digital image processing for edge extraction in the image sharpening part (3): spatial filter_Miao Ni's Blog-CSDN Blog
4. Image segmentation based on region growing
clear,clc,close all;
Image=im2double(imread('lotus1.jpg'));
[height,width,channel]=size(Image);
if channel==3
Image=rgb2gray(Image);
end
figure,imshow(Image);
% Image=[1 0 4 6 5 1;1 0 4 6 6 2;0 1 5 5 5 1;0 0 5 6 5 0;0 0 1 6 0 1;1 0 1 2 1 1];
% [height,width,channel]=size(Image);
% figure,imshow(Image);
[seedx,seedy,button] = ginput(1);
seedx=round(seedx);
seedy=round(seedy);
region=zeros(height,width);
region(seedy,seedx)=1;
region_mean=Image(seedy,seedx);
region_num=1;
flag=zeros(height,width);
flag(seedy,seedx)=1;
neighbor=[-1 -1;-1 0;-1 1;0 -1;0 1;1 -1;1 0;1 1];
for k=1:8
y=seedy+neighbor(k,1);
x=seedx+neighbor(k,2);
waiting(k,:)=[y,x];
flag(y,x)=2;
end
pos=1;
len=length(waiting);
while pos<len
len=length(waiting);
current=waiting(pos,:);
pos=pos+1;
pixel=Image(current(1),current(2));
pdist=abs(pixel-region_mean);
if pdist<40/255
region(current(1),current(2))=1;
region_mean=region_mean*region_num+pixel;
region_num=region_num+1;
region_mean=region_mean/region_num;
for k=1:8
newpoint=current+neighbor(k,:);
if newpoint(1)>0 && newpoint(1)<=height && newpoint(2)>0 && newpoint(2)<width && flag(newpoint(1),newpoint(2))==0
waiting(end+1,:)=newpoint;
flag(newpoint(1),newpoint(2))=2;
end
end
end
end
figure,imshow(region),title('区域生长');
imwrite(region,'regiongrow.jpg');