[数字图像处理]Spatial Transforms and Filtering实验报告

Introduction:

Through this LAB, I have known the principle and formula of histogram equalization, histogram matching, local histogram equalization and use histogram equalization and histogram matching to process the figure. And I try to reduce the noise in the figure by applying the medium filter method.

Lab results & Analysis:

  1. Implement the histogram equalization to the input images:
  1. Introduction of the histogram equalization algorithm:

Consider a discrete grayscale image {x} and let ni be the number of occurrences of gray level i. The probability of an occurrence of a pixel of level i in the image is

L being the total number of gray levels in the image (typically 256), n being the total number of pixels in the image, and px(i) being in fact the image's histogram for pixel value i, normalized to [0,1].

Let us also define the cumulative distribution function corresponding to px as

which is also the image's accumulated normalized histogram.

 

We would like to create a transformation of the form y = T(x) to produce a new image {y}, with a flat histogram. Such an image would have a linearized cumulative distribution function (CDF) across the value range, i.e.

for some constant K. The properties of the CDF allow us to perform such a transform (see Inverse distribution function); it is defined as

where k is in the range [0,L]). Notice that T maps the levels into the range [0,1], since we used a normalized histogram of {x}. In order to map the values back into their original range, the following simple transformation needs to be applied on the result:

  1. Pseudocode:

1. Read the standard image into a grayscale image to obtain the cumulative histogram

2. Read the original image and turn it into a grayscale image to obtain the cumulative histogram

3. Compare each gray level and get the mapping relation: the method finds the two points closest to the cumulative histogram

4. The original grayscale is mapped to the new grayscale

  1. Matlab codes:

function [OutputImage, OutputHist, InputHist]=HistEqu_11711118(InputImage)

OutputImage = zeros(size(InputImage));

[row,col] = size(InputImage);

cdf = zeros(256,1);

h = zeros(256,1);

InputHist = imhist(InputImage,256);

% calculate cdf

for g = 1:256

    cdf(g) = 0;

    for i=1:1:g

           cdf(g) = cdf(g) + InputHist(i);

    end

end

%nomorlize

for k = 1:256

    h(k) = (cdf(k)-1)/((row*col)-1) * 255;

end

for k = 1:256

    h(k) = round(h(k)) + 1;

end

% remap to the new image

for r = 1:row

    for c = 1:col

        idx = (InputImage(r,c)/(256/256)) + 1;

        OutputImage(r,c) = h(idx);

    end

end

 

OutputImage = uint8(OutputImage);

OutputHist = imhist(OutputImage);

end

 

  1. The output figure of the program:

Figure 1 the brighter case

Figure 2 the darker case

 

 

  1. Analysis and conclusions:

This method usually increases the global contrast of many images, especially when the usable data of the image is represented by close contrast values. Through this adjustment, the intensities can be better distributed on the histogram. This allows for areas of lower local contrast to gain a higher contrast. Histogram equalization accomplishes this by effectively spreading out the most frequent intensity values.

  1. The Advantages and disadvantages of histogram equalization:
  1. Advantages: The method is useful in images with backgrounds and foregrounds that are both bright or both dark. In particular, the method can lead to better views of bone structure in x-ray images, and to better detail in photographs that are over or under-exposed. A key advantage of the method is that it is a fairly straightforward technique and an invertible operator. So, in theory, if the histogram equalization function is known, then the original histogram can be recovered.
  2. Disadvantages: A disadvantage of the method is that it is indiscriminate. It may increase the contrast of background noise, while decreasing the usable signal.

 

  1. Use histogram matching to process a grey scale image:
  1. Introduction of the histogram matching algorithm:

Histogram matching or histogram specification is the transformation of an image so that its histogram matches a specified histogram. It is a special case in which the specified histogram is uniformly distributed.

The proof of the algorithm:

We have known:

Then we define a random variable, z, makes it have character like:

Then we can find that:

And use the differential and integral calculus, we can get:

 

  1. Matlab codes:

function [OutputImage1, OutputHist1, InputHist1]=HistMatch_11711118(InputImage,SpecHist)

[OutputImage]=HistEqu_11711118(InputImage);

imRef   = OutputImage;

hist    = imhist(InputImage);                % Compute histograms

histRef = imhist(imRef);

cdf     = cumsum(hist) / numel(InputImage);  % Compute CDFs

cdfRef  = cumsum(histRef) / numel(imRef);

 

% Compute the mapping

M   = zeros(1,256);

for idx = 1 : 256

    [tmp,ind] = min(abs(cdf(idx) - cdfRef));

    M(idx)    = ind-1;

end

 

% Now apply the mapping to get first image to make

% the image look like the distribution of the second image

imMatch = M(double(InputImage)+1);

end

  1. The output figure of the program:

Figure 3 histogram matching output figure (1)

Figure 4 histogram matching output figure (2)

 

  1. Analysis and conclusions:

By observing the resulting image, we can clearly see that we have obtained a specific histogram that represents more image details. And use different histogram specification, we can get different output figure. Like figure3 is use the output figure of histogram equalization.

 

  1. Use local histogram equalization to process a grey scale image:

1)Introduction of the local histogram equalization algorithm:

Local histogram equalization algorithm, also known as sub-block histogram equalization algorithm, is classified according to the degree of overlap of the equalized sub-blocks, which can be divided into three types: non-overlapping sub-block, overlapping sub-block and partially overlapping sub-block. The following is a brief introduction of them:

Non - overlapping subblock equalization algorithm:

The algorithm divides the input image into a series of non-overlapping sub-blocks and performs independent histogram equalization for each sub-block. Its advantage is that the contrast of local details of the image can be fully enhanced, but its disadvantage is that the histogram equalization function of each sub-block is quite different, so it is difficult to avoid block effect in the output image.

