image - imshow() not showing changed pixel values -
i converted rgb iamge gray , binary. wanted white pixels of binary image replaced pixel value of gray image. though command window shows 1s replaced gray pixel value same not reflected in image. binary image (bw) , new image (newbw) looks exactly. why ?
clc;clear all;close all; = imread('c:\users\asus\documents\academics 2014 (sem 7)\dip\matlabtask\im1.jpg'); igray = rgb2gray(i); bw = im2bw(igray); [m,n]=size(bw); newbw = zeros(m,n); i=1:m j=1:n if bw(i,j)==1 newbw(i,j)=igray(i,j); else newbw(i,j)=bw(i,j); end end end subplot(311),imshow(igray),subplot(312),imshow(bw),subplot(313),imshow(newbw)
the reason why because when creating new blank image, automatically created double
type. when doing imshow
, if provide double
type image, dynamic range of pixel intensities expected between [0,1]
0
black , 1
white. less 0 (negative) shown black, , greater 1 shown white.
because surely not case in image, , a lot of values going > 1
, output of either black or white. suspect image of type uint8
, , dynamic range between [0,255]
.
as such, need cast
output image of same type input image. once this, should able see gray values displayed properly. have change newbw
statement variable of same class input image. in other words:
newbw = zeros(m,n,class(igray));
your code should work. fwiw, you're not first 1 encounter problem. of questions answer when using imshow
due fact people forget putting in image of type double
, type uint*
behave differently.
minor note
for efficiency purposes, not use for
loop. can achieve above behaviour using indexing boolean
array. such, replace for
loop statement:
newbw(bw) = igray(bw);
whichever locations in bw
true or logical 1, copy locations igray
on newbw
.
Comments
Post a Comment