## 1. Overview of point operation

Processing flow of digital image processing system: preprocessing -- > feature extraction -- > recognition analysis

Point operation is to perform the same gray transformation operation on each pixel in the image in turn. Let the gray value of the input image f(x,y) and the output image g(x,y) at any point (x,y), and the point operation is expressed as: g(x,y) = T(f(x,y)). Where T is the point operator used, which represents a certain gray level mapping relationship between the original image and the output image.

Point operation is often used to change the gray range and distribution of an image. Because of its nature, point operation is sometimes called contrast enhancement, contrast stretching or gray transformation. (contrast: the ratio of maximum brightness to minimum brightness in gray image)

## 2. Gray histogram

Gray level histogram describes the gray level statistical information of an image, which is mainly used in image segmentation and image gray level transformation.

In terms of mathematical comparison, image histogram describes the statistical characteristics of each gray level in the image. It is a function of image gray value, which is used to count the number or probability of each gray level in an image (that is, the number or proportion of each gray level pixel). The normalized histogram can directly reflect the ratio of different gray levels.

Graphically speaking, the gray histogram is a two-dimensional diagram. The abscissa is the gray level of each pixel in the image, and the ordinate is the number or probability of pixels at each gray level.

Gray histogram of image generated by Matlab:

I = imread('pout.tif'); figure; % Create a window imshow(I); title('Source'); % Display the original image and set the title figure; imhist(I);title('Graph'); % Generate grayscale histogram. The default is 256 grayscale values and 256 intervals(0~255)，At this time, the number of pixels in 256 intervals is counted figure; >> imhist(I, 64); title('Graph64'); % The gray level is divided into 64 intervals

Draw normalized histogram:

I = imread('pout.tif'); figure; [M, N] = size(I); [counts,x] = imhist(I, 32); % 32 Intervals, return the number of pixels falling in each interval counts, x Is each interval counts = counts / (M * N); % normalization stem(x, counts); % Draw normalized histogram

## 3. Linear transformation of gray scale

Matlab example:

I = imread('coins.png'); I = im2double(I); % Convert to floating point representation [M,N] = size(I); figure(1); imshow(I); title('Source Image'); figure(2); [H,x] = imhist(I, 64); stem(x, (H / (M * N)), '.'); title('Source Stem'); % Normalized histogram % Increase contrast Fa = 2; Fb = -55; O = Fa .* I + Fb / 255; figure(3); subplot(2,2,1); imshow(O); title('Fa = 2 Fb = -55 Add Contrast'); figure(4); subplot(2,2,1); [H,x] = imhist(O, 64); stem(x, (H/M/N), '.'); title('Fa = 2 Fb = -55 Add Contrast'); % Reduce contrast Fa = 0.5; Fb = -55; O = Fa .* I + Fb / 255; figure(3); subplot(2,2,2); imshow(O); title('Fa = 2 Fb = -55 Sub Contrast'); figure(4); subplot(2,2,2); [H,x] = imhist(O, 64); stem(x, (H/M/N), '.'); title('Fa = 2 Fb = -55 Sub Contrast'); % Increase brightness Fa = 1; Fb = 55; O = Fa .* I + Fb / 255; figure(3); subplot(2,2,3); imshow(O); title('Fa = 1 Fb = 55 Add Brightness'); figure(4); subplot(2,2,3); [H,x] = imhist(O, 64); stem(x, (H/M/N), '.'); title('Fa = 1 Fb = 55 Add Brightness'); % Reverse display Fa = -1; Fb = 255; O = Fa .* I + Fb / 255; figure(3); subplot(2,2,4); imshow(O); title('Fa = -1 Fb = 255 Reverse'); figure(4); subplot(2,2,4); [H,x] = imhist(O, 64); stem(x, (H/M/N), '.'); title('Fa = -1 Fb = 255 Sub Reverse');

It can be seen from the above example that changing the contrast of the image is the scaling and translation of the histogram, changing the brightness of the image is only the translation of the position of the histogram on the horizontal axis, and the reverse is the horizontal mirror image of the histogram. Simple linear gray-scale transformation can only solve the problem of overall visual contrast to a certain extent, but the enhancement of image details is relatively limited, which generally needs to be combined with nonlinear transformation.

## 4. Gray logarithm transformation

General expression of logarithmic transformation:

