Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

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).

Wednesday, July 13, 2016

reading-frames-from-video

reading-frames-from-video and store in cell arry

By studying this, one can understand the logic of reading frames from video and store them into cell array in matlab. I tested this program by using MATLAB 2012b. To run the following code, download the video using this link : download

%Matlab program to read frames from video and store all frames into a cell
%array.
% input -  path of the video
% output - cell array contains all frames in video


clear all; clc;
input = 'E:/videos/crosscut.avi';

%create object of videoReader
vid = VideoReader(input);

%find number of frames in video
no_of_frames = vid.NumberOfFrames;
fprintf('number of frames in video : %d \n', no_of_frames);

%create cell array with size of 'no_of_frames'
output = cell([1,no_of_frames]);

%read frame by frame and store it into a cell array 'output'
for i=1:no_of_frames
    frame = read(vid,i);
    output{i} = frame;
end

%convert all frames in cell array 'output' to gray color
for i=1:no_of_frames
    output{i} = rgb2gray(output{i});
end

%displaying all frames in cell arry 'output'
for i=1:6
    imshow(output{i});
    pause(0.1);
end


Output :

   number of frames in video : 100 

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).

Tuesday, July 12, 2016

Insertion-sort

Insertion Sort Matlab Program

By studying this, one can understand the logic of insertion sort. 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).

% Program for Insertion sort 
% Author : M Naveenkumar , Research Scholar , NIT Trichy, Tamilnadu, India
% n = input size (array size)

clear all; clc;
n = input('Enter n value:');

% create an array (a) with size 'n' and filled with zeros
a(1,n)=0;

% reading 'n' values to array (a)
for i=1:n
    string = 'value:';
    a(1,i) = input(string);
end

disp ('_______________________________________________________________');

for j=2:n
    key = a(1,j);
    
    % insert 'key' into already sorted array a[1.... j-1]
    i = j-1;
    while (i>0 && a(1,i)> key) 
        a(1,i+1) = a(i);
        i = i-1;
    end 
    a(1,i+1) = key;
    
    disp ('.................................................');
    fprintf('At j =%d:\n',j);
    disp(a);
end

disp ('_______________________________________________________________');
disp('sorted elements:');
disp(a);

Output :


Enter n value:6
value:2
value:1
value:9
value:5
value:7
value:8
_______________________________________________________________
.................................................
At j =2:
     1     2     9     5     7     8

.................................................
At j =3:
     1     2     9     5     7     8

.................................................
At j =4:
     1     2     5     9     7     8

.................................................
At j =5:
     1     2     5     7     9     8

.................................................
At j =6:
     1     2     5     7     8     9

_______________________________________________________________
sorted elements:
     1     2     5     7     8     9


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.

Wednesday, March 30, 2016

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

Working with ROI of image using Matlab

M Saravana Mathan MCA
Project Associate
NIT Trichy

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.

I am writing this article to 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.

For example, you want to apply edge detection algorithm on the half of image (example: top of image), study the following example. Let consider the image as shown below.

Here ROI = "Half (Top) of the Image"

% read input image
img=imread('E:/images/input.jpg');

% convert color to gray image if input is color image
img=rgb2gray(img);
subplot(1,3,1);
imshow(img), title('Input image');

% calculating size of the image
[rows,cols]=size(img);
rows = rows/2;

% Apply Canny edge operation
edgeimg=edge(img(1:rows,1:cols),'Canny');
subplot(1,3,2);
imshow(edgeimg), title('Canny operation on Extracted ROI');

% calculating size of the ROI
[row,col]= size(edgeimg);

% edgeimg is in logical values (0,1). converting edgeimg into grayimage
edgeimg=uint8(edgeimg);
for i= 1:row 
     for j=1:col
         if(edgeimg(i,j)==1)
             edgeimg(i,j)=255;
         end
     end
end

% substituting Extracted ROI in Input image
img(1:rows,1:cols)=edgeimg;

subplot(1,3,3);
imshow(img), title('Input Image after substituing ROI');

output:




See also:

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.

Tuesday, February 23, 2016

Matlab-Cropping-binary-image-algorithm-and-program

Matlab

Program for Cropping Binary Image : Matlab Code

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.

Input: Gray or binary image (for example consider the image below)

           

Output: Gray or binary image (as below)

                      

MATLAB Code:


% read the input binary image 

img = imread('path of image');

% calculating size of the image 

[row col] = size(img);

% removing black portion on top side of the image 
  for i = 1:row
    if sum(img(i,:)) > 0
        top = i;
        break
    end
  end

% removing black portion on bottom side of the image 
  for i = row:(-1):1
    if sum(img(i,:)) > 0
        bottom = i;
        break
    end
  end

% removing black portion on left side of the image 
  for i = 1:col
    if sum(img(:,i)) > 0
        left = i;
        break
    end
  end

% removing black portion on right side of the image 
  for i = col:(-1):1
    if sum(img(:,i)) > 0
        right = i;
        break
    end
  end

% output image  
 output = img(top:bottom, left:right);

 imshow(output);



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.

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...