You are on page 1of 19

Computer

Science

Digital Image Processing

Dept. of Computer Science

1
Section (3) Digital Image Processing

MATLAB R2013

How To Process Images Using


MatLab ??

2
Section (3) Digital Image Processing

MATLAB R2013

3
Section (3) Digital Image Processing

MATLAB R2013

4
Section (3) Digital Image Processing

MATLAB R2013

Using Size() in loops instead of


512 as in previous example

5
Section (3) Digital Image Processing

MATLAB R2013

Decompose Color Image into 3


Channels (R, G and B)

6
Section (3) Digital Image Processing

Check Your Knowledge

By using MatLab, try to read a color image and then add (100) to each pixel in each layer (channel) and show the
two images, then try to subtract (100 ) from each pixel in each layer and show the two images also.

7
Section (3) Digital Image Processing

MATLAB R2013

8
Section (3) Digital Image Processing

MATLAB R2013

9
Section (3) Digital Image Processing

Three algorithms for RGB to Grayscale Conversion

How do you convert a color image to grayscale?


We have already defined the RGB color images and grayscale images in our tutorial. Now we will convert a
color image into a grayscale image. There are a lot of methods to convert it. Both has their own characteristics.
But we will see three methods only:

1. Average method
2. Lightness (Desaturation) Method
3. Weighted method or luminosity method

10
Section (3) Digital Image Processing

Three algorithms for RGB to Grayscale Conversion (Cont’d)


 Average method is the most simple one. You just have to take the average of three colors. Since its an RGB
image , so it means that you have add R with G with B and then divide it by 3 to get your desired grayscale
image. Its done in this way:
Grayscale = (R + G + B ) / 3 = 0.333*R + 0.333*G + 0.333*B

Explanation
There is one thing to be sure , that something happens to the original image. It means that our average method
works. But the results were not as expected. We wanted to convert the image into a grayscale , but this
turned out to be a rather black image.
11
Section (3) Digital Image Processing

This method is the most boring. “Averaging” is the most common grayscale conversion routine, and it works
like this:
Gray = (Red + Green + Blue) / 3

It is fast and simple algorithm for rookie programmers. This formula generates a reasonably nice grayscale
equivalent. while this formula is fast and simple, it does a poor job of representing shades of gray relative to the
way humans perceive luminosity (brightness). For that, we need something a bit more complex.

12
Section (3) Digital Image Processing

13
Section (3) Digital Image Processing

Three algorithms for RGB to Grayscale Conversion (Cont’d)


 The lightness (Desaturation) method averages the most prominent and least prominent colors; A pixel can
be desaturated by finding the midpoint between the maximum of (R, G, B) and the minimum of (R, G, B),
like so:
Grayscale=(max([R, G, B]) + min([R, G, B])) / 2.
Assignment # 1

Desaturation results in a flatter, softer grayscale image. Desaturation looks like the kind of grayscale photo you
might take with a cheap point-and-shoot camera.

14
Section (3) Digital Image Processing

Three algorithms for RGB to Grayscale Conversion (Cont’d)


 The Weighted or Luminosity method is a more sophisticated version of the average method. It also
averages the values, but it forms a weighted average.
Grayscale=((0.3 * R) + (0.59 * G) + (0.11 * B)).
Assignment # 2

Explanation
Because humans do not perceive all colors equally, the “average method” of grayscale conversion is
inaccurate. Instead of treating red, green, and blue light equally, a good grayscale conversion will weight
each color based on how the human eye perceives it. As you can see here , that the image has now been
properly converted to grayscale, and the image is more brighter.
15
Section (3) Digital Image Processing

Converting GrayScale Image Into Black and White Image


(Thresholding)

Thresholding is the simplest method of image segmentation (As will be seen later). It can be used to create
binary images from a grayscale image.

The simplest thresholding methods replace each pixel in an image with a black pixel if the image intensity ( i )
is less than some fixed constant T (that is, i<T), or a white pixel if the image intensity is greater than that
constant.

Default value for the constant T is (T = 128)

0
1

Which means if the pixel value in the Grayscale image is less than 128 it will be converted into black (0)
otherwise it will be converted into White (1).

16
Section (3) Digital Image Processing

17
Section (3) Digital Image Processing

Assignment # 3

Write a MatLab code that generates the following grayscale (one channel) image.
Hint : (0-black , 255 – white)

18
Computer
Science

End Of Section (3)

19

You might also like