# Calculator Image Processing Experiment 5 image segmentation

## 1, Purpose and requirements of the experiment

1. Understand the significance and common methods of image segmentation
2. Master common image segmentation methods

## 2, Experiment related knowledge

Image segmentation is a technology to divide the image into non overlapping regions and extract the target of interest. It is a key step from image processing to image analysis.
Common Matlab functions related to this experiment:
Edge: detect the edge of gray or binary image and return a binary image. 1 pixel is the detected edge and 0 pixel is the non edge
Usage: BW=edge(I, 'sobel', thresh, direction);% I is the test object;
sobel, roberts, prewitt, zerocross, log and canny can be used as edge detection operators;
thresh specifies the threshold. All edges less than the threshold are ignored during detection. The threshold is automatically selected by default;
Direction specifies the direction. The options are horizontal, vertical or both. The operator is used for edge detection in the specified direction

## 3, Experimental content

1. Three edge detection operators, Roberts, Prewitt and Sobel, are used to detect the horizontal, vertical and all directions of the image wire.bmp, and the detection results are displayed in the form of white background and black lines;

(1) Source code

```I=imread('C:\Users\Administrator\Pictures\wire.bmp');
I=rgb2gray(I);%take RGB Convert image or color image to grayscale image
BW1=edge(I,'roberts',0,'both');%Roberts operator
BW2=edge(I,'roberts',0,'horizontal');%Roberts Operator horizontal direction
BW3=edge(I,'roberts',0,'vertical');%Roberts Operator horizontal direction
subplot(2,3,1);imshow(I);title('Original image');
subplot(2,3,4);imshow(BW1);title('Roberts operator');
subplot(2,3,5);imshow(BW2);title('Roberts Operator horizontal direction');
subplot(2,3,6);imshow(BW3);title('Roberts Operator vertical direction');

BW4=edge(I,'prewitt',0,'both');%Prewitt operator
BW5=edge(I,'prewitt',0,'horizontal');%Prewitt Operator horizontal direction
BW6=edge(I,'prewitt',0,'vertical');%Prewitt Operator vertical direction
figure;
subplot(2,3,1);imshow(BW4);title('Prewitt operator');
subplot(2,3,2);imshow(BW5);title('Prewitt Operator horizontal direction');
subplot(2,3,3);imshow(BW6);title('Prewitt Operator vertical direction');

BW7=edge(I,'sobel',0,'both');%Sobel operator
BW8=edge(I,'sobel',0,'horizontal');%Sobel Operator horizontal direction
BW9=edge(I,'sobel',0,'vertical');%Sobel Operator vertical direction
subplot(2,3,4);imshow(BW7);title('Sobel operator');
subplot(2,3,5);imshow(BW8);title('Sobel Operator horizontal direction');
subplot(2,3,6);imshow(BW9);title('Sobel Operator vertical direction');

%Convert to black lines on a white background
figure;
subplot(1,3,1);imshow(reverse_color(BW1));title('Roberts operator');
subplot(1,3,2);imshow(reverse_color(BW4));title('Prewitt operator');
subplot(1,3,3);imshow(reverse_color(BW7));title('Sobel operator');

function [ BW_new ] = reverse_color( BW )
[m,n]=size(BW);
BW_new=zeros(m,n);
for i=1:m
for j=1:n
if BW(i,j)==0
BW_new(i,j)=1;
end
end
end
end
```

(2) Experimental results

2. The manual threshold segmentation method is used to segment the bottle image, display the segmentation results, and analyze its advantages and disadvantages.

(1) Source code

```I=imread('C:\Users\Administrator\Pictures\bottle.tif');
subplot(1,2,1);imshow(I);title('Original image');
subplot(1,2,2);imhist(I);title('histogram');

[m,n]=size(I);
J=zeros(m,n);
for i=1:1:m
for j=1:1:n
if I(i,j)>130
J(i,j)=1;
else
J(i,j)=0;
end
end
end
figure;
imshow(J);
```

(2) Experimental results

(3) Experimental analysis

Advantages: the gray characteristics of the image are directly used, so the calculation is simple, the operation efficiency is high and the speed is fast.
Disadvantages: it is sensitive to noise, has no obvious gray difference and overlapping segmentation of different target gray values, so it needs to be combined with other methods. Appropriate threshold lookup.

3. Process the following figure A to obtain an appearance similar to figure B;

(1) Source code

```I=imread('C:\Users\Administrator\Pictures\test53.png');
I=rgb2gray(I);%take RGB Convert image or color image to grayscale image
subplot(1,3,1);imshow(I);title('Original image');
subplot(1,3,2);imhist(I);title('histogram');

[m,n]=size(I);
J=zeros(m,n);
for i=1:m
for j=1:n
if I(i,j)>110
J(i,j)=1;
else
J(i,j)=0;
end
end
end
subplot(1,3,3);imshow(J);title('Histogram segmentation');
BW=edge(J,'roberts',0,'both');%Roberts operator
figure;
subplot(1,2,1);imshow(BW);title('Roberts operator');
subplot(1,2,2);imshow(reverse_color(BW));title('Black and white conversion');

function [ BW_new ] = reverse_color( BW )
[m,n]=size(BW);
BW_new=zeros(m,n);
for i=1:m
for j=1:n
if BW(i,j)==0
BW_new(i,j)=1;
end
end
end
end
```

(2) Experimental results

(3) Experimental analysis
Firstly, the histogram threshold segmentation method is used to segment the image, then the Roberts operator is used for edge detection, and finally the image is transformed into white background and black line.

Tags: MATLAB

Posted on Mon, 13 Sep 2021 23:00:30 -0400 by aayatoos