当前位置:网站首页>MATLAB basic operation command

MATLAB basic operation command

2022-06-26 09:14:00 G fruit

Learning materials
MATLAB file :https://ww2.mathworks.cn/help/matlab/ref/imwrite.html

% Command line semicolon : Cancel output 
% Command line comma : The separation command 

Image operation

Read images
f=imread('filename.jpg')% Read the current directory image , Save as  f  Array 
f=imread('D:\pictures\filename.jpg')% Absolute address 
f=imread('.\pictures\filename.jpg')% Relative address 
Display images
imshow(f,G)%G Is the grayscale series , If you omit G, The default grayscale level is 256
imshow(f,[low high])% Will be less than or equal to low The value of is displayed in black , All greater than or equal to high The values of are displayed in white 
imshow(f,[ ])% take low Set to array minimum , Variable high Set to numerical maximum 
 Interactively display the brightness value of a single pixel 
pixval% Display the cursor over the image 
 Show multiple pictures 
imshow(f), figure, imshow(g)% No addition figure,imshow The new image overwrites the old one 
Save image
imwrite(f,'filename.jpg') perhaps imwrite(f,'filename','jpg')

Only applicable to JPEG Compression and storage of images

imwrite(f,'filename.jpg','quality',q)%q yes 0 To 100 Integer between ,q The smaller it is , The more serious the image degradation 

View the details of the image file

imfinfo filename.jpg%width multiply height multiply BitDepth Divide Filesize The ratio of image compression can be obtained 

Get the information of the image file

k=imfinfo('filename.jpg')
k.Width, k.Height

Only applicable to tif How to save images

imwrite(g,'filename.tif','compression','paramater'...'resolution',[colres rowres])

'paramater’ The value is 3 individual :'none’ Represents no compression ,‘packbits’ Indicates bit packet compression ’( Default parameters for non binary images ),‘ccitt’ Express ccitt Compress ( Default parameters for binary images )
[colres rowres] The column resolution and row resolution of the image are given by the number of points in each unit

The luminance image is a numerical matrix , The normalized value represents the brightness
One uint8 Class , Its pixels are uint8, The brightness range of the image is [0,255]
One uint16 Class , Its pixels are uint16, The brightness range of the image is [0,65535]
One double Class , Its pixels are all floating point numbers , The brightness range of the image is [0,1]

A binary image is a value with only 0 and 1 Logical array of , A value contains only 0 and 1 Of uint8 An array of class , stay MATLAB Is not considered a binary image
logical Function can convert a numeric matrix into a binary matrix

B=logical(A)% if A In addition to 0,1 Values outside , These values are transformed into logic 1

Test whether a value is a logical array

islogical(C)% If it is a logical array, return 1, Otherwise return to 0

Conversion between data classes

B=data_class_name(A)% such as B=uint8(A) and B=double(A)

Conversion between image class and data class

im2uint8(f)% Less than... In input 0 The Settings for 0, Greater than 1 The Settings for 255 stay , Multiply other values by 255( rounding )
im2uint16(f)
im2double(f)% When the input is uint8, Divide by 255; When the input is uint16, Divide by 65535
im2bw(f,T)% Two valued ,T It's the threshold , The default is 0.5, Output is logical class ( Normalization is included in the middle )

Only applicable to double Class normalization

mat2gray(A,[Amin,Amax])% normalization [0,1], Less than the specified parameter Amin The value of is converted to 0,, Greater than Amax The value of is converted to 1

Support nested

g=im2double(im2bw(mat2gray(f),0.6))

Support function combination

g=double(f>2)

View the number of image rows and columns

size(f,dim)%dim=1 Press the first dimension ( Row number ),dim=2 Press the second dimension ( Number of columns )

View additional information about the image array , such as Bytes

whos f

Vector operations

