Wednesday, July 20, 2016

image-gradients

Image Gradients

By studying this article, one can understand the meaning of image gradient and role of gradient values in edge detection. The gradient of pixel represents the change of the intensity values in both X and Y directions.

Gradient of image in X – Direction

  • The gradient of image in X-direction is calculated by computing change of each pixel with respect to X-direction.
  • Change of each pixel in X- direction is calculated as follows
    P(x, y) = P(x, y+1) – P(x, y-1) (right pixel – left pixel)
% Matlab program to find X-gradient image for the given image
clear all; clc;

%read a color image and convert it to gray
img = imread('E:images/face.jpg');
img = rgb2gray(img);
 
%find the size of image
[rows,cols] = size(img);
 
%converting image into double for performing operations
img=double(img);
 
%initialization xgradient matrix 'xgrad' with image 'img'
xgrad=img;
 
%calculating gradient image in X-direction
for i=1:rows
    for j=2:cols-1
        xgrad(i,j) = abs(img(i,j+1) - img(i,j-1));  
       
    end
end
 
 
figure, imshow(uint8(img)),title('given image');
figure, imshow(uint8(xgrad+22)), title('X-gradient image');

The output of the above code is shown below:

Gradient of image in Y – Direction

  • The gradient of image in Y-direction is calculated by computing change of each pixel with respect to Y-direction.
  • Change of each pixel in Y-direction is calculated as follows
    P(x, y) = P(x+1, y) – P(x-1, y) (upper pixel – lower pixel)
% Matlab program to find Y-gradient image for the given image
clear all; clc;

%read a color image and convert it to gray
img = imread('E:images/face.jpg');
img = rgb2gray(img);
 
%find the size of image
[rows,cols] = size(img);
 
%converting image into double for performing operations
img=double(img);
 
%initialization ygradient matrix 'ygrad' with image 'img'
ygrad=img;
 
%calculating gradient image in X-direction
for i=2:rows-1
    for j=1:cols
        ygrad(i,j) = abs(img(i+1,j) - img(i-1,j));  
       
    end
end
 
 
figure, imshow(uint8(img)),title('given image');
figure, imshow(uint8(ygrad+22)), title('Y-gradient image');

The output of the above code is shown below:

Popular Articles:

1. matlab-cropping-binary-image-algorithm

Objective of the Program: Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.

2. Working-with-ROI-of-image-using-Matlab

Objective of the Program:The part of the image, on which you have interest to work out, is called Region of Interest (ROI). In another words, selected subset of image is called ROI. In some contexts, you want to apply operations on ROI of image rather than the entire image. To achieve this, generally people extract the ROI from the image, store it in another variable and then apply operations on ROI. If you want to apply your operations on ROI without extracting from the image, it is bit difficult. This article will explain the performing the operations on ROI without extracting from the image. In this context, the ROI part of image is affected rather than the entire image.

3. Insertion Sort Matlab Program:

Insertion sort is very simple algorithm and easy to implement. It works well when the input size is less. If the input size is more, insertion sort is not efficient(quick sort or merge sort works good when the input size is big).

4 comments:

Python-environment-for-deep-learning-in-windows

Python is increasingly becoming a popular programming language for machine learning and deep learning. If you want to use python for train...