Subblock overlapping equalization algorithm:

The algorithm defines a rectangular sub-block on the input image, and USES the histogram information of the sub-block image to move the sub-block center pixel by pixel and repeat the above process until all pixels of the input image are traversed (the method is similar to the process of Niblick binarization).This method not only makes the local details of the image get sufficient contrast enhancement, but also eliminates the block effect. Since the total number of subblock equalization is equal to the total number of pixels in the input image, the efficiency of the algorithm is low.

Subblock partially overlapping equalization algorithm:

The differences between this method (POSHE) and the subblock overlapping method are as follows:

(1) the subblock is not moved pixel by pixel, but the moving step is taken as a fraction of the size of the subblock.

(2) the grayscale conversion function of subblock equalization is used not only to map the grayscale value of the pixel in the center of the subblock, but also to map the grayscale value of all pixels in the subblock.

(3) for pixels that have been balanced for many times, the average of the balance result is taken as the gray value of the pixel in the output image

The features of the subblock partial overlap algorithm:

(1) the partial overlapping of subblocks reduces the shape difference of the equilibrium function between adjacent subblocks, so that the block effect can basically eliminate a small number of block effects that may occur on the boundary of subblocks. It is not difficult to overcome the block effect elimination filter (BERF).

(2) since the total number of subblock equalization is much less than the overlapping mode of subblock, the calculation efficiency is greatly improved.

(3) the enhancement ability of image details is similar to the sub-block overlapping algorithm.

   2)  Pseudocode:

1. Determine template size n*n

2. Expand the image, because the processing of the boundary will make the image unable to achieve one-to-one correspondence with the template.

3. Starting from the first pixel of the image, dot with the template and the local area after the dot product to conduct histogram averaging, and take the local center element as the current value of the image

   

3)Matlab codes:

function [result] = LocalHistEqu_11711118(img, mSize)

%Building template

n=mSize;

model(1:n,1:n)=1;

%Expand the original image, because the template center starts from the first pixel of the image, the template must be a little outside the image, there is no corresponding point, so expand, the image one to one

imgTend=wextend('2D','sym',img,n);

tendTrans=double(imgTend);%change to Double type to be convenient for later dot product calculation

[row,col]=size(imgTend);%Gets the size of the expanded image

tendTransTmp=tendTrans;

 

for i=n+1:row-n

    for j=n+1:col-n %Because of the expansion, the first pixel of the original image corresponds to the point (n+1, n+1) in the expanded image

        modelResult=tendTrans(i:i+(n-1),j:j+(n-1)).*model(1:n,1:n);%In the image, take the size of n*n, and the template corresponding dot product, the product of the dot product of the size of n*n matrix m

        modelEpual=histeq(uint8(modelResult));%Histogram equalization of local m was performed

        %k=histeq(m);%Histogram equalization is carried out for block graph, m is of double type, so its k is all 1, then the corresponding position in x2 is all 1, and finally the display is converted into uint8 and the display is definitely black.

        equalTmp=double(modelEpual);%K is still an n by n matrix

        tendTransTmp(i,j)=equalTmp(8,8);%Assign the pixel value of the balanced center point to the element of the corresponding point in the original image

    end

end

%Unassigned element of % takes its original value

result=tendTransTmp(n+1:row-n,n+1:col-n);%Take the original image size after equalization

result=uint8(result);

end

4)The output figure of the program:

Figure 5 input figure

Figure 6 output figure

 

4)Analysis and conclusions:

By observing the resulting image, we can clearly see that after the processing of local histogram equalization algorithm, the patterns and symbols hidden in the shadow are clearly displayed. Some blemishes and distortions occur, but they do not affect the detail or shape of the image.

  1. Use median filtering to reduce SAP:
  1. Use medium filter to reduce SAP:

Salt and pepper noise is also known as impulse noise. It is produced by image sensor, transmission channel, decoding and processing of black and white light and dark noise. The black noise is known figuratively as pepper noise, and the white noise is known as salt noise. In general, these two kinds of noises appear at the same time, which is the black and white clutter on the image.

Salt and pepper noise are often caused by image cutting. Median filtering is the most commonly used algorithm to remove pulse interference and salt and pepper noise.

 

2)Matlab codes:

function [OutputImage] = ReduceSAP1_11711118(InputImage, nsize)

[~,~,ch]  = size(InputImage);

if ch ==3

    InputImage =rgb2gray(InputImage);

end

OutputImage = medfilt2(InputImage, [nsize,nsize]);

figure;imshow(OutputImage);title('median filtering');

end

 3)The output figure of the program:

 

Figure 7 output figure

 

4)Analysis and conclusions:

By observing the resulting image, we can clearly see the SAP has been reduced. And we can change the value of nsize to change the quality of the figure.

Let us have a close look to check the influence of changing the value of nsize:

Figure 8  nsize = 2

 

 

Figure 9 nsize = 4

 

Figure 10 nsize = 10

  When nsize is small, there will still have many noises on the figure. And nsize gets larger the quality of the figure and the effect of removing noise will be better. But when nsize is large enough, the picture will blur and lose a lot of detail. We can see that the effective method to filter salt and pepper noise is median filtering. After removing salt and pepper noise, relatively smooth signal can be obtained, and its effect is better than the mean filter. Of course, the median filter will also cause edge blur and the signal is not sharp enough, which seems to be a common problem of many filtering methods. As long as it is denoising, it must be low-frequency filtering, and the result of low-frequency filtering is that low-frequency information is kept while high-frequency information is lost.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值