Object recognition

Characteristic Properties of Images

```RGB = imread('teaset.png');
imshow(RGB)f=gcf();f.name='Color Image';
Image = rgb2gray(RGB);
//figure('name','Gray Level Image');
imshow(Image);
f=gcf();f.name='Gray Level Image';
imshow(Image,jetcolormap(256))f=gcf();f.name='Pseudo Color Image';```

How to calculate and display a histogram

`Histogram=imhist(Image);`
`figure();plot(0:255, Histogram')`
`xgrid(color('black'),1,8)`

Thresholding

A gray level image can be segmented by comparing the gray value with a threshold at each pixel.

`RGB = imread ("teaset.png");`
`Image = rgb2gray(RGB);`
`InvertedImage = uint8(255 * ones(size(Image,1), size(Image,2))) - Image;`
`Histogram=imhist(InvertedImage);`
`figure();plot(0:255, Histogram')`
`xgrid(color('black'),1,8)`

`LogicalImage = im2bw(InvertedImage, 100/255);`
`f1=scf(1);f1.name='Original Image';`
`imshow(Image);`
`f2=scf(2);f2.name='Inverted Image';`
`imshow(InvertedImage);`
`f3=scf(3);f3.name='Result of Thresholding';`
`imshow(LogicalImage);`
`//Calculate Otsu's Global threshold value`
`level = imgraythresh (InvertedImage)`
`LogicalImage = im2bw(InvertedImage, level);`
`f4=scf(4);f4.name='Result of Thresholding by Otsu Method';`
`imshow(LogicalImage);`

Crop images

`//Crop the image`
`CroppedInvertedImage = InvertedImage(100:334,9:621);`
```
```
`//gray level of the threshold for binarization`
`level = imgraythresh (CroppedInvertedImage);`
`graylevel = level*255; //returns 105`
`LogicalImage = im2bw(CroppedInvertedImage, level);`
`//imshow(CroppedInvertedImage)`
`f5=scf(5);f5.name='Segmented Cropped Image';`
`imshow(LogicalImage);`

`Histogram=imhist(CroppedInvertedImage);`
`figure();plot(0:255, Histogram')`
`xgrid(color('black'),1,8)`

Blob Analysis

Objects can be found in a logical image by searching the connected areas of true pixels. The pixels of each connected area are mapped to an integer number greater than zero. All pixels of the same area have the same number whereas pixels belonging to different areas have different numbers. All false pixels are mapped to zero.

```RGB = imread ("teaset.png");
Image = rgb2gray(RGB);
InvertedImage = uint8(255 * ones(size(Image,1), size(Image,2))) - Image;
Threshold=99;
level=Threshold/255;
LogicalImage = im2bw(InvertedImage, level);
[ObjectImage,n] = imlabel(LogicalImage);

f1=scf(1);f1.name='Logical Image';
imshow(LogicalImage);
f2=scf(2);f2.name='Result of Blob Analysis';
imshow(ObjectImage,jetcolormap(n))```

Filtering

Filtering means that a mask is placed on each pixel and a new gray value or logical value is calculated from the pixels below the mask. Objects of interest can be emphasized and irrelevant objects can be removed applying a filter.

```Linear Filtering

--> Image = zeros(8, 8); Image(5:8,:) = 1

Image  =

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

1.   1.   1.   1.   1.   1.   1.   1.

1.   1.   1.   1.   1.   1.   1.   1.

1.   1.   1.   1.   1.   1.   1.   1.

1.   1.   1.   1.   1.   1.   1.   1.

--> Mask = [-1 -1 -1; 0 0 0; 1 1 1] / 3

-0.3333333  -0.3333333  -0.3333333

0.          0.          0.

0.3333333   0.3333333   0.3333333

FilteredImage  =

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

1.   1.   1.   1.   1.   1.   1.   1.

1.   1.   1.   1.   1.   1.   1.   1.

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.```

Median Filtering

```--> Image = zeros(9,9); Image(4:6,4:6) = 1; Image(5,2) = 1

Image  =

​
0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   1.   1.   1.   0.   0.   0.

0.   1.   0.   1.   1.   1.   0.   0.   0.

0.   0.   0.   1.   1.   1.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

--> immedian(Image, 3)

ans  =

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   1.   0.   0.   0.   0.

0.   0.   0.   1.   1.   1.   0.   0.   0.

0.   0.   0.   0.   1.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.

0.   0.   0.   0.   0.   0.   0.   0.   0.```

Morphological Filtering

```--> Image = 0.2 * ones(9,9); Image(:,5) = 1

Image  =

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

--> StructureElement = imcreatese('rect',3,3);

--> StructureElement

StructureElement  =

1  1  1

1  1  1

1  1  1

--> imdilate(Image,StructureElement)

ans  =

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

0.2   0.2   0.2   1.   1.   1.   0.2   0.2   0.2

--> imerode(Image,StructureElement)

ans  =

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

--> imclose(Image,StructureElement)

ans  =

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   1.   0.2   0.2   0.2   0.2

--> imopen(Image,StructureElement)

ans  =

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2

0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2```

How to Detect Objects in Images

```RGB = imread ("teaset.png");
Image = rgb2gray(RGB);
InvertedImage = uint8(255 * ones(size(Image,1), size(Image,2))) - Image;
Threshold=100;
level=Threshold/255;
LogicalImage = im2bw(InvertedImage, level);
StructureElement = imcreatese('rect',21,21);
FilteredLogicalImage = imclose(LogicalImage,StructureElement)
[ObjectImage,n] = imlabel(FilteredLogicalImage);

f1=scf(1);f1.name='Logical Image';
imshow(LogicalImage);
f2=scf(2);f2.name='Filtered Logical Image';
imshow(FilteredLogicalImage,jetcolormap(n))
f3=scf(3);f3.name='Result of Blob Analysis';
imshow(ObjectImage,jetcolormap(n))

[Area, BB] = imblobprop(ObjectImage);
f4=scf(4);f4.name='Result of Blob Analysis';
imshow(RGB);
imrects(BB,[255 0 0]);```

Finally: