MATLAB Imagini

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

MATLAB – APIB

1. imread
2. A = imread(filename)
3. A = imread(filename,fmt)
4. A = imread(___,idx)
5. A = imread(___,Name,Value)
6. [A,map] = imread(___)
7. [A,map,transparency] = imread(___)
8. Description
9. ex
10. A = imread(filename) reads the image from the file specified by filename, inferring the format of the file from its contents. If filename is a multi-image file,
then imread reads the first image in the file.
11. A = imread(filename,fmt) additionally specifies the format of the file with the standard file extension indicated by fmt. If imread cannot find a file with the name
specified by filename, it looks for a file named filename.fmt.
12. A = imread(___,idx) reads the specified image or images from a multi-image file. This syntax applies only to GIF, PGM, PBM, PPM, CUR, ICO, TIF, SVS, and HDF4
files. You must specify a filename input, and you can optionally specify fmt.
13. ex
14. A = imread(___,Name,Value) specifies format-specific options using one or more name-value pair arguments, in addition to any of the input arguments in the previous
syntaxes.
15. ex
16. [A,map] = imread(___) reads the indexed image in filename into A and reads its associated colormap into map. Colormap values in the image file are automatically
rescaled into the range [0,1].
17. ex
18. [A,map,transparency] = imread(___) additionally returns the image transparency. This syntax applies only to PNG, CUR, and ICO files. For PNG
files, transparency is the alpha channel, if one is present. For CUR and ICO files, it is the AND (opacity) mask.
19. Examples

2. imshow
imshow(I)
imshow(I,[low high])
imshow(I,[])
imshow(RGB)
imshow(BW)
imshow(X,map)
imshow(filename)
imshow(___,Name,Value)
himage = imshow(___)
imshow(I,RI)
imshow(X,RX,map)
Description
ex
imshow(I) displays the grayscale image I in a figure. imshow uses the default display range for the image data type and optimizes figure, axes, and image object properties for
image display.
imshow(I,[low high]) displays the grayscale image I, specifying the display range as a two-element vector, [low high]. For more information, see
the DisplayRange parameter.
ex
imshow(I,[]) displays the grayscale image I, scaling the display based on the range of pixel values in I. imshow uses [min(I(:)) max(I(:))] as the display
range. imshow displays the minimum value in I as black and the maximum value as white. For more information, see the DisplayRange parameter.
ex
imshow(RGB) displays the truecolor image RGB in a figure.
ex
imshow(BW) displays the binary image BW in a figure. For binary images, imshow displays pixels with the value 0 (zero) as black and 1 as white.
ex
imshow(X,map) displays the indexed image X with the colormap map. A colormap matrix can have any number of rows, but it must have exactly 3 columns. Each row is interpreted
as a color, with the first element specifying the intensity of red, the second green, and the third blue. Color intensity can be specified on the interval [0, 1].
ex
imshow(filename) displays the image stored in the graphics file specified by filename.
imshow(___,Name,Value) displays an image, using name-value pairs to control aspects of the operation.
himage = imshow(___) returns the image object created by imshow.
imshow(I,RI) displays the image I with associated 2-D spatial referencing object RI.
imshow(X,RX,map) displays the indexed image X with associated 2-D spatial referencing object RX and colormap map.

3. imadjust
Adjust image intensity values or color map
collapse all in page

Syntax
J = imadjust(I)
J = imadjust(I,[low_in high_in])
J = imadjust(I,[low_in high_in],[low_out high_out])
J = imadjust(I,[low_in high_in],[low_out high_out],gamma)
J = imadjust(RGB,[low_in high_in],___)
newmap = imadjust(cmap,[low_in high_in],___)

Description
ex
J = imadjust(I) maps the intensity values in grayscale image I to new values in J. By default, imadjust saturates the bottom 1% and the top 1% of all pixel values. This
operation increases the contrast of the output image J.
This syntax is equivalent to imadjust(I,stretchlim(I)).
J = imadjust(I,[low_in high_in]) maps intensity values in I to new values in J such that values between low_in and high_in map to values between 0 and 1.
ex
J = imadjust(I,[low_in high_in],[low_out high_out]) maps intensity values in I to new values in J such that values between low_in and high_in map to values
between low_out and high_out.
J = imadjust(I,[low_in high_in],[low_out high_out],gamma) maps intensity values in I to new values in J, where gamma specifies the shape of the curve describing the
relationship between the values in I and J.
ex
J = imadjust(RGB,[low_in high_in],___) maps the values in truecolor image RGB to new values in J. You can apply the same mapping or unique mappings for each color
channel.
newmap = imadjust(cmap,[low_in high_in],___) maps the values in color map cmap to new values in newmap. You can apply the same mapping or unique mappings for each
color channel.

4. imhist
Histogram of image data
collapse all in page

Syntax
[counts,binLocations] = imhist(I)
[counts,binLocations] = imhist(I,n)
[counts,binLocations] = imhist(X,map)
imhist(___)

Description
[counts,binLocations] = imhist(I) calculates the histogram for the grayscale image I. The imhist function returns the histogram counts in counts and the bin locations
in binLocations. The number of bins in the histogram is determined by the image type.
[counts,binLocations] = imhist(I,n) specifies the number of bins, n, used to calculate the histogram.
[counts,binLocations] = imhist(X,map) calculates the histogram for the indexed image X with color map map. The histogram has one bin for each entry in the color map.
ex
imhist(___) displays a plot of the histogram. If the input image is an indexed image, then the histogram shows the distribution of pixel values above a color bar of the color
map map.

5. histeq
Enhance contrast using histogram equalization
collapse all in page

Syntax
J = histeq(I,hgram)
J = histeq(I,n)
J = histeq(I)
newmap = histeq(X,map)
newmap = histeq(X,map,hgram)
[___,T] = histeq(___)

Description
ex
J = histeq(I,hgram) transforms the grayscale image I so that the histogram of the output grayscale image J with length(hgram) bins approximately matches the target
histogram hgram.
J = histeq(I,n) transforms the grayscale image I so that the histogram of the output grayscale image J with n bins is approximately flat. The histogram of J is flatter when n is
much smaller than the number of discrete levels in I.
J = histeq(I) transforms the grayscale image I so that the histogram of the output grayscale image J has 64 bins and is approximately flat.
newmap = histeq(X,map) transforms the values in the color map so that the histogram of the gray component of the indexed image X is approximately flat. The transformed color
map is newmap.
newmap = histeq(X,map,hgram) transforms the color map associated with the indexed image X so that the histogram of the gray component of the indexed image (X,newmap)
approximately matches the target histogram hgram. The histeq function returns the transformed color map in newmap. length(hgram) must be the same as size(map,1).
[___,T] = histeq(___) also returns the transformation T that maps the gray component of the input grayscale image or color map to the gray component of the output grayscale
image or color map.

6. imabsdiff
Absolute difference of two images
collapse all in page

Syntax
Z = imabsdiff(X,Y)

Description
ex
Z = imabsdiff(X,Y) subtracts each element in array Y from the corresponding element in array X and returns the absolute difference in the corresponding element of the output
array Z.
Examples
collapse all
Display Absolute Difference between Filtered image and Original
Try This Example

Read image into workspace.


I = imread('cameraman.tif');
Filter the image.
J = uint8(filter2(fspecial('gaussian'), I));
Calculate the absolute difference of the two images.
K = imabsdiff(I,J);
Display the absolute difference image.
figure
imshow(K,[])

6. imabsdiff
Subtract one image from another or subtract constant from image
collapse all in page

Syntax
Z = imsubtract(X,Y)

Description
ex
Z = imsubtract(X,Y) subtracts each element in array Y from the corresponding element in array X and returns the difference in the corresponding element of the output array Z.
Examples
collapse all
Subtract Two uint8 Arrays
Try This Example

This example shows how to subtract two uint8 arrays. Note that negative results are rounded to 0.

X = uint8([ 255 0 75; 44 225 100]);


Y = uint8([ 50 50 50; 50 50 50 ]);
Z = imsubtract(X,Y)
Z = 2x3 uint8 matrix

205 0 25
0 175 50

Subtract Image Background


Try This Example

Read a grayscale image into the workspace.


I = imread('rice.png');
Estimate the background.
background = imopen(I,strel('disk',15));
Subtract the background from the image.
J = imsubtract(I,background);
Display the original image and the processed image.
imshow(I)
7. imresize
Resize image
collapse all in page

Syntax
J = imresize(I,scale)
J = imresize(I,[numrows numcols])
[Y,newmap] = imresize(X,map,___)
___ = imresize(___,method)
___ = imresize(___,Name,Value)

Description
ex
J = imresize(I,scale) returns image J that is scale times the size of I. The input image I can be a grayscale, RGB, binary, or categorical image.
ex
J = imresize(I,[numrows numcols]) returns image J that has the number of rows and columns specified by the vector [numrows numcols].
ex
[Y,newmap] = imresize(X,map,___) resizes the indexed image X with color map map. By default, imresize returns an optimized color map, newmap, with the resized indexed
image. To return a color map that is the same as the original color map, use the Colormap name-value pair argument.
ex
___ = imresize(___,method) specifies the interpolation method used.
___ = imresize(___,Name,Value) uses name-value pair arguments to control various aspects of the resizing operation.

8. imrotate
Rotate image
collapse all in page

Syntax
J = imrotate(I,angle)
J = imrotate(I,angle,method)
J = imrotate(I,angle,method,bbox)

Description
ex
J = imrotate(I,angle) rotates image I by angle degrees in a counterclockwise direction around its center point. To rotate the image clockwise, specify a negative value
for angle. imrotate makes the output image J large enough to contain the entire rotated image. By default, imrotate uses nearest neighbor interpolation, setting the values of
pixels in J that are outside the rotated image to 0.
ex
J = imrotate(I,angle,method) rotates image I using the interpolation method specified by method.
ex
J = imrotate(I,angle,method,bbox) also uses the bbox argument to define the size of the output image. You can crop the output to the same size as the input image or return
the entire rotated image.

9. imcrop
Crop image
collapse all in page

Syntax
Icropped = imcrop

Icropped = imcrop(I)

Xcropped = imcrop(X,cmap)

___ = imcrop(h)

Icropped = imcrop(I,rect)

Xcropped = imcrop(X,cmap,rect)

___ = imcrop(xref,yref,___)

[___,rectout] = imcrop(___)

[xrefout,yrefout,___] = imcrop(___)

imcrop(___)

Description
Crop Image Interactively
Note

The interactive syntaxes do not support categorical images. For categorical images, you must specify the crop region, rect.
Icropped = imcrop creates an interactive Crop Image tool associated with the grayscale, truecolor, or binary image displayed in the current figure. imcrop returns the cropped
image, Icropped.
With this syntax and the other interactive syntaxes, the Crop Image tool blocks the MATLAB® command line until you complete the operation. For more information about using the
Crop Image tool, see Interactive Behavior.
ex
Icropped = imcrop(I) displays the grayscale, truecolor, or binary image I in a figure window and creates an interactive Crop Image tool associated with the image.
Xcropped = imcrop(X,cmap) displays the indexed image X in a figure using the color map cmap, and creates an interactive Crop Image tool associated with that
image. imcrop returns the cropped indexed image, Xcropped, which also has the color map cmap.
___ = imcrop(h) creates an interactive Crop Image tool associated with the image specified by the handle h.
Crop Image by Specifying Crop Region
ex
Icropped = imcrop(I,rect) crops the image I according to the position and dimensions specified in the crop rectangle rect. The cropped image includes all pixels in the input
image that are completely or partially enclosed by the rectangle.
The actual size of the output image does not always correspond exactly with the width and height specified by rect. For example, suppose rect is [20 20 40 30], using the
default spatial coordinate system. The upper left corner of the specified rectangle is the center of the pixel with spatial (x,y) coordinates (20,20). The lower right corner of the
rectangle is the center of the pixel with spatial (x,y) coordinates (60,50). The resulting output image has size 31-by-41 pixels, not 30-by-40 pixels.
ex
Xcropped = imcrop(X,cmap,rect) crops the indexed image X with color map cmap according to the position and dimensions specified in the crop rectangle rect. imcrop returns
the cropped indexed image, Xcropped, which also has the color map cmap.
___ = imcrop(xref,yref,___) crops the input image using the world coordinate system defined by xref and yref. After the xref and yref input arguments, you can specify the
arguments of any syntax that includes an input image I or X.
Specify Additional Output Options
ex
[___,rectout] = imcrop(___) also returns the position of the crop rectangle in rectout. You can use the input arguments of any other syntax.
[xrefout,yrefout,___] = imcrop(___) also returns the image limits of the input image in xrefout and yrefout.
imcrop(___) without output arguments displays the
cropped image in a new figure window. This syntax
does not support categorical images.