As shown in the logarithmic function curve in the figure below, the curve slope is very high when the function independent variable is low, and the curve slope becomes smaller when the independent variable is high. It can be seen from the logarithmic function curve that this transformation can enhance the details of the darker part (equivalent to the low value independent variable) in an image, so it can be used to expand the darker pixels in the compressed high value image. Therefore, logarithmic transformation is widely used in the display of spectrum image. A typical application is Fourier spectrum, whose dynamic range may be as wide as 0 ~ 10 ^ 6 When the spectrum is directly displayed, the dynamic range of the image display device often can not meet the requirements, resulting in the loss of a large number of dark details. After the logarithmic transformation, the dynamic range of the image is reasonably nonlinear compressed, so that it can be clearly displayed.

The following mathematical functions are used to realize the logarithmic transformation of image I:

The following uses Matlab to realize the logarithmic transformation of Fourier spectrum image from

% Read image I = imread('coins.png'); % Calculate the Fourier spectrum of the image F = fft2(im2double(I)); F = fftshift(F); F = abs(F); % Logarithmic transformation T = log(F + 1); subplot(1,2,1); imshow(F, []); title('Untransformed Fourier spectrum'); subplot(1,2,2); imshow(T, []); title('Spectrum after logarithmic transformation');

## 5. Gamma transform

Gamma transform, also known as exponential transform or power transform, is another commonly used gray nonlinear transform.

The mapping relationship of gamma transformation is shown in the figure below. During transformation, the gray range of 0 ~ 255 should be transformed to the range of 0 ~ 1 first, and then mapped to the original range after gamma transformation.

The following program realizes the effect of different values of gamma coefficient:

I = imread('pout.tif'); % Gamma = 0.75 subplot(1,3,1); imshow(imadjust(I, [], [], 0.75)); title('Gamma 0.75'); % Gamma = 1.0 subplot(1,3,2); imshow(imadjust(I, [], [], 1.0)); title('Gamma 1.0'); % Gamma = 1.5 subplot(1,3,3); imshow(imadjust(I, [], [], 1.5)); title('Gamma 1.5');

It can be seen from the effect that the gamma factor brings changes to the overall brightness of the image and the influence of the detail definition of the dark and bright parts of the image. When gamma silver takes 1, the image does not change. The grayscale histograms of three images are generated below:

subplot(1,3,1); imhist(imadjust(I, [], [], 0.75)); title('Gamma=0.75'); subplot(1,3,2); imhist(imadjust(I, [], [], 1)); title('Gamma=1'); subplot(1,3,3); imhist(imadjust(I, [], [], 1.5)); title('Gamma=1.5');

Observe the changes of non-zero interval position of histogram and the impact of these changes on the image. Because gamma transform is not a linear transform, it can not only change the contrast of the image, but also enhance the details, so as to improve the overall effect.

## 6. Gray threshold transformation

Gray threshold transformation can convert a gray image into a black-and-white binary image. This process is also called binarization or thresholding. The user specifies a gray value that plays the role of decomposition limit. If the gray value of a pixel in the image is less than the gray value, set the gray value of the pixel to 0, otherwise set to 255. This gray value that plays the role of decomposition first is called the threshold. Thresholding can directly divide the image content into two parts that we care about and don't care about. It is one of the important means of image segmentation.

Matlab has two functions related to threshold: im2bw and graythresh

BW = im2bw(I, level) Grayscale image I Convert to binary image BW，among level It's a threshold level. It's a[0,1]Between double Type, which can be mapped to the corresponding gray value. If it is not specified, it defaults to 0.5. For example, enter an image I The grayscale range is 0~255 Between uint8 Images, specifying level=0.5 The corresponding segmentation threshold is 128. BW = im2bw(X, map, level) Index image with color mapping table X Convert to binary image BW = im2bw(RGB, level) True color image RGB Convert to binary image If the input image is not a grayscale image, then im2bw The input image is converted into a grayscale image, and then the grayscale image is converted into a binary image through the threshold. level = graythresh(I) use Otsu algorithm(Otsu algorithm)A global threshold is calculated, which can be used to convert gray image into binary image. I The input image for which the threshold needs to be calculated. Specific instructions can be viewed Matlab Help documentation, using doc+You can view the function name command.

Matlab2017B editionIn order to convert the picture into a binary image, use imbinarize function,The parameters must be two-dimensional grayscale image:

## bw=imbinarize( g ); or bw=im2bw( g );

stay matlab2018 Recommended in imbinarize To convert the picture into a binary image, its parameters must be gray-scale image.

stay matlab2016 Among them, only im2bw Function, whose parameters can be grayscale image or rgb Figure.

