# Thilina's Blog

## Contrast Stretching for Gray Scale images with MATLAB

When studying “Introduction to Image Processing and Machine vision” and “Bio Medical Engineering and Instrumentation” for the end semester exam, I read a small section related to those two modules which can be easily implemented using MATLAB. “Contrast Stretching” is a simple piecewise linear transformation function which expands the range of intensity of the image to user defined span.

Simple mathematical illustration of the process is as below.

Considering Line L1;

Considering Line L2;

Considering Line L3;

2011 March 23 Posted by | Image Processing, MATLAB | 11 Comments

## MATLAB GUI programming (code level) 1.0 – Paint Effect for Images

When surfing in facebook I saw a lot of profile pictures which is modified as painted images. Rather than using the facebook apps I thought to write my own program to do it for me. In this article I’ll try to share my experience on GUI programming in MATLAB without MATLAB GUI guide and the construction of the paint brush effect code.

When studying the image with paint brush effect I noticed that the image colour levels are now limited to a low number, which is most likely a one colour intensity to represent a set of colour. By keeping this in mind I coded a small test program as below.

```clc, clear all, close all;
rateR = 40;
rateG = 60;
rateB = 50;
ima(:,:,1) = cast(rateR*round(im(:,:,1)/rateR),'uint8');
ima(:,:,2) = cast(rateG*round(im(:,:,2)/rateG),'uint8');
ima(:,:,3) = cast(rateB*round(im(:,:,3)/rateB),'uint8');
imshow(ima);
imhisto(:,1) = imhist(ima(:,:,1));
imhisto(:,2) = imhist(ima(:,:,2));
imhisto(:,3) = imhist(ima(:,:,3));
figure, plot(imhisto);
```

Hopefully it got worked, what I have done is,

2011 January 25 Posted by | Image Processing, MATLAB | 7 Comments

## Chroma Keying MATLAB Implementation 4.0

Hopefully this will be my final article on chroma keying, in this article I will try to address the key disadvantages and faults identified in my previous posts by myself. In my previous chroma keying functions I only segmented a region which has the luminance level of [x, 255] or [0, x]. But on working with function I realised that some colour details in images also has segmented out as a background mask.

With this experience I modified my function to segment out a small colour band which is most likely acts as the background of the foreground image. What I have done is obtain the mean colour (say mR, mG, mB) and its maximum deviation (say sR, sG, sB) on user defined points on the foreground image and then segment the foreground image using the colour band obtained from the above result. Now the modified segemented colour band will be [mR-sR:mR+sR, mG-sG:mG+sG, mB-sB:mB+Sb] which will results in less colour distortion in the resulting image.

2011 January 10

## Colour Picker for MATLAB Image Processing

When dealing with applications such as image segmentation using colour thresholding, colour replacement I personally faced a problem in dealing with picking an average colour in a set of points in an image or a region in an image. So finally I came up with a small solution which worked on me in my image processing applications on MATLAB. From this article I am going to explain how to make a simple colour picker for small scale MATLAB applications.

## Mean Colour of Discrete points

For picking the points (as discrete points) I used the MATLAB built-in function ‘getpts’. The general for of this function is;

[x y] = getrect(fig); where

Metrics x and y will contain the coordinates of column and row points selected by the mouse click. Since the x and y are in double data format we need to convert them to int16 data type for further operations. A right click or the key command <ENTER> must be press to notify the end of picking points for this operation and you can remove last picked points by pressing <BACKSPACE>. And the fig is the image handle which we are used to display the image.

After selecting the points a small for loop can be use to get the sum in-order to obtain the mean colour of the selected points of the image.

`   1:  for i = 1:size(x,1)`
`   2:          if((0<x(i))&&(x(i)<=r)&&(0<y(i))&&(y(i)<=c))`
`   3:          pts = pts + 1;`
`   4:          sum = sum + [im(x(i),y(i),1) im(x(i),y(i),2) im(x(i),y(i),3)];`
`   5:  end`
`   6:  color = cast(sum/pts,'uint8');`

2011 January 4 Posted by | Image Processing, MATLAB | 5 Comments

## Chroma Keying MATLAB Implementation 3.0

In my previous articles on chroma key implementation on MATLAB (Chroma Keying MATLAB Implementation 1.0 and 2.0), I explained how to key two images using a simple image segmentation method called ‘Image thresholding’ and to key an image and a video. In this article I’ll try to extend above to implement on MATLAB simulink.

There are two approaches to implement these on MATLAB, first is to implement chroma key using the MATLAB embedded function block and the second is to implement above functionality using basic simulink blocks.

### Using MATLAB simulink embedded function

For the implementation arrange the Simulink blocks as below. Image from file block can be found in simulink video and image processing tool box under the source category and Video viewer under sink category. Matrix operation blocks can be found in Signal processing tool box and the Embedded MATLAB function can be found under Simulink user defined functions. And set its parameters as described below.

2010 December 29

## Digital Image Zooming – Sample Codes on MATLAB

Image zooming or image scaling is another basic operation in computer graphics. In this article I am trying to describe how to implement the image zooming or scaling from our own.  Image resize algorithms are try to interpolate the suitable image intensity values for the pixels of resized image which does not directly mapped to its original image. There are three major algorithms for image resizing,

You can read more about these methods in Wikipedia and from this web site and also by typing ‘doc imresize’ on MATLAB command prompt which is also the default operation for image resizing. I’ll try to briefly describe what’s going on in the each algorithm and a way to implement it on MATLAB.

### Nearest Neighbour Interpolation

In the nearest neighbour algorithm, the intensity value for the point v(x,y is assigned to the nearest neighbouring pixel intensity f(x,y)which is the mapped pixel of the original image. The logic behind the approximation is as the equation below.

2010 December 24 Posted by | Image Processing, MATLAB | 79 Comments

## Chroma Keying MATLAB Implementation 2.0

In my previous article on chroma key implementation on MATLAB (Chroma Keying MATLAB Implementation 1.0), I explained how to key two images using a simple image segmentation method called ‘Image thresholding’. In this article I’ll try to extend above result to key a foreground video with a background image. Please note that this implementation can be use to process a video after saving it or post processing manner.

During this implementation I used an *.avi video file ‘movie.avi’, which has the chroma keying properties; a video taken in front of a blue screen as my foreground video and ‘background.jpg’ as my background image.

Opening a movie to MATLAB workspace can done by using the command ‘mmreader ()’. The command creates a multimedia reader object on MATLAB workspace. You can read more on this command by typing ‘doc mmreader’ on MATLAB command prompt.

2010 December 16

## Chroma Keying MATLAB Implementation 1.0

In my previous article I discussed the major concepts on chroma keying. In this article I will discuss a method for use chroma keying principles to key two images in to a single image. For the implementation we need two images which can use as background and foreground images. I select ‘background.jpg’ and ‘foreground.jpg’ as my background and foreground images for the keying operation. Note that the foreground is the image which you stay in front of that special reflecting screen.

We can add those two images in to MATLAB workspace by using following commands.

As we can see the foreground major colour is blue, therefore we have to mask the corresponding blue colour from the image. In order to perform this operation, we need to create a mask with colour thresholds. The ‘key’ variable contains the corresponding RGB colours for the above colour filtering operation.

2010 December 3

## Interpolation Based method for Directional Derivative Calculation in Image Processing 1.0

Directional derivative calculation can be done by using a number of kernels in image processing. Most of them are fixed and can only use in single direction. This article attempts to discuss and use a geometrical model to as a solution for this situation.

Considering the gradient calculation in image processing, since it is discrete domain, calculations are done by forward and backward differences. Therefore co-efficients of pixels can directly use for built a kernel which can convolve with a given image to determine the gradient.

Consider two pixels f(x,y) and f(xr,y) of an image which are in r direction with |r| distance. Then the directional derivative can be written as;

 Model discussed below attempts to specify a virtual pixel D(xr,yr) which can use to determined the directional derivative along the vector r. Consider following 9 pixels. Assume we need to calculate the directional derivative through Q in θ direction with x axis.

2010 November 25 Posted by | Image Processing, MATLAB, Technology | 4 Comments

## Chroma Keying

When we watching TV programs and some movies some times we amazed by seeing the background of the presenter. Some times in front of a fantasy location or with various moving objects etc.

Technology behind this magic is called chroma keying. In chroma keying uses a special kind of back screen which reflects the light to the direction which to the screen. In the camera side as in the picture, around its opening green or blue colour LEDs were set as a light source. Selecting this blue or green colour is due to a special reason that is they are the less related colours to the human complexion. Most of the time this colours are selected as Green (0,255,0) and Blue (0,0,255)  in 8-bit RGB colour pallet.

2010 November 18