contrast enhancement of an image using neighbourhoood

roni picture roni · Dec 2, 2013 · Viewed 7.7k times · Source

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

enter image description here

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:

enter image description hereenter image description hereenter image description hereenter image description here

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.

enter image description hereenter image description hereenter image description hereenter image description here

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.

Answer

Royi picture Royi · Apr 16, 2014

I would solve it in either of the 2 following ways:

  1. Apply the filter all over the image (Using colfilt instead of nlfilter).
    Then multiply the result by the mask.
  2. Extract the required pixels and their neighborhood, forming them in a column form like colfilt works on and apply the filter.

Either way would work.
I think for small photos the first would be faster.