Digital Image Processing Chapter 1

Chapter 1: Introduction

1.1 Background

The main purpose of this book is to bring together broad theoretical concepts and the knowledge required to implement these concepts with modern image processing software tools. Principle-based, organized and presented through textbooks rather than manuals.

1.2 What is digital image processing

Digital Image Processing is a method and technology for removing noise, enhancing, restoring, segmenting, and extracting features through a computer. The emergence and rapid development of digital image processing are mainly affected by three factors: first, the development of computers; second, the development of mathematics (especially the establishment and improvement of discrete mathematical theory); , military, industrial and medical applications demand growth.

1.3 Basics of MATLAB and Image Processing Toolbox

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and transformation into a simple environment to represent problems and their solutions in familiar mathematical notation. Typical applications include the following:

  • Math and Computing
  • Algorithm development
  • data collection
  • Modeling, Simulation and Prototyping
  • Data analysis, detection and visualization
  • Science and Engineering Graphics
  • Application development, including graphical user interface building

1.4 Scope of image processing covered in this book

  • Chapter 2: Grayscale Transformation and Spatial Filtering
  • Chapter 3: Frequency Domain Filtering
  • Chapter 4: Image Restoration and Reconstruction
  • Chapter 5: Color Image Processing
  • Chapter 6: Image Compression
  • Chapter 7: Image Segmentation
  • Chapter 8: Representation and Description

1.7 Fundamentals

1.7.5 Digital image representation

write picture description here
write picture description here
write picture description here
write picture description here

1.7.6 Image input/output and display

enter:

Use the function imread to read the image into the MATLAB environment. The basic usage is

imread('filename')

Here, filename is a string containing the full name of the image file (including any available extensions). E.g:

>> f = imread('chestxray.jpg');

The function imread supports most popular image graphics formats, including JPEG and TIFF

output:

Use the function imshow to display the image on the MATLAB desktop. The basic syntax of the function is:

imshow(f)

where f is an array of images.
example:

>> f = imread('rose_512.tif');
>> imshow(f)

If you then use imshow to display another image g, MATLAB replaces the image in the graphics window with the new image. To keep the first image and output the second image, use the function figure as follows:

>> figure, imshow(g)

write picture description here

The image is written to the current directory by the function imwrite, which has the following basic usage:

imwrite(f,'filename')

The function imwrite can also have other parameters, depending on the file format to be written. A more general imwriteuifa is

imwrite(f, 'filename', 'quality', q)

where q is an integer from 0 to 100 (for JPEG compression, the lower the number, the higher the degradation).

1.7.7 Classes and Image Types

Although we use integer coordinates, pixel values ​​(grayscale) are not restricted to integers in MATLAB. The table lists the various classes supported by MATLAB and the Image Processing Toolbox for describing pixel values.

The toolbox supports 4 image types:

  • grayscale image
  • Binary image
  • index image
  • RGB image

About binary images:
Use the function logical to convert a numeric array to a binary image

B = logical(A)

You can use the function islogical to test whether an array is logical

iflogical(C)

1.7.8 M-function programming

M file:

M files are created by a text editor and saved with a filename of the form filenamne.m. The components are as follows:

  • function definition line
  • Line H1
  • help text
  • function body
  • Remarks A
    function definition line has the following form:
function [outputs] = name(inputs)

For example, a function that computes the sum and product of two images has the form:

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

where f and g are the input images, s is the sum image, and p is the product image. The noun sumprod is arbitrarily chosen, but the word function always appears on the left. The function name must start with a letter, the rest can be any combination of alphanumerics and underscores, and no spaces are allowed.

Functions can be called at the command prompt. E.g,

>> [s,p] = sumprod(f,g);

Line H1 is the first text line, which is a separate comment line following the function definition line. There can be no blank lines or leading spaces between them, for example:

%SUMPROD Computes the sum and product of two images.

when the user enters

>> help function_name

Line H1 is the text that appears first.

Help text is the block of text immediately following the H1 line, with no blank lines in between.
All non-H1 lines or lines of help text following the symbol "%" are considered comment lines and are not recognized as part of a block of help text.
M files can be created and edited with any text editor, and can be saved to a specified directory with the extension .m. Another way to create or edit an M-file is to use the edit function after the prompt. E.g:

>> edit sumprod

The file sumprod will be opened for editing, and if it cannot be found, MATLAB will create the file.

arithmetic operators

MATLAB has two different types of arithmetic operators. Matrix arithmetic is defined by the rules of linear algebra, array arithmetic is performed element-by-element, and multidimensional arrays can be used. The period character (.) is used to distinguish array operations from matrix operations.
write picture description here