Namely in matlab2016 Can be used in im2bw Directly rgb The image is converted into a binary image; But in matlab2018 You can only first rgb Convert picture to grayscale image (with rgb2gray() Function), and then imbinarize Convert to figure 2.

Both functions convert images into grayscale images. In addition to different types of parameters, both functions can pass in another one [0,1] Threshold of, i.e.:## bw = im2bw( g , level ); or bw = imbinarize( g , level );

Threshold( level)The function of is: use value 1 (white)Replace brightness greater than in the input image level Scale all pixels with values 0 (black)Replace all other pixels.

When im2bw of level If omitted, the default is 0.5 .

Here is an example:

I = imread('rice.png'); thresh = graythresh(I); % The Otsu algorithm is used to calculate the optimal threshold bw1 = im2bw(I, thresh); % Thresholding bw2 = imbinarize(I, 130/255); % Thresholding using 130 thresholds subplot(1,3,1);imshow(I);title('Source Img'); subplot(1,3,2);imshow(bw1);title('Threshed By Otsu'); subplot(1,3,3);imshow(bw2);title('Threshed By 130');

## 7. Piecewise linear transformation

There are many kinds of piecewise linear transformation, including gray stretching, gray window transformation, etc. this section only analyzes the most commonly used gray stretching.

The following is an example. First, two user-defined functions are defined:

% parse_inputs.m % Analyze the number and validity of input parameters % A Input image, RGB chart(3D)，Grayscale image(2D)，Index diagram(X) % map Palette of index graphs(color table) (:,3) % [x1, x2] Parameter group 1: abscissa of 2 turning points in piecewise linear curve % [y1, y2] Parameter group 2, ordinate of 2 turning points in piecewise linear curve function [A, map, x1, x2, y1, y2] = parse_inputs(varargin) map = []; % iptchecknargin(low, high, num_inputs, func_name)function % inspect nargin Is the number of input traversals contained in low and high The specified range. If it is not in the range, an error will be reported. % Refer to the help document for usage iptchecknargin(3,4,nargin,mfilename); % iptcheckinput(A, classes, attributes, func_name, var_name, arg_pos)function % Check matrix A Whether the element in belongs to the given type list. If any element does not belong to the given type, an error will be reported iptcheckinput(varargin{1},... {'uint8', 'uint16', 'int16', 'double'},... {'real', 'nonsparse'},mfilename,'I, X or RGB',1); % Return different values according to the number of parameters switch nargin case 3 % The parameter may be(I, [x1,x2],[y1,y2])or(RGB, [x1,x2],[y1,y2]) A = varargin{1}; x1 = varargin{2}(1); x2 = varargin{2}(2); y1 = varargin{3}(1); y2 = varargin{3}(2); case 4 % (I, map,[x1,x2],[y1,y2]) A = varargin{1}; map = varargin{2}; x1 = varargin{3}(1); x2 = varargin{3}(2); y1 = varargin{4}(1); y2 = varargin{4}(2); end % Check the validity of input parameters % inspect RGB array if(ndims(A) == 3) && (size(A,3) ~= 3) msg = sprintf('%s:True color images should use M-N-3 Dimension array', mfilename); eid = sprintf('Images:%s:RGB Color Image Must Be M-N-3', mfilename); error(eid, '%s', msg); end % Check palette if ~isempty(map) if (size(map,2) ~= 3) || ndims(map)>2 msg1 = sprintf('%s:The palette must be a matrix with 3 columns(R G B)', mfilename); error('Error', '%s', msg1); elseif (min(map(:))<0) || (max(map(:)>1)) msg1 = sprintf('%s:The intensity of each component in the palette should be[0,1]Range', mfilename); error('Error', '%s', msg1); end end % int16 turn uint16 if isa(A, 'int16') A = int16touint16(A); end

% imgrayscaling.m % Perform grayscale stretching % Syntax: % out = imgrayscaling(I, [x1,x2],[y1,y2]); % out = imgrayscaling(X, map, [x1,x2],[y1,y2]); % out = imgrayscaling(RGB, [x1,x2],[y1,y2]); % When performing grayscale stretching, the input image should be grayscale image, but if it is not grayscale image, the function will automatically convert it to grayscale image x1,x2,y1,y2 Should be used double type function out = imgrayscaling(varargin) % Analytical parameters [A, map, x1, x2, y1, y2] = parse_inputs(varargin{:}); % Calculate input image A Value range corresponding to data type in range = getrangefromclass(A); range = range(2); % Convert if not grayscale if ndims(A)==3 % A The matrix is three-dimensional, RGB image A = rgb2gray(A); elseif ~isempty(map) % Index image A = ind2gray(A, map); end % Read the original image size and initialize the output image [M,N] = size(A); I = im2double(A); % turn double out = zeros(M,N); % Gray stretch for m=1:M for n=1:N if I(m,n)<x1 out(m,n)=y1*I(m,n)/x1; elseif I(m,n)>x2 out(m,n)=(I(m,n)-x2)*(range-y2)/(range-x2)+y2; else out(m,n)=(I(m,n)-x1)*(y2-y1)/(x2-x1)+y1; end end end % Converts the format of the output image to the same as the input image if isa(A,'uint8') out = im2uint8(out); elseif isa(A,'uint16') out=im2uint16(out); % Output of other situations double end % Output processing if nargout==0 % No return value received outside imshow(out); return; end

