2012年10月17日 星期三

matlab 將彩色圖片轉為 binary image


將彩色圖片根據 threshold 轉為 binary image
主程式:



clear all;
close all;

strResponse = input('please input image file (ex.bananafruit.jpg): ', 's');
[X, map]=imread(strResponse);
strResponse = input('please input threshold number: ', 's');
threshold=str2double(strResponse);

if ~ismatrix(X)
    grayX = rgb2gray(X);
    BW = img2bw(grayX,threshold);
else
    BW=X;
end

figure(1);
imshow(X);
title('The original color image');

figure(2);
imshow(BW);
title('The binary image');



副程式:


function [ BW ] = img2bw(X,threshold)
%IMG2BW Summary of this function goes here
%   Detailed explanation goes here

BW=[];
for i=1:size(X,1)
    for j=1:size(X,2)
        if X(i,j)>=threshold
            BW(i,j)=1;
        else
            BW(i,j)=0;
        end
     
    end
end
end





沒有留言:

張貼留言