2 views (last 30 days)

function myThirdAssignment(I,WindowSize,K0,K1,K2)

if size(I,3)==1

x= im2double(imread(I));

[r,c]=size(x);

elseif size(I,3)==3

x= im2double(imread(I));

gray1=rgb2gray(x);

[r,c]=size(gray1);

figure(1); imshow(x); title('ORIGINAL IMAGE');

imwrite(x,'OriginalImage.bmp.bmp'); %writing data of original image in to current directory.

% GIVING B THE SAME ROWS AND COLUMS AS THE ORIGINAL IMAGE

B=zeros(r,c);

% CALCULATING CEIL & FLOOR VALUES TO MAKE PROGRAM MORE GENERAL

p= ceil((Size / 2)); %3/2= 1.5=2

s= floor((Size / 2)); %3/2=1.5=1

for i=p:r-s

for j=p:c-s

sum=0;

temp=0;

% CHECKING IF BOTH CRITERIAS ARE FULFILLED

%--------------------------------------------

if avg <= K0*(meanIntensity) && (K1*(stdG) <= std) && (std <= K2*(stdG))

% only enhance an area of defined window size when its mean/average is

% lesser than or equal to the mean of the image by some constant

% K0 AND its standard deviation is lesser than the value of the

% standard deviation by a constant K2 and greater than the

% global standard deviation by a constant K1.

B(i,j)= 2*x(i,j);

else

B(i,j)= x(i,j);

end

%--------------------------------------------

end

end

%RGB = cat(3, B, B, B);

figure(2);imshow(B); title('IMAGE AFTER LOCAL HISTOGRAM EQUALIZATION');

imwrite(B,'enhancedImage.jpeg.jpeg'); %writing data of enhanced image in to current directory

end

Im attaching the original and resultant images here:

Original image:

Resultant/enhanced image:

Image Analyst
on 9 Jul 2020

For the first question:

When you do this:

x= im2double(imread(I));

[r,c]=size(x);

x is still an RGB image that you later go on to use instead of gray1. And c is not the number of columns in the image, it's the number of columns times the number of color channels. Why? See Steve's article: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/ You should never use size like that for an image. You should always do it like this when you're unsure if the image might be RGB or grayscale:

[rows, columns, numberOfColorChannels] = size(rgbImage);

So when you're looping over c, you're actually looping over three times as much as you thought you were. So when you do this:

B(i,j)= 2*x(i,j);

you're adding columns because the badly-named j is 3 times as big as you thought. Here, let's take an example. Let's instantiate B as a 2-by-2 matrix:

>> B = rand(2,2)

B =

0.96489 0.97059

0.15761 0.95717

and now let's set B(2, 6) and see what happens to B:

>> B(2, 6) = 100

B =

0.96489 0.97059 0 0 0 0

0.15761 0.95717 0 0 0 100

See? It made B three times as wide. That's why you're seeing B three times as wide.

---------------------------------------------------------------------------------------------------------------------

Now for the secnod question (why is B grayscale?):

B is grayscale because you instantiated it with 2 dimensions, not three.

% GIVING B THE SAME ROWS AND COLUMS AS THE ORIGINAL IMAGE

B=zeros(r,c);

Gray scale images are 2-D while RGB images are 3-D. You did not make B an RGB image, like you'd get if you did this:

[rows, columns, numberOfColorChannels] = size(x);

B = zeros(rows, columns, numberOfColorChannels, 'uint8');

so you got a grayscale image.

Image Analyst
on 9 Jul 2020

If you want floating point, don't tell it to make zeros 'uint8'.

If your image is floating point, it will need to be in the range 0-1 to see it, unless you use [] in imshow():

imshow(B, []); % Scales max to min

otherwise anything above 1 will appear white and anything below 0 will appear black.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.