Brightness transformation and spatial filtering
1. Background knowledge
空间域技术
It is to directly operate on the pixels of the image. There are expressions:
g (x, y) = T [f (x, y)] g(x,y)=T[f(x,y)]g(x,and )=T[f(x,y ) ] where f is the input and g is the output.
Also, the adjacent point of the point (x, y) is defined as a square or rectangle whose center is at (x, y). (This is interesting, the border may not be filled)
2. Brightness transformation function
In a grayscale image, the brightness is the grayscale, while in a color image, it is the color component of each channel.
2.1 function imadjust
The function syntax:g = imadjust(f, [low_in high_in], [low_out high_out], gamma)
- To put it simply,
in
the value of the value映射
toout
the value can be understood as the kind of domain change in mathematics. The ranges of these two matrices are0~1
independent of the image type and will be adaptive. gamma
Determines whether the mapping method is linear or not, which can be omitted, and the default value is 1.
- If
high_out < low_out
the brightness will be reversed.
2.2 Logarithmic and contrast stretching transformation
Logarithmic scaling means that if there are too many high frequencies, things in the low frequency range are not dominant when viewing the image. If the high frequencies dominate, the low frequencies will be suppressed. So we have to figure out a way to deal with it 缩放
. This function will be introduced later.
Based on this idea, one is derived 对比度拉伸变换函数
. The high frequency has a high value, the low frequency has a low value, and then the intermediate frequency is scaled by a logarithmic series or directly 阶跃
. In the case of a step, the image will become a binary image.
The function expression of this method is:
s = T (r) = 1 1 + (m / r) E s=T(r)=\frac{1}{1+(m/r)^E}s=T(r)=1+(m/r)E1E is the zoom function, which 斜率
matlab
can be expressed as follows:
g = 1./(1 + (m./(double(f) + eps)).^E)
simple, you can use it directly
g = im2uint8(mat2gray (log(1 + double (f))));
2.3 Integration
We can intrans
realize the 负片、对数、gamma、对比度拉伸变换
integration of all the functions we have learned by writing a function ourselves :
function g = intrans(f, varargin)
error(nargchk(2, 4, nargin));
classin = class(f);
if strcmp(class(f), 'double') & max(f(:)) > 1 & ~strcmp(varargin{
1}, 'log')
f = mat2gray(f);
else
f = im2double(f);
end
method = varargin{
1};
switch method
case 'neg'
g = imcomplement(f);
case 'log'
if length(varargin) == 1
c = 1;
elseif length(varargin) == 2
c = varargin{
2};
elseif length(varargin) == 3
c = varargin{
2};
classin = varargin{
3};
else
error('Incorrect number of inputs for the log option')
end
g = c*(log(1 + double(f)));
case 'gamma'
if length(varargin) < 2
error('Not enough inputs for the gamma option')
end
gam = varargin{
2};
g = imadjust(f, [ ], [ ], gam);
case 'stretch'
if length(varargin) == 1
m = mean2(f);
E = 4.0;
elseif length(varargin) == 3
m = varargin{
2};
E = varargin{
3};
else
error('Incorrect number of inputs for the stretch option')
end
g = 1./(1+(m./(f + eps)).^E);
otherwise
error('Unknown method')
end
g = changeclass(classin, g);
Then changeclass
this function needs to be written by yourself:
function image = changeclass(class, varargin)
switch class
case 'uint8'
image = im2uint8(varargin{
:});
case 'uint16'
image = im2uint16(varargin{
:});
case 'double'
image = im2double(varargin{
:});
otherwise
error('Unsupported IPT data class.');
end
So we can call this integrated function:
f = imread('gray.jpg');
g = intrans(f, 'stretch', mean2(im2double(f)), 0.9);
subplot(1,2,1);
imshow(f);
subplot(1,2,2);
imshow(g);
3. Histogram processing and drawing functions
At present, the histogram plays a basic role in enhancement, compression, segmentation, and description. Other applications will be introduced later, and now I learn how to draw.
3.1 Generate and draw a histogram
To put it simply, it is to divide the discretization according to the brightness level. There are L brightness levels in [0,G].
The specific function is h = imhist(f, b)
how many levels b is to be divided into, if it is empty, the default is G+1,
and how many elements numel(f)
can be obtained by passing it f
, that is, h = imhist(f, b)/numel(f)
the histogram can be normalized.
The imhist(f, b);
histogram can be obtained by calling directly , but this picture is very unsightly, so other drawing methods are commonly used:
bar(horz, v, width)
- v is the row vector composed of the points of the histogram we want to draw
- horz is a vector with the same dimension as v, which is used to mark the horizontal scale value. If it is empty, the default is length(v). ,
- The default width is the width of the column of the histogram
axis([horzmin horzmax vertmin vertmax])
Is horizontal, the vertical axis minimum and maximum values of
set(gca, 'xtick\ytick', [])
the scale of the coordinate axes can be adjusted by a row vector
text(xloc, yloc, 'text str', 'fontsize', 'size')
can add text coordinate axes
title('str')
can add titles
similar, FIG rod: stem(horz, v, 'color_style_marker', 'fill')
, fill
is whether the coordinate point is filled
there plot(horz, v, 'color_style_marker')
.
Which color_style_marker
is consistent with the parameters in Python: the
final style is as follows:
3.2 Histogram equalization
Assuming that the gray level is normalized to a continuous amount in the range [0,1], then p r r represents the probability density function (PDF) of the gray level in a given image, and the input gray level is transformed as follows: Get the output gray level s:
s = T (r) = ∫ 0 rpr (w) dws=T(r)=\int_{0}^{r} p_r(w)\, dws=T(r)=∫0rpr(w)d w where w is the dummy component of the integral, it can be seen that the probability density function of the output gray level is uniform:
ps (S) = {1, 0 ≤ s ≤ 1 0, other p_s(S) = \ begin{cases} 1,\qquad\ 0\leq s\leq 1 \\ 0, \qquad\ other\end{cases}ps(S)={
1, 0≤s≤10, That he
Anyway, in general, after the histogram is equalized, some areas that are not obvious will be added. For example, if the picture is too bright, it will become darker, and if it is too dark, it will change. It has high contrast.
Generally speaking, we use the function g = histeq(f, nlev)
where nlev is 64 by default. But in order to have the best effect, you need to set the gray level to the largest possible number, such as 256.
Here you can see the effect:
by executing the code:
f = imread('gray.jpg');
h = imhist(f);
h1 = h(1:10:256);
axis([0 256 0 15000]);
subplot(2,2,1);
plot(h);
subplot(2,2,3);
imshow(f);
g = histeq(f, 256);
h2 = imhist(g);
subplot(2,2,2);
plot(h2);
subplot(2,2,4);
imshow(g);
figure(2);
hnorm = imhist(f)./numel(f);
cdf = cumsum(hnorm);
x = linspace(0,1,256);
plot(x, cdf);
axis([0 1 0 1]);
Since the original image is brighter, our equalized image becomes darker, and we can see the transformation function of our cdf
3.2 Histogram matching
Histogram matching is to adjust the histogram of the graph according to the given histogram, and the function still uses the histeq(f, [])
latter parameter instead of a number but a row vector.
Four. Spatial filtering
4.1 Linear spatial filtering
To put it bluntly 卷积
. Flip first and then pan. Rotate the convolution kernel by 180° and translate each pixel.
Then you can use this function:g = imfilter(f, w, mode, boundary, size)
- f is the original image
- w is the convolution kernel
- mode can be:
corr
orconv
. The former is related to the kernel without operation, and the latter is that the convolution will rotate the convolution kernel by 180°. - boundary: boundary option.
P
The boundary filling value P, the'replicate'
replication boundary expansion, thesymmetric
mirror reflection and the boundary expansion,circular
the image is regarded as a period of a two-dimensional periodic function to expand. - size: Size options.
'full'
After pressing the expansion, the output of the filled image'same'
will be the same size as the input image.
4.2 Nonlinear spatial filtering
Non-linear spatial filtering refers to: similar to linear spatial filtering, except that the operation on each pixel includes a neighborhood of pixels, such as taking the maximum value of the pixel in the kernel. There are two functions, colfilt
and in nlfilter
general, the pursuit of speed is used colfilt
. It will generate a largest mn × MN matrix A, which is a matrix of neighborhood × image size. In this matrix, each column corresponds to a pixel surrounded by a neighborhood whose center is located at a certain position in the image.
The syntax is: g = colfilt(f, [m n], 'sliding', @func, parameters)
@func
it is called a function handle, which is a function pointer in C language. parameters
It is the parameter to be used by func.
The function func
must operate on each column of the matrix and return a row vector v containing all the columns. The kth element of v represents the result of the func operation on the kth column in A. So there can be MN columns in A, and the maximum dimension of v is 1×MN.
So we can padarray
fill it with:
g = padarray(f, [r c ], method, direction)
r c
is the number of rows and columns to be increased. For
example:
f = [1 2;3 4];
fp = padarray(f, [3 2], 'replicate', 'post');
disp(fp);
%%
1 2 2 2
3 4 4 4
3 4 4 4
3 4 4 4
3 4 4 4
%%
Five. IPT's standard spatial filter
5.1 Linear spatial filter
Syntax for generating filtersw = fspecial('type', parameters)
5.2 Nonlinear spatial filter
ordfilt2
You can sort the pixels contained in the image field, and then use the value determined by the sorting result to replace the value of the center pixel in the neighborhood. Other nonlinear filters will be discussed later.
grammarg = ordfilt2(f, order, domain)
- Use the first in a set of sorted elements of the field
order
to replace the element in f. The neighborhood is specified by non-zero elements in the domain - Domain is a matrix composed of 0 1. And can specify a specific "neighborhood".
For example, the minimum value filter:g = ordfilt2(f, 1, ones(m, n))
there is also the maximum value filter: ofg = ordfilt2(f, m * n, ones(m, n))
course, there is also the median filter:g = ordfilt2(f, median(1:m*n), ones(m, n))
median
you can find the median value of each row vector according to the row vector, and finally get a median column vector. but.ceil((m*n)/2)
It's okay to use it, but it takes more time to use median. . .