Hi I want to enhance the contrast of an image using the neighbourhood pixel values.
Let the image be considered as u0. Then I want to enhance the image by using the formula
Here, M1 is the minima and M2 is the maxima of u0 among the neighbourhood pixels, Mg is the maximum gray level value of the original image. The neighbourhood taken for my operation is 9X9. uN is the new generated image (contrast enhanced image).
I have tried out the following code but am not sure whether I am correct or not.
%Generate a contrast enhanced image
tic
clear all; close all;
I = imread('4.jpg');
I = imresize(I,[128 128]);
if size(I,3)== 3
P = rgb2gray(uint8(I));
P = double(P);
elseif size(I,3) == 2
P = 0.5.*(double(I(:,:,1))+double(I(:,:,2)));
else
P = double(I);
end
ssize=9;
mg=max(P(:));
f1 = @(x) min(x(:));
m1 = nlfilter(P,[9 9],f1);
f2 = @(x) max(x(:));
m2 = nlfilter(P,[9 9],f2);
P_op=((P-m1)./(m2-m1)).*mg;
subplot(2,1,1),imagesc(P,[0 255]);colormap(gray);axis off;
subplot(2,1,2),imagesc(P_op,[0 255]);colormap(gray);axis off;
toc
Some of the results that I got are being shown below:
Can anyone please check and tell me whether my code is correct or not? I am not so sure myself. Also please tell me whether there is a better way of doing this. Thanks in advance guys.
EDITED question I re-read the work and I **have to apply the sliding window function to only a few specified pixels. **
The pixels that I have to apply on is found out in this method. The initial contour of the image is detected (shown on the images in red). Then a band around the contour is drawn at a specified distance. The sliding window function has to be applied only on those pixels within the narrowband for the original images
I am giving the images, and the initial contours and the band images.
The pixels marked in white are my specified pixels on which the sliding function has to be applied. Can nfilter be applied on such criteria ? Please help. I will clarify further if my question is not correct.
I would solve it in either of the 2 following ways:
colfilt
instead ofnlfilter
).Then multiply the result by the mask.
colfilt
works on and apply the filter.Either way would work.
I think for small photos the first would be faster.