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

whos
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')
CombinedAllMatrices=cat(3,r,g,b);
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:

subplot(2,2,1)
imshow(gry) % original grey image
title('original gray image')
subplot(2,2,2)
imhist(gry) % histogram of grey image( unuinform distribution of pixels)
title('histogram og original gray image :- unuinform')
I=histeq(gry); % distribute pixels uniformly 
subplot(2,2,3)
imhist(I) % uniformly distrubuted pixels
title('uniformly distrubuted pixels')
subplot(2,2,4)
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:

figure
im=imread('deer.jpg') % read color image
subplot(1,2,1)
imshow(im) % orginal image display
title('original image')
gry=rgb2gray(im); % convert in gray
I=histeq(gry);
edg=edge(I,'log'); % log metod	
subplot(1,2,2)
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
subplot(1,2,1)
imshow(im)
title('Live camera')
gry=rgb2gray(im); % convert in gray
edg=edge(gry); % log metod	
subplot(1,2,2)
imshow(edg) % display edge
title('Live Edge')
end

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
cam=webcam(1)
while 1
im=cam.snapshot;
subplot(1,2,1)
imshow(im)
edg=edge(im(:,:,2));
im(:,:,2)=edg*255;
subplot(1,2,2)
imshow(im)
end

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

5 thoughts on “Basics of Image Processing to Live Edge Detection”

  1. You actually make it appear so easy along with your presentation however I in finding this topic to be really one thing which I feel I would by no means understand. It seems too complex and extremely vast for me. I’m taking a look ahead on your next put up, I will attempt to get the hang of it!

  2. Your style is so unique compared to other folks I’ve read stuff from.

    Many thanks for posting when you have the opportunity,
    Guess I will just bookmark this site.

  3. I have read a few just right stuff here. Definitely value bookmarking for revisiting. I surprise how so much attempt you place to create one of these wonderful informative web site.

Leave a Reply

Your email address will not be published.