Chapter 1 Introduction

Chapter 1 Introduction

-

  • MATLAB and Digital Image Processing

  • digital image representation

    • Coordinate convention

    • image as matrix

  • Image input and display

  • Classes and Image Types

    • kind

    • image type

    • grayscale image

    • Binary image

  • M function programming

    • M file

    • operator

    • arithmetic operators

    • relational operator

    • Logical Operators

    • array index

    • some important standard arrays

    • Function handles, cell arrays, and structures

  • Code optimization

1. MATLAB and digital image processing

MATLAB is a matrix-oriented language, short for Matrix Laboratory (Matrix Laboratory), and its original intention was to be a software for processing matrices and linear algebra.

Digital image processing is the processing of digital images with the help of computers.

Digital image: An image can be defined as a two-dimensional function f(x, y), where x and y are spatial coordinates, and the magnitude of f at any coordinate (x, y) is called the image at that point = =brightness or grayscale==. When the magnitudes of x, y, and f are all finite discrete values, the image is called a digital image.

2. Digital image representation

1. Grayscale usually refers to the brightness of a monochrome image.

2. A color image is composed of multiple monochrome images.


RGB彩色系统中,一副彩色图像是由三幅单色图像组成的,即红(R)、绿(G)、蓝(B)原色(或分量)图像。
Coordinate convention
  • Spatial coordinates: The plane far points of the image are all defined at (x, y) = (0, 0). ==x ranges from 0 to M - 1 and y ranges from 0 to N - 1, in integer increments. ==

  • Pixel coordinates: Row and column are represented by (r, c), and the origin of this coordinate system is at (r, c) = (1, 1). ==r ranges from 1 to M and c ranges from 1 to N, in integer increments. ==

image as matrix

Each element in the matrix is ​​called a picture element, picture element, or pixel.

Matrices are two-dimensional, while arrays can be arbitrarily finite-dimensional.

3. Image input and display

  • imread reads the image into the environment

imread(‘filename’)


f = imread('chestxray.jpg')

imread supports most popular image/graphics formats, including JPEG, JPEG 2000 and TIFF.

  • imshow displays the image on the MATLAB desktop

imshow(f)

When there are two images, to keep the first image and output the second image, use


figure, imshow(g)
  • The image is written to the current directory by the function

imwrite(f, ‘filename’)

Note: imwrite can also have other parameters, depending on the format of the file to be written.

A more general form of imwrite syntax that only works with tif images is

imwrite(g, ‘filename.tif’, ‘compression’, ‘parameter’,…’resolution’, [colres rowres])

A more generic imwrite syntax for JPEG only is

imwrite(f, ‘filename.jpg’, ‘quality’, q)

  • All the information of the image is stored in a structure

imfinfo(‘filename’)

Tip: The ";" sign at the end of the statement is used by MLAB to suppress the output. If there is no semicolon in the statement, MATLAB will display the result of the operation specified in the line on the screen.

4. Classes and Image Types

  • kind

image

  • image type

    • Gray-scale images

    • Binary images

    • Indexed images

    • RGB images

grayscale image

A grayscale image is a data matrix whose values ​​represent the shades of gray.

Binary image

A binary image is a logical array of only 0s and 1s.


A = [1 0 1; 0 1 0; 1 0 1]

imshow(A, 'InitialMagnification', 'fit');

A matrix of only 0s and 1s can form a binary image.

image

  • Check if an array is of class logical

islogical(A)


B = logical(A)

islogical(A)

islogical(B)

The first line of the result will be setting B to be a 3 x 3 logical array, the same value as A. A is not a logical class, ans = 0; B is a logical class, ans = 1.

  • Convert logical array to numeric array

B = class_name(A)

where class_name can be im2uint8, im2uint16, im2double, im2single or mat2gray.

g = mat2gray(A, [Amin, Amax])

Amin和Amax的作用:若A中的值小于Amin,则在g中变为0;若A中的值大于Amax,则在g中变为1.

g = mat2gray(A)

将Amin和Amax的值设定为A中的实际最小值和最大值。

5. M function programming

  • M file

functional form

function [outputs] = name(inputs)

Take calculating the sum and product of two images as an example


function [s, p] = sumprod(f, g)

s = plus(f, g);

p = times(f, g);

image

image

help function_name

show the function

arithmetic operators

image

relational operator

A == B

A==B: Generate a logical array with the same dimensions as A and B. When the corresponding elements of A and B match, the position is 1, and the rest is 0.

Roughly similar to relational operators in other languages.

Logical Operators

1.AND—>&

2.OR—->|

3.NOT—>~

  • Logical function

xor (exclusive or)

all: All elements in a vector are non-zero, the function returns 1, otherwise it returns 0.

any: If any element in a vector is non-zero, the function returns 1, otherwise it returns 0, the function operates column-wise in the matrix.

array index

>> v = [1 3 5 7 9]



v =



     1     3     5     7     9



>> w = v.'



w =



     1

     3

     5

     7

     9



>> v(1:3)  %坐标约定从1开始,如果改成v(0:3)会提示错误,索引必须为正整数或逻辑值。这和一般我们学过的语言不相同



ans =



     1     3     5



>> v(3:end)



ans =



     5     7     9



>> v([1 4 5])  %向量作为索引



ans =



     1     7     9



>> v(1:2:end)  %从1开始,依次加2



ans =



     1     5     9
  • matrix index

>> A = [1 2 3; 4 5 6; 7 8 9]



A =



     1     2     3

     4     5     6

     7     8     9