v=[1 3 5 7 9]
Vector index
v(2)% Vector number 2 Number =3
v.'% Vector transpose 
v(:)% Generate a column vector 
v(1:end)% Generate a row vector 
v(1:3)% Take out the vector number 1 To the first 3 Number of numbers =1 3 5
v(3:end)% Take out No 3 To the last number =5 7 9
v(1:2:end)% Start with the first number , In steps of 2, Take the last number =1 5 9
v(end:-2:1)% Start with the last number , step -2, Take the first number =9 5 1
v([1 4 5])% Pick out the second 1 individual 、 The first 4 individual 、 The first 5 Elements 

Vector generating function

x=linspace(a,b,n)% Produce a product containing n A row vector of elements , this n The elements are linearly separated and contain a And b

Matrix operation

A=[1 2 3;4 5 6;7 8 9]
A=1 2 3
  4 5 6
  7 8 9
Matrix index
A(8)% The matrix is in MATLAB The save of is sorted by column , By column 8 Number =6
A(2,3)% The first 2 Xing di 3 Column =6
A(:,3)% The first 3 Column 
A(2,:)% The first 2 That's ok 
A(1:2,1:3)% The first 1-2 Xing di 1-3 Column 
A(:,3)=0% The first 3 Column full deployment 0
A(end,end)% Last row last column 
A(end,end-2)% The last line is the derivative 2 Column 
A(2:end,end:-2:1)% The first 2 Line to last line , The last column , In steps of -2, Until the first column 

Use a vector as the index of a matrix

A([1 3],[2 3])% The first 1 Xing He 3 That's ok , The first 2 Column and column 3 Column 

Logical array as the index of matrix

D=logical([1 0 0;0 0 1;0 0 0])
A(D)% The first 1 Xing di 1 individual , The first 2 Xing di 3 individual 

Find the sum of all the elements of a matrix

sum(A(:))% A single colon turns a matrix into a vector 

f It's a 1024X1024 Image

f(end:-1:1,:)% Flip vertically 
f(257:768,257:768)% Image cropping 
f(1:2:end,1:2:end)% Secondary sampling 

ndims(A)% Array A Dimension of (>2)

zeros(m,n)% The generation size is  m X n  whole 0 Elemental double Class matrix 
ons(m,n)% The generation size is  m X n  whole 1 Elemental double Class matrix 
ture(m,n)% Generate all 1 Elemental logical Class matrix 
false(m,n)% Generate all 0 Elemental logical Class matrix 
magic(m,n)% Generate magic matrix ( The ranks of 、 The sum of the main diagonal elements is relative , Elements are integers )
rand(m,n)% The generated elements are all in [0,1] Interval uniformly distributed random number 
randn(m,n)% The generating elements conform to the normal distribution ( The mean for 0, The variance of 1) The random number 

MATLAB Function definition

%function  notes 
function [outputs]=function_name(inputs)
function [s,p]=sumprod(f,g)

help function_name% View function comments 
edit sumprod% Edit the file 

Period character (.) Distinguish between array operations and matrix operators

A*B Matrix multiplication 
A.*B Array multiplication ( Elements multiply )

IPT Supported image arithmetic functions

imadd% Add two images , Or add constants to the image 
imsubtract% Subtract two figures or subtract constants from an image 
immultiply% Multiply two graphs , Or multiply the image by a constant 
imdivide% Divide two graphs , Or the image divided by a constant 
imabsdiff% The absolute difference between two graphs 
imcomplement% Images complement each other 
imlincomb% Calculate the linear combination of multiple graphs 
max(A)
max(A,B)
max(A,[ ],dim)

Relational operator (A、B Same dimension )

A==B% The corresponding position elements are equal to 1, The rest are 0
A>=B% The corresponding position element is greater than or equal to 1, The rest are 0
>% Greater than 
<% Less than 
~>% It's not equal to 

Logical operators

&% And operation , whole 0 For or not at all 0 by 1, For the other 0
|% Or operations , There are not 0 by 1, For the other 0
~% Non operation , Not 0 by 0,0 by 1
xor% The logical difference between the two numbers is 1
all% If all elements in a vector are not 0 by 1, For the other 0, Operate by column 
any% If there are elements in a vector that are not 0 by 1, For the other 0, Operate by column 

nemwl(A)% Get the number of pixels of the image 
原网站

版权声明
本文为[G fruit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170552523465.html