contrast enhancement of an image using neighbourhoood
Asked Answered
D

1

7

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.

Deviation answered 2/12, 2013 at 6:18 Comment(8)
Your code and your results seem fine at a precursory glance but is there an actual question here? If not, please close the post.Rotten
I am editing the question. My question was not complete. Please do look into my edited question.Deviation
you may consider using the gray-scale version of imdilate and imerode to compute the local max/min of each pixel. It might be slightly faster than nlfilt.Laverne
@Laverne Thanks for your suggestion . I will do that. Could you please relook into my edited question ?Deviation
@RoneyMichael Could you please relook into my edited question ?Deviation
@Deviation - I don't think nlfilt accepts a mask. What you can do is enhance ALL the image (as you have done) and then merge the enhanced result with the original image based on the mask you computed.Laverne
@shai Thanks that is a great idea. Wonder why it did not come to me. Could you please provide me a short code for it ?Deviation
CLAHE may be helpful for you. It is an adaptive histogram equalizer. You can find it in OpenCV.Flabby
G
0

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.

Gerstner answered 16/4, 2014 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.