10. imfilter
N-D filtering of multidimensional images
Syntax
B = imfilter(A,h)
B = imfilter(A,h,options,...)

Description
ex
B = imfilter(A,h) filters the multidimensional
array A with the multidimensional filter h and returns
the result in B.
ex
B = imfilter(A,h,options,...) performs
multidimensional filtering according to one or more
specified options.
11. fspecial
Create predefined 2-D filter
Syntax
h = fspecial(type)
h = fspecial('average',hsize)
h = fspecial('disk',radius)
h = fspecial('gaussian',hsize,sigma)
h = fspecial('laplacian',alpha)
h = fspecial('log',hsize,sigma)
h = fspecial('motion',len,theta)
h = fspecial('prewitt')
h = fspecial('sobel')

Description
ex
h = fspecial(type) creates a two-dimensional filter h of the specified type. Some of the filter types have optional additional parameters, shown in the following
syntaxes. fspecial returns h as a correlation kernel, which is the appropriate form to use with imfilter.
h = fspecial('average',hsize) returns an averaging filter h of size hsize.
h = fspecial('disk',radius) returns a circular averaging filter (pillbox) within the square matrix of size 2*radius+1.
h = fspecial('gaussian',hsize,sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma. Not recommended.
Use imgaussfilt or imgaussfilt3 instead.
h = fspecial('laplacian',alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator, alpha controls the shape of the Laplacian.
h = fspecial('log',hsize,sigma) returns a rotationally symmetric Laplacian of Gaussian filter of size hsize with standard deviation sigma.
h = fspecial('motion',len,theta) returns a filter to approximate, once convolved with an image, the linear motion of a camera. len specifies the length of the motion
and theta specifies the angle of motion in degrees in a counter-clockwise direction. The filter becomes a vector for horizontal and vertical motions. The default len is 9 and the
default theta is 0, which corresponds to a horizontal motion of nine pixels.
h = fspecial('prewitt') returns a 3-by-3 filter that emphasizes horizontal edges by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.
[ 1 1 1

0 0 0

-1 -1 -1 ]

h = fspecial('sobel') returns a 3-by-3 filter that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. To emphasize vertical edges,
transpose the filter h'.
[ 1 2 1
0 0 0

-1 -2 -1 ]

12. bwmorph
Morphological operations on binary images
Syntax
BW2 = bwmorph(BW,operation)
BW2 = bwmorph(BW,operation,n)

Description
ex
BW2 = bwmorph(BW,operation) applies a specific morphological operation to the binary image BW.
Note

To perform morphological operations on a 3-D volumetric image, use bwmorph3.

BW2 = bwmorph(BW,operation,n) applies the operation n times. n can be Inf, in which case the operation is repeated until the image no longer changes.

13. bwperim
Find perimeter of objects in binary image
Syntax
BW2 = bwperim(BW)
BW2 = bwperim(BW,conn)

Description
ex
BW2 = bwperim(BW) returns a binary image that contains only the perimeter pixels of objects in the input image BW. A pixel is part of the perimeter if it is nonzero and it is connected
to at least one zero-valued pixel.
BW2 = bwperim(BW,conn) specifies the pixel connectivity, conn.

14. imsubstract
Subtract one image from another or subtract constant from image
Syntax
Z = imsubtract(X,Y)

Description
ex
Z = imsubtract(X,Y) subtracts each element in array Y from the corresponding element in array X and returns the difference in the corresponding element of the output array Z.

15. imreconstruct
Morphological reconstruction
Syntax
J = imreconstruct(marker,mask)
J = imreconstruct(marker,mask,conn)

Description
ex
J = imreconstruct(marker,mask) performs morphological reconstruction of the image marker under the image mask, and returns the reconstruction in J. The elements
of marker must be less than or equal to the corresponding elements of mask. If the values in marker are greater than corresponding elements in mask, then imreconstruct clips
the values to the mask level before starting the procedure.
J = imreconstruct(marker,mask,conn) performs morphological reconstruction with the specified connectivity, conn.

16. imfill
Fill image regions and holes
Syntax
BW2 = imfill(BW,locations)
BW2 = imfill(BW,locations,conn)
BW2 = imfill(BW,'holes')
BW2 = imfill(BW,conn,'holes')
I2 = imfill(I)
I2 = imfill(I,conn)
BW2 = imfill(BW)
BW2 = imfill(BW,0,conn)
[BW2, locations_out] = imfill(BW)

Description
ex
BW2 = imfill(BW,locations) performs a flood-fill operation on background pixels of the input binary image BW, starting from the points specified in locations.
BW2 = imfill(BW,locations,conn) fills the area defined by locations, where conn specifies the connectivity.
ex
BW2 = imfill(BW,'holes') fills holes in the input binary image BW. In this syntax, a hole is a set of background pixels that cannot be reached by filling in the background from the
edge of the image.
ex
BW2 = imfill(BW,conn,'holes') fills holes in the binary image BW, where conn specifies the connectivity.
ex
I2 = imfill(I) fills holes in the grayscale image I. In this syntax, a hole is defined as an area of dark pixels surrounded by lighter pixels.
ex
I2 = imfill(I,conn) fills holes in the grayscale image I, where conn specifies the connectivity.
BW2 = imfill(BW) displays the binary image BW on the screen and lets you define the region to fill by selecting points interactively with the mouse. To use this syntax, BW must be a
2-D image.
Press Backspace or Delete to remove the previously selected point. Shift-click, right-click, or double-click to select a final point and start the fill operation. Press Return to finish the
selection without adding a point.
BW2 = imfill(BW,0,conn) lets you override the default connectivity as you interactively specify locations.
[BW2, locations_out] = imfill(BW) returns the locations of points selected interactively in locations_out. To use this syntax, BW must be a 2-D image.

16. bwdist
Distance transform of binary image
Syntax
D = bwdist(BW)
[D,idx] = bwdist(BW)
[D,idx] = bwdist(BW,method)

Description
ex
D = bwdist(BW) computes the Euclidean distance transform of the binary image BW. For each pixel in BW, the distance transform assigns a number that is the distance between that
pixel and the nearest nonzero pixel of BW.
[D,idx] = bwdist(BW) also computes the closest-pixel map in the form of an index array, idx. Each element of idx contains the linear index of the nearest nonzero pixel of BW.
The closest-pixel map is also called the feature map, feature transform, or nearest-neighbor transform.
[D,idx] = bwdist(BW,method) computes the distance transform using an alternate distance metric, specified by method.

17. imcontour
Create contour plot of image data
Syntax
imcontour(I)
imcontour(I,levels)
imcontour(I,V)
imcontour(x,y,___)
imcontour(___,LineSpec)
[C,h] = imcontour(___)

Description
imcontour(I) draws a contour plot of the grayscale image I, choosing the number of levels and the values of levels automatically. imcontour automatically sets up the axes so
their orientation and aspect ratio match the image.
ex
imcontour(I,levels) specifies the number, levels, of equally spaced contour levels in the plot.
imcontour(I,V) draws contour lines at the data values specified in vector V. The number of contour levels is equal to length(V).
imcontour(x,y,___) uses the vectors x and y to specify the image x- and y coordinates.
imcontour(___,LineSpec) draws the contours using the line type and color specified by LineSpec. Marker symbols are ignored.
[C,h] = imcontour(___) returns the contour matrix, C, and the handle, h, to the contour patches drawn onto the current axes.

18. bwlabel
Label connected components in 2-D binary image
Syntax
L = bwlabel(BW)
L = bwlabel(BW,conn)
[L,n] = bwlabel(___)

Description
ex
L = bwlabel(BW) returns the label matrix L that contains labels for the 8-connected objects found in BW.
L = bwlabel(BW,conn) returns a label matrix, where conn specifies the connectivity.
[L,n] = bwlabel(___) also returns n, the number of connected objects found in BW.

19. regionprops
Measure properties of image regions
Syntax
stats = regionprops(BW,properties)

stats = regionprops(CC,properties)

stats = regionprops(L,properties)

stats = regionprops(___,I,properties)

stats = regionprops(output,___)
Description
ex
stats = regionprops(BW,properties) returns measurements for the set of properties for each 8-connected component (object) in the binary image, BW. You can
use regionprops on contiguous regions and discontiguous regions (see More About).
Note

To return measurements of a 3-D volumetric image, consider using regionprops3. While regionprops can accept 3-D images, regionprops3 calculates more statistics for 3-D
images than regionprops.
For all syntaxes, if you do not specify the properties argument, then regionprops returns the 'Area', 'Centroid', and 'BoundingBox' measurements.
stats = regionprops(CC,properties) measures a set of properties for each connected component (object) in CC, which is a structure returned by bwconncomp.
stats = regionprops(L,properties) measures a set of properties for each labeled region in label image L.
stats = regionprops(___,I,properties) returns measurements for the set of properties specified by properties for each labeled region in the image I. The first input
to regionprops (BW, CC, or L) identifies the regions in I.
ex
stats = regionprops(output,___) returns measurements for a set of properties, where output specifies the type of return value. regionprops can return measurements in
a struct array or a table.

20. edge
Find edges in intensity image
Syntax
BW = edge(I)
BW = edge(I,method)
BW = edge(I,method,threshold)
BW = edge(I,method,threshold,direction)
BW = edge(___,'nothinning')
BW = edge(I,method,threshold,sigma)
BW = edge(I,method,threshold,h)
[BW,threshOut] = edge(___)
[BW,threshOut,Gv,Gh] = edge(___)

Description
BW = edge(I) returns a binary image BW containing 1s where the function finds edges in the grayscale or binary image I and 0s elsewhere. By default, edge uses the
Sobel edge detection method.
ex
BW = edge(I,method) detects edges in image I using the edge-detection algorithm specified by method.
BW = edge(I,method,threshold) returns all edges that are stronger than threshold.
BW = edge(I,method,threshold,direction) specifies the orientation of edges to detect. The Sobel and Prewitt methods can detect edges in the vertical direction, horizontal
direction, or both. The Roberts method can detect edges at angles of 45° from horizontal, 135° from horizontal, or both. This syntax is valid only
when method is 'Sobel', 'Prewitt', or 'Roberts'.
BW = edge(___,'nothinning') skips the edge-thinning stage, which can improve performance. This syntax is valid only when method is 'Sobel', 'Prewitt', or 'Roberts'.
BW = edge(I,method,threshold,sigma) specifies sigma, the standard deviation of the filter. This syntax is valid only when method is 'log' or 'Canny'.
BW = edge(I,method,threshold,h) detects edges using the 'zerocross' method with a filter, h, that you specify. This syntax is valid only when method is 'zerocross'.
[BW,threshOut] = edge(___) also returns the threshold value.
[BW,threshOut,Gv,Gh] = edge(___) also returns the directional gradient magnitudes. For the Sobel and Prewitt methods, Gv and Gh correspond to the vertical and horizontal
gradients. For the Roberts methods, Gv and Gh correspond to the gradient at angles of 45° and 135° from horizontal, respectively. This syntax is valid only
when method is 'Sobel', 'Prewitt', or 'Roberts'.

21. rangefilt
Local range of image
Syntax
J = rangefilt(I)
J = rangefilt(I,nhood)

Description
ex
J = rangefilt(I) returns the array J, where each output pixel contains the range value (maximum value − minimum value) of the 3-by-3 neighborhood around the corresponding
pixel in the input image I.
ex
J = rangefilt(I,nhood) returns the local range of image I using the specified neighborhood, nhood.

You might also like