>> A(2,:)  %第二行的所有值



ans =



     4     5     6



>> sum(A(:, 3))   %第三列的所有值的和



ans =



    18
  • logical index

>> A = [1 2 3; 4 5 6; 7 8 9]



A =



     1     2     3

     4     5     6

     7     8     9



>> D = logical([1 0 0; 0 0 1; 0 0 0])



D =



  3×3 logical 数组



   1   0   0

   0   0   1

   0   0   0



>> A(D)  %显示的是所在位置的值



ans =



     1

     6
some important standard arrays

image


>> A = 2 * ones(3, 3)  %元素都是2的一个3 x 3的矩阵



A =



     2     2     2

     2     2     2

     2     2     2



>> B = rand(2, 5)  %随机生成2 x 5的矩阵



B =



    0.8147    0.1270    0.6324    0.2785    0.9575

    0.9058    0.9134    0.0975    0.5469    0.9649

Function handles, cell arrays, and structures

function handle
  • named function handle

>> f = @sin



f =



  包含以下值的 function_handle:



    @sin



>> f(pi/4)



ans =



    0.7071



>> sin(pi/4)



ans =



    0.7071
  • anonymous function handle

(input-argument-list) expression


>> g = @(x) x.^2



g =



  包含以下值的 function_handle:



    @(x)x.^2



>> g(3)



ans =



     9
cell array

Element matrices provide a way to combine a set of objects under a variable name

C = {f, b, char_arry}


>> f = imread('image.tif');

>> b = [1 2 3; 4 5 6]



b =



     1     2     3

     4     5     6



>> char_array = {'area', 'centroid'}



char_array =



  1×2 cell 数组



    {'area'}    {'centroid'}



>> C = {f, b, char_array}



C =



  1×3 cell 数组



    {664×840 uint8}    {2×3 double}    {1×2 cell}



>> C{3}



ans =



  1×2 cell 数组



    {'area'}    {'centroid'}



>> C(3)



ans =



  1×1 cell 数组



    {1×2 cell}

Curly braces look at the entire contents of an element of the unit, and parentheses give a description of the variable.

structure

function s = image_stats(f)

s.dm = size(f);   %size(f)返回二维图像f的行数和列数

s.AI = mean2(f);   %计算f中元素的平均值

s.AIrows = mean(f, 2);   %mean(v)返回v元素的平均值,若A是一个矩阵,则将A当做向量来处理,返回平均值的一个行向量

s.AIcols = mean(f, 1);

Call functions


>> s = image_stats(f)



s = 



  包含以下字段的 struct:



        dm: [664 840]

        AI: 192.5988

    AIrows: [664×1 double]

    AIcols: [1×840 double]

s shows the basic information of this picture.

6. Code optimization

There are two important approaches to optimization:

1. Preallocate the array

2. Vectorized loop

  • Measuring function runtime

tic; function; toc;

preallocated array

%正常函数形式

function y = sinfun1(M)

x = 0:M - 1;

for k = 1:numel(x)              %numel(x)给出数组的元素数

    y(k) = sin(x(k) / (100 * pi));

end

%预分配函数形式

function y = sinfun2(M)

x = 0 : M - 1;

y = zeros(1, numel(x));    %zeros(M, N)生成一个double类的全0的M x N矩阵。

for k = 1 : numel(x)

    y(k) = sin(x(k) / (100 * pi));

end

Preallocation can help us reduce memory fragmentation.


>> tic;sinfun1(100);toc

时间已过 0.000430 秒。

>> tic;sinfun2(100);toc

时间已过 0.000366 秒。

The above is the time comparison of the two functions, you can see the time gap.

  • reliable time measurement

Using tic;toc; If the statement is typed on separate lines, the time measurement will include the time required to type the next two lines. This will result in inaccurate time measurement. So use the timeit function.

s = timeit(f)


>> M = 100;

>> f = @()sinfun1(M);

>> timeit(f)



ans =



   8.1831e-05



>> g = @()sinfun2(M);

>> timeit(g)



ans =



   6.5239e-06

MATLAB vectorization is a technique that eliminates loops entirely, but its runtime is about the same as with loops.

Vectorized loop (meshgrid function)

%两个嵌套for循环

function f = twosin1(A, u0, v0, M, N)

f = zeros(M, N);

for c = 1:N

    v0y = v0 * (c - 1);

    for r = 1:M

        u0x = u0 * (r - 1);

        f(r, c) = A * sin(u0x + v0y);

    end

end

%使用meshgrid函数重写该函数

function f = twosin2(A, u0, v0, M, N)

r = 0:M - 1;   %Row coordinates

c = 0:N - 1;   %Column coordinates

[C, R] = meshgrid(c, r);

f = A * sin(u0 * R + v0 * C);

When comparing the time, it is found that when using the meshgrid function, the time used is relatively small, which speeds up the running speed


>> timeit(@()twosin1(1, 1/(4*pi), 1/(4*pi), 512, 512))



ans =



    0.0075



>> timeit(@()twosin2(1, 1/(4*pi), 1/(4*pi), 512, 512))



ans =



    0.0054


  • displayed picture

>> f = twosin2(1,1/(4*pi),1/(4*pi),512,512);

>> imshow(f, [])

image


>> f = twosin2(1,1/(4*pi),1/(4*pi),512,512);

>> imshow(f)

image

The two pictures are displayed differently, but both are f. The reason is that imshow(f) directly displays the f image; imshow(f,[]) displays the grayscale normalized image, which is equivalent to f = mat2gray(f).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733484&siteId=291194637