# Thilina's Blog

## 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');`

## Mean Colour of Continuous Region

When obtaining the mean colour in a continuous region we can use the MATLAB built-in function ‘getpts’. The general for of this function is;

s = getrect(fig); where

Metric s will contain the coordinates of column, row points, height and the width of the rectangle selected by the mouse click. In other words the corners of the selected rectangle will be [ s(1), s(2), s(1)+s(3), s(2)+s(4) ]. When obtaining the mean colour we can simply get the mean of the selected rectangle.

`   1:  box = im(ym:ym+h,xm:xm+w,:);`
`   2:  color = cast(mean(mean(box)),'uint8');`

Finalised Function will be as below.

`   1:  function colour = colourPicker(im,mode)`
`   2:  `
`   3:  if(mode==0)`
`   4:      h = figure('Name','Pick points');`
`   5:      imshow(im);`
`   6:      [y x] = getpts(h);`
`   7:      close(h);`
`   8:      x = cast(x,'uint16');`
`   9:      y = cast(y,'uint16');`
`  10:      [r c p] = size(im);`
`  11:      im = double(im);`
`  12:      pts = 0;`
`  13:      sum = [0 0 0];`
`  14:      for i = 1:size(x,1)`
`  15:          if((0<x(i))&&(x(i)<=r)&&(0<y(i))&&(y(i)<=c))`
`  16:              pts = pts + 1;`
`  17:              sum = sum + [im(x(i),y(i),1) im(x(i),y(i),2) im(x(i),y(i),3)];`
`  18:          end`
`  19:      end`
`  20:      color = cast(sum/pts,'uint8');`
`  21:  `
`  22:  else`
`  23:      h = figure('Name','Pick Region');`
`  24:      imshow(im);`
`  25:      s = getrect();`
`  26:      close(h);`
`  27:      xm = s(1);`
`  28:      ym = s(2);`
`  29:      w = s(3);`
`  30:      h = s(4);`
`  31:      xm = cast(xm,'uint16');`
`  32:      ym = cast(ym,'uint16');`
`  33:      w = cast(w,'uint16');`
`  34:      h = cast(h,'uint16');`
`  35:      box = im(ym:ym+h,xm:xm+w,:);`
`  36:      color = cast(mean(mean(box)),'uint8');`
`  37:  end`
`  38:  `
`  39:  map = ones(60,60,3);`
`  40:  map = cast(map,'uint8');`
`  41:  map(:,:,1) = color(1)*map(:,:,1);`
`  42:  map(:,:,2) = color(2)*map(:,:,2);`
`  43:  map(:,:,3) = color(3)*map(:,:,3);`
`  44:  `
`  45:  figure('Name','Colour')`
`  46:  imshow(map);`
```  47:  colour = color;

```

Application of the above code will be as the video below.

2011 January 4 - Posted by | Image Processing, MATLAB

1. really nice article!

Comment by Dileepa Jayakody | 2011 January 4 | Reply

• Thank you 🙂 🙂

Comment by thilinamees | 2011 January 4 | Reply

2. good work..:-)

Comment by Aaron angel | 2011 May 5 | Reply

• Thank you 🙂

Comment by Thilina S. | 2011 May 5 | Reply

3. hello , Thilinasameera ..