One of the classic problems is
classifying the images. There will be series of posts on this topic which will be
closely related to machine learning concepts. Though there are different
techniques available in the scientific community and market, let’s start with
the basic method of classification - decision tree. Look at the dataset,
Channel
|
Variance
|
Image
Type
|
Monochrome
|
low
|
BW
|
RGB
|
low
|
BW
|
RGB
|
High
|
Color
|
There is only two types of
classification ‘Black and White’ or ‘Color’
MATLAB CODE:
%Image Classification: BW
and Color Image
%List of 'jpeg' images
fname = dir('*.jpeg');
for k=1:size(fname,1)
%Read the image
Img = imread(fname(k).name);
%Size of the Image
mn=size(Img,1)*size(Img,2);
%Estimate the difference along the
Channels
VarI=std(double(Img),0,3);
BelowT = sum(VarI(:)<=25);
Prob_BT= BelowT/mn;
if(Prob_BT==1)
figure(1),imshow(Img);title('Black and
White Image');
else
figure(2),imshow(Img);title('Color Image');
end
end
The key feature is the ‘Variance’
that ensures how different each value along the Red, Green and Blue Channels at
each pixel location.
For instance, at a particular pixel location if Red channel = 30
, Green channel = 30 and Blue channel = 30 then the variance will be zero which
infers that the image is grayscale or BW. Also, we can tend to infer that the
image as black and white even when the pixel values in these channels are close
to each other.
If we look at the car image and
its corresponding variance map, we can see that the pixel values of the RGB
channels at a particular pixel location varies greatly and thus the variance
map shows this huge difference. We can
say that the picture has been classified correctly.
All the RGB channels have the
same value with respect to each pixel location which indicates that the image
is gray scale. And variance map indicates zero for the whole image. So the Cat
image is BW type.
This zebra image which is a
natural black and white image even though its RGB components are not same but
they are close to each other and the variance map has done a good job to check
the closeness between the RGB channels and it has been classified as BW.
The last one is the swan image.
On a first glance it looks like a black and white image and I would like to
classify it as BW but the variance map tells a different story. We can see the
beak region has a high variance where the RGB components differ greatly while
the remaining part of the image is almost having RGB components close to each
other.
EXAMPLE 2:
Add more features and more
classification to make the data set little bit complicated and messy. We have
obtained the decision tree for the below mentioned data set from the article on ‘DecisionTree – supervised Learner’. Let’s use it to check out the results.
Channel
|
Variance
|
DominantB
|
ImageType
|
Monochrome
|
low
|
High
|
Black
|
Monochrome
|
low
|
low
|
White
|
Monochrome
|
low
|
Medium
|
BW
|
RGB
|
low
|
High
|
Black
|
RGB
|
low
|
low
|
White
|
RGB
|
low
|
Medium
|
BW
|
RGB
|
High
|
High
|
Color
|
RGB
|
High
|
low
|
Color
|
MATLAB CODE:
%Image Classification: BW,
Black Dominant Image, White Dominant Image, Color Image
%List of 'jpeg' images
fname = dir('*.jpeg');
for k = 1:size(fname,1)
%Read the image
Img = imread(fname(k).name);
%size of the image
mn = size(Img,1)*size(Img,2);
%Compute Variance
VarI = std(double(Img),0,3);
BelowT = sum(VarI(:)<=14);
Prob_BT = BelowT/mn; %Number of
values below the Threshold
T1 = (Img(:,:,1) < 100&Img(:,:,2)
< 100&Img(:,:,3) < 100);%Black or gray pixels
T2 = (Img(:,:,1) > 150&Img(:,:,2)
> 150&Img(:,:,3) > 150);%White pixels
T1 = (sum(T1(:)) / mn)*100;
T2 = (sum(T2(:)) / mn)*100;
if((T1/T2) > 0.8 & (T1/T2) <
1.6) %Medium
Criteria for the feature 'DominantB'
figure(1),imshow(Img);title('Image
Type:Black and White Image','FontSize',20);
%When the number of black pixels
are high
elseif(T1 > T2) %High
Criteria for the feature 'DominantB'
%Variance Low
if(Prob_BT > 0.6)
figure(1),imshow(Img);title('Image
Type:Black Dominant Image','FontSize',20);
else
figure(1),imshow(Img);title('Image
Type:Color Image','FontSize',20);
end
%When the number of white pixels
are high
elseif(T2 > T1)%Low
Criteria for the feature 'DominantB'
%Variance Low
if(Prob_BT > 0.6)
figure(1),imshow(Img);title('Image
Type:White Dominant Image','FontSize',20);
else
figure(1),imshow(Img);title('Image
Type:Color Image','FontSize',20);
end
end
%To visualize each image
pause
end
EXPLANATION:
The number of gray or dark pixels
is estimated. Similarly, the number of white or light pixels is estimated. If
the ratio between these two is almost equal then the image is black and white.
If the gray pixels are more than the white pixels then estimate the variance.
Based on the variance map, decide whether the image is color image or black dominant
image. In the similar manner, if the white pixels are more than the gray pixels
then based on the variance map decide whether the image type is color or white
dominant image. The result below illustrates the classification done on the
images and how they were classified.
The images used for the classification were taken from website the https://pixabay.com/
0 comments:
Enjoyed Reading? Share Your Views