relational operator

write picture description here

Logical Operators

write picture description here

flow control

write picture description here

array index

write picture description here
Using the transpose operator (.'), you can convert a row vector to a column vector (and vice versa):
write picture description here
To access a block of elements, use a colon, for example, to access the first three elements of v:

>> v(1:3)

ans =

     1     3     5

Similarly, the third to last element can be accessed using the following statement:

>> v(3:end)

ans =

     5     7     9

where end represents the last element in the vector.
You can also use a vector as an index into another vector:

>> v([1 4 5])

ans =

     1     7     9

Also, indices are not limited to adjacent elements, for example:

>> v(1:2:end)

ans =

     1     5     9

Among them, the symbol 1:2:end means starting from 1, adding 2 as the addend, and stopping when the count reaches the last element.
In MATLAB, a matrix can be conveniently represented as a series of row vectors, which are represented by square parentheses and separated by semicolons. For example, type

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

给出一个3×3矩阵

A =

     1     2     3
     4     5     6
     7     8     9

We can select elements in the matrix the same way we do with vectors, but we need two indices: one for the row and one for the column.
You can also select entire rows, columns, or entire matrices using a colon as an index:

>> A(2,:)

ans =

     4     5     6

>> sum(A(:))

ans =

    45

The function sum computes the sum of each column of its arguments, the single colon index transforms A into a column vector, and passes the result to sum.
Another fairly useful form of indexing is logical indexing. A logical index has the form A(D), where A is an array and D is a network group of the same size as A. The expression A(D) extracts all elements in A that correspond to 1-valued elements in D, such as:

>> 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

Function handles, cell arrays, and structures

A function handle is a MATLAB data type in which the guard has all the information when referencing a function. One of the advantages is that a function handle can be passed as an argument in a call to another function.
Functions further have two different types, both of which are created using the function handle operator @.
E.g:

>> f = @sin

f =

    @sin

The function sin can be called indirectly by calling the function handle f:
write picture description here
the second function handle is of type anonymous function handle, which is formed by the MATLAB expression in place of the function name. The general format for building your i-name function is

@(input-argument-list) expression

For example, the square of the input available with the following anonymous function handle:

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

write picture description here

An anonymous function handle is called in a similar way to a supermodel function handle.

Cell matrices provide a way to combine a set of objects (such as arrays, characters, other cell matrices) under a variable name. For example, suppose we are dealing with: 1. a uint8 image f of size 512×512 pixels; 2. a 2D coordinate sequence b in row form of a 188×2 array; 3. a cell array containing two character names char_array={ 'area', 'centroid'}. These three different entities can be organized into a single variable c using a cell array:

C = {f, b, char_array}

Code optimization

Preallocation means initializing an array before entering a for loop that counts the elements of the array. We are going to create a MATLAB function that computes

f(x) = sun(x/100π)

where x = 0,1,2...M-1. Here is the first form:

function y = sinfun1(M)
x = 0:M - 1
for k = 1:nume1(x)
    y(k) = sin(x(k) / (100*pi));
end

( numel(x) gives the number of elements of the array x)
The output when M = 5 is

write picture description here
The MATLAB functions tic and toc can be used to measure the time the function takes to execute. Call tic, then call the function, then call toc

>> tic; sinfun1(100); toc
时间已过 0.001127 秒。

The function timeit can be used to get a reliable, repeatable time measurement of function calls:

s = timeif(f)

where f is a function handle to the function being timed, and s is the measured time in seconds of the corresponding call to f.
We can use timeit for sinfun1 when M=100:

>> M=100

M =

   100

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

ans =

   2.9065e-05

Continue the experiment and use timeit to measure the execution time of sinfun1 at M=500, 1000, 1500, ······, 20000

M=500:500:20000;
for k = 1:numel(M)
f = @() sinfun1(M(k));
t(k) = timeit(f);
end

This time will increase in proportion to M^2, and new memory space must be re-allocated each time, which is a huge overhead.
Preallocation can be initialized to the output size of the PM before it starts. The function zeros is usually used for preallocation. The second form of the function sinfun2.m uses preallocation:

function y = sinfun2(M)
x = 0:M-1;
y = zeros(1, numel(x));
for k = 1:numel(x)
    y(k) = sin(x(k) / (100*pi));
end

Comparing the time required for sinfun1 (20000) and sinfun2 (20000): in the preallocated
write picture description here
form, it runs about 220 times faster.

Guess you like

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