I = imread('coins.png'); J1=imgrayscaling(I,[0.3 0.7],[0.15 0.85]); figure,imshow(J1,[]);title('[x1 x2]=[0.3 0.7], [y1 y2]=[0.15 0.85]'); J2=imgrayscaling(I,[0.15,0.85],[0.3,0.7]); figure,imshow(J2,[]);title('[x1 x2]=[0.15,0.85], [y1 y2]=[0.3,0.7]'); figure(3); subplot(1,2,1); imhist(J1); subplot(1,2,2); imhist(J2);

## 8. Histogram equalization

Histogram equalization, also known as gray-scale equalization, refers to converting the input image into an output image with approximately the same number of pixels at each gray level through gray-scale mapping (that is, the output histogram is uniform). The pixels in the processed image will occupy as many gray levels as possible and be evenly distributed, so it has high contrast and dynamic range.

First, consider the case where the gray level range is [0,1] continuous, At this time, the normalized histogram of the image is the probability density function (in mathematics, the probability density function of a continuous random variable is a function that describes the possibility that the output value of the random variable is near a certain value point; the probability that the value of the random variable falls within a certain region is the integral of the probability density function in this region. When the probability density function exists, the cumulative distribution function is the integral of the probability density function. Baidu Encyclopedia: https://baike.baidu.com/item/%E6%A6%82%E7%8E%87%E5%AF%86%E5%BA%A6%E5%87%BD%E6%95%B0/5021996?fr=aladdin):

I=imread('pout.tif'); I=im2double(I); % For images with high contrast I1=2*I-55/255; subplot(4,4,1); imshow(I1); subplot(4,4,2); imhist(I1); subplot(4,4,3); % [J,T]=histeq(I)Function for histogram equalization, input image I，Output image J Sum transformation matrix T imshow(histeq(I)); subplot(4,4,4); imhist(histeq(I1)); % For images with low contrast I2=0.5*I+55/255; subplot(4,4,5); imshow(I2); subplot(4,4,6); imhist(I2); subplot(4,4,7); imshow(histeq(I2)); subplot(4,4,8); imhist(histeq(I2)); % For images with linearly increasing brightness I3=I+55/255; subplot(4,4,9); imshow(I3); subplot(4,4,10); imhist(I3); subplot(4,4,11); imshow(histeq(I3)); subplot(4,4,12); imhist(histeq(I3)); % For images with linearly reduced brightness I4=I-55/255; subplot(4,4,13); imshow(I4); subplot(4,4,14); imhist(I4); subplot(4,4,15); imshow(histeq(I4)); subplot(4,4,16); imhist(histeq(I4));

## 9. Histogram specification (matching)

Matlab example:

% Realize from image I Separate to image I1 and I2 Histogram matching I=imread('pout.tif'); % Original drawing I1=imread('coins.png'); % Image to match histogram I2=imread('circuit.tif'); % Image to match histogram % Calculate histogram [hgram1,x]=imhist(I1); [hgram2,x]=imhist(I2); % Perform histogram equalization J1=histeq(I,hgram1); J2=histeq(I,hgram2); % mapping subplot(2,3,1); imshow(I);title('Original drawing'); subplot(2,3,2); imshow(I1);title('Standard figure 1'); subplot(2,3,3); imshow(I2);title('Standard figure 2'); subplot(2,3,5); imshow(J1);title('Match to 1'); subplot(2,3,6); imshow(J2);title('Match to 2'); % histogram figure; subplot(2,3,1); imhist(I);title('Original drawing'); subplot(2,3,2); imhist(I1);title('Standard figure 1'); subplot(2,3,3); imhist(I2);title('Standard figure 2'); subplot(2,3,5); imhist(J1);title('Specify to 1'); subplot(2,3,6); imhist(J2);title('Specify to 2');