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.

4 comments:

  1. This code can be optimized:

    img = imread('path of image');
    [rows,cols] = find(img > 0);
    output = img(min(rows):max(rows), min(cols):max(cols));
    imshow(outputs,[]);

    Hope it helps.

    ReplyDelete
  2. its not working, its showing 3 copies of images in vertical .

    ReplyDelete
    Replies
    1. Your image should be gray image (single channel image). If color image (three channel image), you will get three copies as output.

      Delete

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