Info

This question is closed. Reopen it to edit or answer.

I examine a grayscale image by dividing it to 50x50 as parts(windows), each part has 3 types of color. I found the avrg of the background type only and need to set dark pixls to this avrg and set other to white. I couldnt fix the errors? Need help.

1 view (last 30 days)
clear all;
clc;
close all;
ImageinRGB = imread('\DSCF0074new3.tif');
I = rgb2gray(ImageinRGB);
figure, imshow(I), title('Grayscale');
A=double(I);
[r c]=size(A);
count = 0;
sumofBacgrounds = 0;
AvgofBackgrounds = 0;
for column = 1: c
checkCol = c - column;
if (checkCol > 50 | checkCol == 50)
for row = 1: r
checkRow = r - row;
if(checkRow > 50 | checkRow == 50)
TestWindow = I(row:row+49,column:column+49);
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<95 & I(rowofWindow,colofWindow)>58)
count = count+1;
sumofBacgrounds =sumofBacgrounds + I(rowofWindow,colofWindow);
AvgofBackgrounds = sumofBacgrounds / count;
end
end
end
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<58)
I(rowofWindow,colofWindow) = AvgofBackgrounds;
elseif (I(rowofWindow,colofWindow)>95)
I(rowofWindow,colofWindow) = 255;
end
end
end
sumofBacgrounds = 0;
AvgofBackgrounds = 0;
else
TestWindow = I(row:row+checkRow,column:column+49);
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<95 & I(rowofWindow,colofWindow)>58)
count = count+1;
sumofBacgrounds =sumofBacgrounds + I(rowofWindow,colofWindow);
AvgofBackgrounds = sumofBacgrounds / count;
end
end
end
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<58)
I(rowofWindow,colofWindow) = AvgofBackgrounds;
elseif (I(rowofWindow,colofWindow)>95)
I(rowofWindow,colofWindow) = 255;
end
end
end
end
sumofBacgrounds = 0;
AvgofBackgrounds = 0;
end
else
for row = 1: r
checkRow = r - row;
if(checkRow > 50 | checkRow == 50)
TestWindow = I(row:row+49,column:column+checkCol);
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<95 & I(rowofWindow,colofWindow)>58)
count = count+1;
sumofBacgrounds =sumofBacgrounds + I(rowofWindow,colofWindow);
AvgofBackgrounds = sumofBacgrounds / count;
end
end
end
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<58)
I(rowofWindow,colofWindow) = AvgofBackgrounds;
elseif (I(rowofWindow,colofWindow)>95)
I(rowofWindow,colofWindow) = 255;
end
end
end
sumofBacgrounds = 0;
AvgofBackgrounds = 0;
else
TestWindow = I(row:row+checkRow,column:column+checkCol);
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<95 & I(rowofWindow,colofWindow)>58)
count = count+1;
sumofBacgrounds =sumofBacgrounds + I(rowofWindow,colofWindow);
AvgofBackgrounds = sumofBacgrounds / count;
end
end
end
for colofWindow = 1: length(TestWindow)
for rowofWindow = 1: size(TestWindow,1)
if (I(rowofWindow,colofWindow)<58)
I(rowofWindow,colofWindow) = AvgofBackgrounds;
elseif (I(rowofWindow,colofWindow)>95)
I(rowofWindow,colofWindow) = 255;
end
end
end
sumofBacgrounds = 0;
AvgofBackgrounds = 0;
end
end
end
end
figure, imshow(I), title('GrayscalePartAfterAdjust');

Answers (1)

Walter Roberson
Walter Roberson on 5 Nov 2016
Change your lines
sumofBacgrounds =sumofBacgrounds + I(rowofWindow,colofWindow);
to
sumofBacgrounds = sumofBacgrounds + double(I(rowofWindow,colofWindow));
  3 Comments
Walter Roberson
Walter Roberson on 5 Nov 2016
I would suggest that you completely rewrite your code to use logical indexing. blockproc() to have your algorithm run on 50 by 50 blocks. For each block,
mask_low = block.data <= 58;
mask_high = block.data >= 95;
mask_background = ~mask_low & ~mask_high;
avgofBackgrounds = mean( double( block.data(mask_background) );
block.data(mask_low) = avgofBackgrounds;
block.data(mask_high) = 255;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!