Basics of Image Processing to Live Edge Detection

Image processing is an image analysis which we apply to images or video frames to get some information from these images. And to get this information from an image or a video we use computer vision.
Image processing can involve image enhancement, morphology, deblurring, feature extraction, denosing of images etc.

In general there are three kinds of images that are coloured, grey and binary images. Some special kinds of images are multi-spectral images, satellite images or geographical images.
Multi-spectral images are mix of visible and invisible bands like colour images with infrared bands.

Types of Images

Normal images are color, grey and binary images. Binary images are also known as black & white images.
The smallest part of an image is a pixel. To assign a pixel intensity value we require 8 bits (values lie between 0 to 255) for colour and grey images. If we require 8 bits to assign each red, green and blue intensity then the total number of colours we can make will be equal to 2^8 x 2^8 x 2^8.
If you have a TV with 2k colour scheme it means the maximum number of colours which you can see on your TV is 2000 only.
For a binary image we assign pixel intensity value logical 0 or 1. 0 denotes black and 1 denotes white.

Pixel: Pixels are the building blocks of an image. In other words, a pixel is the smallest possible image that can be depicted on your screen.

Colour image is made of combined matrices of red, green and blue. It represents RGB colour bar. There are some other colour bars like HSV, YCbCr and L*A*B.

Grayscale Image: It contains intensity values ranging from a minimum ( absolute black) to a maximum (absolute white) and in between varying shades of gray. Typically, this range is between 0 and 255.

Binary Image: An image that consists of only black and white pixels. Values are 0 and 1 only.

Different types of images

Code to plot the above images in MATLAB. Try to convert your colour image using this code. I’ve used deer.jpg image.

im=imread('deer.jpg'); % read Image
gry=rgb2gray(im); % Convert color in grey image
bw=imbinarize(gry); % convert grey image in binary 
subplot(1,3,1) % plot at index 1
imshow(im,[]) % display color image
title('color image')
subplot(1,3,2) % plot at index 2
imshow(gry,[]); % display gray image
title('grey image')
subplot(1,3,3) % plot at index 3
imshow(bw) % display binary image
title('binary or black & white image')

If you give whos command it will give you information of variables of MATLAB workspace

As you see binary (bw) is logical matrix of size 600×798, grey (gry) is unsigned integer8 matrix and color image im has 3 matrices of red , green and blue, it is also unsigned inter8

There are two methods to convert an image to binary form, default is global.

Global – Calculate global image threshold using Otsu’s method.
Adaptive – Calculate locally adaptive image threshold chosen using local first-order image statistics around each pixel.

If you want to use adaptive then use imbinarize(gry,’adaptive’)

Intensity Plot of red, green and blue Matrices

You can extract individual intensity matrix of red, green and blue respectively using the code given below. And you can combine again all the matrices using cat command.

r=im(:,:,1); % extraction of red matrix, (:,:,1) means (all rows, all columns, and index of first matrix)
g=im(:,:,2); % extraction of green matrix,  (:,:,2) means (all rows, all columns, and index of second matrix)
b=im(:,:,3); % extraction of blue matrix,  (:,:,3) means (all rows, all columns, and index of third matrix)

subplot(2,2,1) % plot at index 1
imshow(r,[]) % display red intensity
title('red intensity')
subplot(2,2,2) % plot at index 2
imshow(g,[]); % display green intensity
title('green intensity')
subplot(2,2,3) % plot at index 3
imshow(b) % display blue intensity
title('binary or black & white image')
subplot(2,2,4) % plot at index 4
imshow(CombinedAllMatrices) % display combined matrices
title('combined image of red, green and blue matrices')

Histogram of Image Data

Histogram of images is necessary to find the number of pixels based on the intensity. You can check the distribution of pixels. If pixels are not uniformly distributed, you can equalize the pixels. It is useful for intensity based image segmentation.

Uniform distribution of pixels means the probability of finding any pixel in any intensity range is equal.

Code for the above figures:

imshow(gry) % original grey image
title('original gray image')
imhist(gry) % histogram of grey image( unuinform distribution of pixels)
title('histogram og original gray image :- unuinform')
I=histeq(gry); % distribute pixels uniformly 
imhist(I) % uniformly distrubuted pixels
title('uniformly distrubuted pixels')
imshow(I) % enhance image after unifrom distribution of pixels
title('enhance image after unifrom distribution')

Edge Detection of an Image

Edge detection is an image processing technique for finding boundaries of objects within images.
There are different algorithms to detect edges as given below.

  • Sobel Finds edges at those points where the gradient of the image is maximum, using the Sobel approximation to the derivative. MATLAB use it as default
  • Prewitt – Finds edges at those points where the gradient of the image is maximum, using the Prewitt approximation to the derivative.
  • Other methods are Roberts, log, zerocross, Canny, approxcanny.

If I have to use the above any methods, except sobel, then I will use edge(‘image name’, any one method as above’) ,
foe example, edge(‘deer.jpg’,’Sobel’)

Original vs edge image

Code for edge detection:

im=imread('deer.jpg') % read color image
imshow(im) % orginal image display
title('original image')
gry=rgb2gray(im); % convert in gray
edg=edge(I,'log'); % log metod	
imshow(edg) % display edge
title('edge detected image')

You can’t detect edges in a colour image directly because at every point intensity changes drastically and it has 3 matrices.

Live Edge Detection

The code which we applied above to detect the edge in a single image, the same code we will apply for the live edge detection. Either you are doing image processing or video processing, first apply your code to a single image or a single frame of video, then apply to all the images or all the frames of video.

Edge detection with a live camera

Code for the live edge detection:

clear , close all % Clear all variables and close all iamges
webcamlist % check how many camera do you have on your machine
cam=webcam(1); % on webcam
while 1  % for infinity loop
im=cam.snapshot; % take snapshot from camera
title('Live camera')
gry=rgb2gray(im); % convert in gray
edg=edge(gry); % log metod	
imshow(edg) % display edge
title('Live Edge')

Color image detection

It’s a trick , check the code below. If you have any query reply me in the comments below.

Code for colour image detection:

clear, close all
while 1

If you want to interface your code with IP camera you can check my previous post Emotion Detection using CNN a Deep Learning

If you like this post, don’t forget to comment below.

For next part of this article read my next post Identify Objects and Compute Area in an Image using MATLAB

Leave a Reply

Your email address will not be published.