Skip to content

MatanCohe/IntroToImageProcessing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to digital image processing.

About

This project shows ideas that are in my opinion a must for anyone who wish to understand digital image processing.

Requirements:

  • Calculus
  • Linear algebra
  • probability
  • Matlab or Octave with image processing packages

Assignment 1 - Histogram shape.

In these assignment we wrote a function that receives a gray-scale image and returns a gray-scale image obtained by applying the histogram shape algorithm on this image where the histogram of the result image should match the histogram of the destimg.

The algorithm:

In order to implement the histShape algorithm we must first know more about:

  1. Histogram
  2. Cumulative histogram
  3. Histogram equalization

Matlab code

function [ newImg ] = histShape( srcimg, destimg )
%histShape   This function creates new image from srcimg with destimg histogram.
%
%   INPUT PARAMETERS
%       srcimg     -   uint8 matrix
%       destimg    -   uint8 matrix
%
%   OUTPUT PARAMETERS
%       newImg     -   uint8 matrix (same size as  srcimg)
%

  OFFSET = 1;
  [N, M] = size(srcimg);
  cv = computeConversionVector(srcimg, destimg);
  newImg = zeros(size(srcimg), 'uint8');
  for i = 1:N
      for j = 1:M
          newImg(i, j) = cv(srcimg(i, j) + OFFSET); 
      end
  end
end

For the rest of the code click here.

Example:

Source image:

alt text

Destination image:

alt text

% Let srcimg be the Source image
% Let destimg be the image we wish to match it's histogram.
srcimg = imread('darkimg.tiff');
destimg - imread('flatHist.tiff');
output = histShape(srcimg, destimg);
imshow(srcimg); figure; imshow(destimg); figure; imshow(output);

Output image:

alt text

Assignment 2 - Enhancing images

This assignment was about image enhancement from different types of noise from an image. More about the topic can be found here

Directional smoothing

In this part of the assignment we wrote a function which receive an image and add gaussian noise to the image with mean value 0, and var=0.004, and then we've try to enhance the image using the directional smoothing. It is up to us, to choose the right directional filters (size, orientation, and values).

Function signature :

function [eImg,nImg] = gaussEnhance(img)

The algorithm:

equation

Note:

One should pay attention for overfitting the filters to the noised image.

Matlab code

function [newImg] = computeSmoothenImage(img, c1, c2, c3, c4, c5, c6, c7)
% Computes the smoothen image form the img and the image after filters
% by choosing the closest value from filtered image to the original image at
% position i, j.
  [N, M] = size(img);
  newImg = zeros(size(img));
   
  for i = 1:N
    for j = 1:M
      val = img(i, j);
      filtered = [c1(i, j), c2(i, j), c3(i, j), c4(i, j), c5(i, j), c6(i, j), c7(i, j)];
      [_, I] = min(abs(val - filtered));
      newImg(i, j) = filtered(I);
    end
  end
end

Example:

Input image:

alt text

Noised image:

alt text

img = imread('house.tiff');
[eImg,nImg] = gaussEnhance(img)

Enhanced image:

alt text

Shape enhance

In this part we've wrote a function which receive an image and add shaped noise given by the following matrix:

[1,0,0,0,1;1,0,0,0,1;0,1,0,1,0;0,1,0,1,0;0,0,1,0,0]

The function should then try to enhance the image using myMedian filter.

Function signature :

function [eImg,nImg] = shapesEnhance(img)

The algorithm:

Due to the shape of the noise we can apply a median filter of the shape (1, 5) to enhance the image.

Matlab code

function [eImg,nImg] = shapesEnhance(img)
% This function adds shaped noise which is given by the following matrix:
% [1,0,0,0,1;
% 1,0,0,0,1;
% 0,1,0,1,0;
% 0,1,0,1,0;
% 0,0,1,0,0]. 
%
% with density of 0.003.
% then removes the noise with 5 x 1 median filter.
%
%
%   INPUT PARAMETERS
%       img     -   matrix of doubles
%         
%
%   OUTPUT PARAMETERS
%       eImg     -   enhansed image, matrix of doubles
%       nImg     -   noisy image, matrix of doubles
  mask = zeros(size(img));
  mask = imnoise(mask, 'salt & pepper', 0.003);
  noiseMat = [1,0,0,0,1;
              1,0,0,0,1;
              0,1,0,1,0;
              0,1,0,1,0;
              0,0,1,0,0];
  mask = conv2(mask, noiseMat, 'same');
  mask = min(mask, 1);
  nImg = max(mask, img);
  
  eImg = myMedian(nImg, 5, 1);
  
end

Example:

Input image:

alt text

Noised image:

alt text

img = imread('house.tiff');
[eImg,nImg] = shapesEnhance(img)

Enhanced image:

alt text

Assignment 3 - Binary images:

This assignment was about binary images. More about the topic can be found here.

Connected components:

In this part we've wrote a matlab function which finds the connected components in a binary image and returns a matrix with the same size in which each connected component is tagged with a different label. The tags should be sequential.

More about connected components can be found here.

Function signature :

function [newImg] = tagConnectedComponents(img)

The algorithm:

This algorithm considers 4-connectivity.

Let I be the input image size N x M.

  1. Initialize connected component matrix: equation

  2. Calculate equivalence table EQ:

    equation

  3. Create conversion vector:

    equation

  4. Convert the tags of T by the conversion vector.

Matlab code

function [newImg] = tagConnectedComponents(img)
% This function  finds the connected components in a binary image and returns a 
%matrix with the same size in which each connected components is tagged with a 
%different label in a sequential manner.
%
%   INPUT PARAMETERS
%       img     -   binary image
%       
%   OUTPUT PARAMETERS
%       newImg     -   matrix of doubles
%

  pImg = padarray (img, [1, 1], 'pre');
  [tagMatrix] = initTagMatrix(pImg);
  eqTable = makeEqTalbe(tagMatrix);
  cv = makeConvertionVector(eqTable);
  newImg = tagImage(tagMatrix, cv); 
  newImg = newImg(2:end, 2:end);
  
end

Example:

Input image:

alt text

img = imread('binary1.tiff');
output = tagConnectedComponents(img);

Output:

   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   1   1   1   1   1   1   1   1   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   1   1   1   1   1   1   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   0   0   0   0   0   0   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   0   3   3   0   0   0   1   1   0   0   0   0   2   2   2   2   2   2   0   0   0   0   0   0
   0   0   0   0   1   1   0   3   3   0   0   0   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   0   0   0   0   0   0   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   1   1   1   1   1   1   1   1   0   0   0   0   0   2   0   2   0   2   0   0   0   0   0   0
   0   0   0   0   1   1   1   1   1   1   1   1   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   4   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   4   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   9   9   9   9   9   9   9   9   9   9   9   9   9   4   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   9   9   9   9   9   9   9   9   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   5   5   5   5   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   5   0   0   5   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   5   0   0   5   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   0   0   0   5   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   0   0   0   0   0   5   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   9   9   9   9   9   9   9   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0   0   0   0   0   0   0   0   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

Skeletonize:

In this part we've wrote a matlab function which skeletonize objects in a given binary image and returns a new binary image.

Function signature :

function [newImg] = skeletonizeImage(img)

The algorithm:

  1. Calculate distance from background for each pixel.
  2. Mark every index where the pixel value is greater or equal to it's 4-connectivity neighborhood.

Example:

Input image:

alt text

bwImg = imread('binaryRect.tiff');
output = skeletonizeImage(bwImg);

Output:

alt text

Assignment 4 - Edge detection and Hough transform:

Canny edge detector:

In this part of the Assignment we've implemented the Canny edge detector algorithm, more about it can be found here.

Function signature :

function [newImg, tgTeta] = edgeDetect(img)

The algorithm:

The algorithm can be found here

Example:

Input image:

alt text

img = im2double(imread('balls4.tiff'));
output = edgeDetect(img);

Output:

alt text

Circle Hough Transform:

In this part of the Assignment we've implemented the Hough transform algorithm to find circles in an image, more about it can be found here.

Function signature :

function [circles,cImg] = findCircles(img)

The algorithm:

The algorithm can be found here

Example:

Input image:

alt text

img = im2double(imread('balls4.tiff'));
output = edgeDetect(img);

Output:

alt text

Assignment 5 - Fourier transform:

In this assignment we've cleaned images in the frequency domain.

Function signature :

function [cImg1,cImg2,cImg3,cImg4] = fftClean(img1,img2,img3,img4)

The algorithm:

Example:

Inputs:

alt text

alt text

alt text

alt text

fft1 = im2double(imread('fft1.tiff'));
fft2 = im2double(imread('fft2.tiff'));
fft3 = im2double(imread('fft3.tiff'));
fft4 = im2double(imread('fft4.tiff'));

[cImg1,cImg2,cImg3,cImg4] = fftClean(fft1, fft2, fft3, fft4);

Outputs:

alt text

alt text

alt text

alt text

Bonus Assignment :

Write a matlab function which receives 2 gray-scale image and creates a new image, which is consists of tiles of the small image which forms the big image. Each of the small image tiles is processed using the histShape function of Ex1 to match the histogram of the big image in the same location.

Function signature :

function nImg = createTiledImage(bigImage, smallImage)

The algorithm:

  1. Cut the big image to non overlapping parts of the size of the small image.
  2. Match the histogram of the small image to each of parts.
  3. Replace the parts with the matched small image.

Example:

Inputs:

alt text

alt text

Output:

alt text

About

Introductory image processing algorithms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages