Está en la página 1de 17

APPENDIX A

Introduction to image processing in Matlab 1 by Kristian Sandberg, Department of Applied Mathematics, University of Colorado at Boulder Introduction This worksheet is an introduction on how to handle images in Matlab. When working with images in Matlab, there are many things to keep in mind such as loading an image, using the right format, saving the data as different data types, how to display an image, conversion between different image formats, etc. This worksheet presents some of the commands designed for these operations. Most of these commands require you to have the Image processing tool box installed with Matlab. To find out if it is installed, type ver at the Matlab prompt. This gives you a list of what tool boxes that are installed on your system. For further reference on image handling in Matlab you are recommended to use Matlab's help browser. There is an extensive (and quite good) on-line manual for the Image processing tool box that you can access via Matlab's help browser. The first sections of this worksheet are quite heavy. The only way to understand how the presented commands work, is to carefully work through the examples given at the end of the worksheet. Once you can get these examples to work, experiment on your own using your favorite image! Fundamentals A digital image is composed of pixels which can be thought of as small dots on the screen. A digital image is an instruction of how to color each pixel. We will see in detail later on how this is done in practice. A typical size of an image is 512-by-512 pixels. Later on in the course you will see that it is convenient to let the dimensions of the image to be a power of 2. For example, 29=512. In the general case we say that an image is of size m-by-n if it is composed of m pixels in the vertical direction and n pixels in the horizontal direction. Let us say that we have an image on the format 512-by-1024 pixels. This means that the data for the image must contain information about 524288 pixels, which requires a lot of memory! Hence, compressing images is essential for efficient image processing. You will later on see how Fourier analysis and Wavelet analysis can help us to compress an image significantly. There are also a few "computer scientific" tricks (for example entropy coding) to reduce the amount of data required to store an image. Image formats supported by Matlab The following image formats are supported by Matlab: BMP HDF JPEG PCX TIFF XWB Most images you find on the Internet are JPEG-images which is the name for one of the most widely used compression standards for images. If you have stored an image you can usually see from the suffix what format it is stored in. For example, an image named myimage.jpg is stored in the JPEG format and we will see later on that we can load an image of this format into Matlab. Working formats in Matlab

If an image is stored as a JPEG-image on your disc we first read it into Matlab. However, in order to start working with an image, for example perform a wavelet transform on the image, we must convert it into a different format. This section explains four common formats. Intensity image (gray scale image) This is the equivalent to a "gray scale image" and this is the image we will mostly work with in this course. It represents an image as a matrix where every element has a value corresponding to how bright/dark the pixel at the corresponding position should be colored. There are two ways to represent the number that represents the brightness of the pixel: The double class (or data type). This assigns a floating number ("a number with decimals") between 0 and 1 to each pixel. The value 0 corresponds to black and the value 1 corresponds to white. The other class is called uint8 which assigns an integer between 0 and 255 to represent the brightness of a pixel. The value 0 corresponds to black and 255 to white. The class uint8 only requires roughly 1/8 of the storage compared to the class double. On the other hand, many mathematical functions can only be applied to the double class. We will see later how to convert between double and uint8. Binary image This image format also stores an image as a matrix but can only color a pixel black or white (and nothing in between). It assigns a 0 for black and a 1 for white. Indexed image This is a practical way of representing color images. (In this course we will mostly work with gray scale images but once you have learned how to work with a gray scale image you will also know the principle how to work with color images.) An indexed image stores an image as two matrices. The first matrix has the same size as the image and one number for each pixel. The second matrix is called the color map and its size may be different from the image. The numbers in the first matrix is an instruction of what number to use in the color map matrix. RGB image This is another format for color images. It represents an image with three matrices of sizes matching the image format. Each matrix corresponds to one of the colors red, green or blue and gives an instruction of how much of each of these colors a certain pixel should use. Multiframe image In some applications we want to study a sequence of images. This is very common in biological and medical imaging where you might study a sequence of slices of a cell. For these cases, the multiframe format is a convenient way of working with a sequence of images. In case you choose to work with biological imaging later on in this course, you may use this format. How to convert between different formats The following table shows how to convert between the different formats given above. All these commands require the Image processing tool box! Image format conversion (Within the parenthesis you type the name of the image you wish to convert.) Operation: Matlab command: Convert between intensity/indexed/RGB format to binary format. dither() Convert between intensity format to indexed format. gray2ind() Convert between indexed format to intensity format. ind2gray() Convert between indexed format to RGB format. ind2rgb()

Convert a regular matrix to intensity format by scaling. mat2gray() Convert between RGB format to intensity format. rgb2gray() Convert between RGB format to indexed format. rgb2ind() The command mat2gray is useful if you have a matrix representing an image but the values representing the gray scale range between, let's say, 0 and 1000. The command mat2gray automatically re scales all entries so that they fall within 0 and 255 (if you use the uint8 class) or 0 and 1 (if you use the double class). How to convert between double and uint8 When you store an image, you should store it as a uint8 image since this requires far less memory than double. When you are processing an image (that is performing mathematical operations on an image) you should convert it into a double. Converting back and forth between these classes is easy. I=im2double(I); converts an image named I from uint8 to double. I=im2uint8(I); converts an image named I from double to uint8. How to read files When you encounter an image you want to work with, it is usually in form of a file (for example, if you down load an image from the web, it is usually stored as a JPEG-file). Once we are done processing an image, we may want to write it back to a JPEG-file so that we can, for example, post the processed image on the web. This is done using the imread and imwrite commands. These commands require the Image processing tool box! Reading and writing image files Matlab Operation: command: Read an image. (Within the parenthesis you type the name of the image file you wish to read. imread() Put the file name within single quotes ' '.) Write an image to a file. (As the first argument within the parenthesis you type the name of the image you have worked with. imwrite( , ) As a second argument within the parenthesis you type the name of the file and format that you want to write the image to. Put the file name within single quotes ' '.) Make sure to use semi-colon ; after these commands, otherwise you will get LOTS OF number scrolling on you screen... The commands imread and imwrite support the formats given in the section "Image formats supported by Matlab" above. Loading and saving variables in Matlab This section explains how to load and save variables in Matlab. Once you have read a file, you probably convert it into an intensity image (a matrix) and work with this matrix. Once you are done you may want to save the matrix representing the image in order to continue to work with this matrix at another time. This is easily done using the commands save and load. Note that save and load are commonly used Matlab commands, and works independently of what tool boxes that are installed. Loading and saving variables

Operation: Matlab command: Save the variable X . save X Load the variable X . load X Examples In the first example we will down load an image from the web, read it into Matlab, investigate its format and save the matrix representing the image. Example 1. Down load the following image (by clicking on the image using the right mouse button) and save the file as cell1.jpg. This is an image of a cell taken by an electron microscope at the Department of Molecular, Cellular and Developmental Biology at CU. Now open Matla and make sure you are in the same directory as your stored file. (You can check what files your directory contains by typing ls at the Matlab prompt. You change directory using the command cd.) Now type in the following commands and see what each command does. (Of course, you do not have to type in the comments given in the code after the % signs.) I=imread('cell1.jpg'); % Load the image file and store it as the variable I. whos % Type "whos" in order to find out the size and class of all stored variables. save I % Save the variable I. ls % List the files in your directory. % There should now be a file named "I.mat" in you directory % containing your variable I. Note that all variables that you save in Matlab usually get the suffix .mat. Next we will see that we can display an image using the command imshow. This command requires the image processing tool box. Commands for displaying images will be explained in more detail in the section "How to display images in Matlab" below. clear % Clear Matlab's memory. load I % Load the variable I that we saved above. whos % Check that it was indeed loaded. imshow(I) % Display the image

I=im2double(I); % Convert the variable into double. whos % Check that the variable indeed was converted into double % The next procedure cuts out the upper left corner of the image % and stores the reduced image as Ired. for i=1:256 for j=1:256 Ired(i,j)=I(i,j); end end whos % Check what variables you now have stored. imshow(Ired) % Display the reduced image. Example 2 Go to the CU home page and down load the image of campus with the Rockies in the background. Save the image as pic-home.jpg Next, do the following in Matlab. (Make sure you are in the same directory as your image file). clear A=imread('pic-home.jpg'); whos imshow(A) Note that when you typed whos it probably said that the size was 300x504x3. This means that the image was loaded as an RGB image (see the section "RGB image above"). However, in this course we will mostly work with gray scale images, so let us convert it into a gray scale (or "intensity") image. A=rgb2gray(A); % Convert to gray scale whos imshow(A) Now the size indicates that our image is nothing else than a regular matrix. Note: In other cases when you down load a color image and type whos you might see that there is one matrix corresponding to the image size and one matrix called map stored in Matlab. In that case, you have loaded an indexed image (see section above). In order to convert the indexed image into an intensity (gray scale) image, use the ind2gray command described in the section "How to convert between different formats" above. How to display an image in Matlab Here are a couple of basic Matlab commands (do not require any tool box) for displaying an image. Displaying an image given on matrix form Operation: Matlab command: Display an image represented as the matrix X. imagesc(X) Adjust the brightness. s is a parameter such that brighten(s) -1<s<0 gives a darker image, 0<s<1 gives a brighter image. Change the colors to gray. colormap(gray)

Sometimes your image may not be displayed in gray scale even though you might have converted it into a gray scale image. You can then use the command colormap(gray) to "force" Matlab to use a gray scale when displaying an image. If you are using Matlab with an Image processing tool box installed, I recommend you to use the command imshow to display an image. Displaying an image given on matrix form (with image processing tool box) Operation: Matlab command: Display an image represented as the matrix X. imshow(X) Zoom in (using the left and right mouse button). zoom on Turn off the zoom function. zoom off http://amath.colorado.edu/courses/5720/2000Spr/Labs/Worksheets/Matlab_tutorial/matlabimp r.html

APPENDIX B

INPUT IMAGE (DISPLAYED)

Woman, 75 y.o. Nodule in the apex of the right lobe (22.3 x 22.6 x 32.4 mm). The structure is very hypoechoic, dishomogeneous, with irregular and polycyclic borders. Right cervical lymphatic node (17.0 x 25.2 x 30.5) diffusely heterogeneous with not defined margins. OUTPUT

APPENDIX C
Thyroid cancer
Tumor - thyroid; Cancer - thyroid
Last reviewed: February 28, 2011.

Thyroid cancer is a cancer that starts in the thyroid gland. The thyroid gland is located inside the front of your lower neck.

Causes, incidence, and risk factors


Thyroid cancer can occur in all age groups. People who have had radiation therapy to the neck are at higher risk. Radiation therapy was commonly used in the 1950s to treat enlarged thymus glands, adenoids and tonsils, and skin disorders. People who received radiation therapy as children are at increased risk for developing thyroid cancer. Other risk factors are a family history of thyroid cancer and chronic goiter. There are several types of thyroid cancer: Anaplastic carcinoma (also called giant and spindle cell cancer) is the most dangerous form of thyroid cancer. It is rare, and does not respond to radioiodine therapy. Anaplastic carcinoma spreads quickly. Follicular carcinoma accounts for about 10% of all cases and is more likely to come back and spread. Medullary carcinoma is a cancer of nonthyroid cells that are normally present in the thyroid gland. This form of the thyroid cancer tends to occur in families. It has been linked with several specific genetic mutations. It requires different treatment than other types of thyroid cancer. Papillary carcinoma is the most common type, and usually affects women of childbearing age. It spreads slowly and is the least dangerous type of thyroid cancer.

Symptoms
Symptoms vary depending on the type of thyroid cancer, but may include: Cough Difficulty swallowing Enlargement of the thyroid gland Hoarseness or changing voice Neck swelling Thyroid lump (nodule)

Signs and tests

Your doctor will perform a physical exam. This may reveal a lump in the thyroid, or swollen lymph nodes in the neck. The following tests may be done: Calcitonin blood test to check for medullary cancer Thyroglobulin blood test to check for papillary or follicular cancer Laryngoscopy Thyroid biopsy Thyroid scan Ultrasound of the thyroid This disease may also affect the results of the following thyroid function tests: T3 T4 TSH

Treatment

Treatment depends on the type of thyroid cancer. Surgery is most often done. The entire thyroid gland is usually removed. If the doctor suspects that the cancer has spread to lymph nodes in the neck, these will also be removed during surgery. Radiation therapy may be performed using external beam (x-ray) radiation or by taking radioactive iodine by mouth. It may be done with or without surgery.

After treatment, you need to take thyroid hormone to replace what your glands used to make. The dose is usually a little higher than what your body needs, which helps keep the cancer from coming back. If the cancer does not respond to surgery or radiation and has spread to other parts of the body, chemotherapy may be used, but this is only effective for a small number of patients.

Expectations (prognosis)
Anaplastic carcinoma has the worst outcome of all the types of thyroid cancer. It is usually deadly despite aggressive treatment. Follicular carcinomas are often fast growing and may spread to other tissues, but the outlook is still good. Most patients are cured. The outcome with medullary carcinoma varies. Women under age 40 have a better chance of a good outcome. Papillary carcinomas are usually slower growing. Most people are cured and have a normal life expectancy. Patients who have treatment for thyroid cancer must take thyroid hormone pills for the rest of their lives.

Complications
Complications of thyroid cancer may include: Injury to the voice box and hoarseness after thryoid surgery Low calcium levels from accidental removal of the parathyroid glands during surgery Spread of the cancer to the lungs, bones, or other parts of the body

Calling your health care provider Prevention

Call your health care provider if you notice a lump in your neck. There is no known prevention. Awareness of risk (such as previous radiation therapy to the neck) can allow earlier diagnosis and treatment. Sometimes, people with strong family histories and genetic mutations related to thyroid cancer will have their thyroid gland removed for preventative purposes.

References 1. Ladenson P, Kim M. Thyroid. In: Goldman L, Ausiello D, eds. Cecil Medicine. 23rd
ed. Philadelphia, Pa: Saunders Elsevier; 2007:chap 244. 2. National Comprehensive Cancer Network. NCCN Clinical Practice Guidelines in Oncology: Thyroid Cancer. Version 1.2011.

http://www.ncbi.nlm.nih.gov/pubmedhealth/PMH0002193/ std Standard deviation Syntax


s = std(X) s = std(X,flag) s = std(X,flag,dim)

Definition There are two common textbook definitions for the standard deviation s of a data vector X. and is the number of elements in the sample. The two forms of the equation differ only in versus in the divisor. Description s = std(X), where X is a vector, returns the standard deviation using (1) above. The result s is the square root of an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples. If X is a matrix, std(X) returns a row vector containing the standard deviation of the elements of each column of X. If X is a multidimensional array, std(X) is the standard deviation of the elements along the first nonsingleton dimension of X.

for flag = 0, is the same as std(X). For flag = 1, std(X,1) returns the standard deviation using (2) above, producing the second moment of the set of values about their mean. s = std(X,flag,dim) computes the standard deviations along the dimension of X specified by scalar dim. Set flag to 0 to normalize Y by n-1; set flag to 1 to normalize by n. Examples For matrix X X= 1 5 9 7 15 22 s = std(X,0,1) s= 4.2426 7.0711 9.1924 s = std(X,0,2) s= 4.000 7.5056 area Filled area 2-D plot GUI Alternatives To graph selected variables, use the Plot Selector in the Workspace Browser, or use the Figure Palette Plot Catalog. Manipulate graphs in plot edit mode with the Property Editor. For details, see Plotting Tools Interactive Plotting in the MATLAB Graphics documentation and Creating Graphics from the Workspace Browser in the MATLAB Desktop Tools and Development Environment documentation.
s = std(X,flag)

Syntax
area(Y) area(X,Y) area(...,basevalue) area(...,'PropertyName',PropertyValue,...) area(axes_handle,...) h = area(...) hpatches = area('v6',...)

Description An area graph displays elements in Y as one or more curves and fills the area beneath each curve. When Y is a matrix, the curves are stacked showing the relative contribution of each row element to the total height of the curve at each x interval. area(Y) plots the vector Y or the sum of each column in matrix Y. The x-axis automatically scales to 1:size(Y,1). area(X,Y) For vectors X and Y, area(X,Y) is the same as plot(X,Y) except that the area between 0 and Y is filled. When Y is a matrix, area(X,Y) plots the columns of Y as filled areas. For each X, the net result is the sum of corresponding values from the columns of Y. If X is a vector, length(X) must equal length(Y). If X is a matrix, size(X) must equal size(Y). area(...,basevalue) specifies the base value for the area fill. The default basevalue is 0. See the BaseValue property for more information. area(...,'PropertyName',PropertyValue,...) specifies property name and property value pairs for the patch graphics object created by area. area(axes_handle,...) plots into the axes with the handle axes_handle instead of into the current axes (gca).

h = area(...)

returns handles of areaseries graphics objects.

Backward-Compatible Version hpatches = area('v6',...) returns the handles of patch objects instead of areaseries objects for compatibility with MATLAB 6.5 and earlier. Note The v6 option enables users of MATLAB Version 7.x of to create FIG-files that previous versions can open. It is obsolete and will be removed in a future version of MATLAB. See Plot Objects and Backward Compatibility for more information. Areaseries Objects Creating an area graph of an m-by-n matrix creates n areaseries objects (i.e., one per column), whereas a 1-by-n vector creates one area object. Some areaseries object properties that you set on an individual areaseries object set the values for all areaseries objects in the graph. See the property descriptions for information on specific properties. Examples Stacked Area Graph This example plots the data in the variable Y as an area graph. Each subsequent column of Y is stacked on top of the previous data. The figure colormap controls the coloring of the individual areas. You can explicitly set the color of an area using the EdgeColor and FaceColor properties. Y = [1, 5, 3; 3, 2, 7; 1, 5, 3; 2, 6, 1]; area(Y) grid on colormap summer set(gca,'Layer','top') title 'Stacked Area Plot' Adjusting the Base Value The area function uses a y-axis value of 0 as the base of the filled areas. You can change this value by setting the area BaseValue property. For example, negate one of the values of Y from the previous example and replot the data. Y(3,1) = -1; % Was 1 h = area(Y); set(gca,'Layer','top') grid on colormap summer The area graph now looks like this: Adjusting the BaseValue property improves the appearance of the graph: set(h,'BaseValue',-2) Setting the BaseValue property on one areaseries object sets the values of all objects. Specifying Colors and Line Styles You can specify the colors of the filled areas and the type of lines used to separate them. h = area(Y,-2); % Set BaseValue via argument set(h(1),'FaceColor',[.5 0 0])

set(h(2),'FaceColor',[.7 0 0]) set(h(3),'FaceColor',[1 0 0]) set(h,'LineStyle',':','LineWidth',2) % Set all to same value var Variance Syntax
V V V V = = = = var(X) var(X,1) var(X,w) var(X,w,dim)

Description returns the variance of X for vectors. For matrices, var(X)is a row vector containing the variance of each column of X. For N-dimensional arrays, var operates along the first nonsingleton dimension of X. The result V is an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples. var normalizes V by N-1 if N>1, where N is the sample size. This is an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples. For N=1, V is normalized by N. V = var(X,1) normalizes by N and produces the second moment of the sample about its mean.var(X,0) is equivalent to var(X). V = var(X,w) computes the variance using the weight vector w. The length of w must equal the length of the dimension over which var operates, and its elements must be nonnegative. The elements of w must be positive. var normalizes w to sum of 1. V = var(X,w,dim) takes the variance along the dimension dim of X. Pass in 0 for w to use the default normalization by N-1, or 1 to use N. The variance is the square of the standard deviation (STD). bwperim Find perimeter of objects in binary image Syntax
V = var(X) BW2 = bwperim(BW1) BW2 = bwperim(BW1, conn)

Description
BW2 = bwperim(BW1) returns a binary image containing only the perimeter pixels of objects in the input image BW1. A pixel is part of the perimeter if it is nonzero and it is connected to at

least one zero-valued pixel. The default connectivity is 4 for two dimensions, 6 for three dimensions, and conndef(ndims(BW), 'minimal') for higher dimensions. BW2 = bwperim(BW1, conn) specifies the desired connectivity. conn can have any of the following scalar values. Value Meaning Two-dimensional connectivities 4 8 4-connected neighborhood 8-connected neighborhood

Three-dimensional connectivities 6 6-connected neighborhood

Value Meaning 18 26 18-connected neighborhood 26-connected neighborhood

Connectivity can also be defined in a more general way for any dimension by using for conn a 3-by-3-by-...-by-3 matrix of 0's and 1's. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric about its center element. Class Support BW1 must be logical or numeric, and it must be nonsparse. BW2 is of class logical. Example Find the perimeter of objects in an image mask. BW1 = imread('circbw.tif'); BW2 = bwperim(BW1,8); imshow(BW1) figure, imshow(BW2) var Variance Syntax
V V V V = = = = var(X) var(X,1) var(X,w) var(X,w,dim)

Description returns the variance of X for vectors. For matrices, var(X)is a row vector containing the variance of each column of X. For N-dimensional arrays, var operates along the first nonsingleton dimension of X. The result V is an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples. var normalizes V by N-1 if N>1, where N is the sample size. This is an unbiased estimator of the variance of the population from which X is drawn, as long as X consists of independent, identically distributed samples. For N=1, V is normalized by N. V = var(X,1) normalizes by N and produces the second moment of the sample about its mean.var(X,0) is equivalent to var(X). V = var(X,w) computes the variance using the weight vector w. The length of w must equal the length of the dimension over which var operates, and its elements must be nonnegative. The elements of w must be positive. var normalizes w to sum of 1. V = var(X,w,dim) takes the variance along the dimension dim of X. Pass in 0 for w to use the default normalization by N-1, or 1 to use N. The variance is the square of the standard deviation (STD).
V = var(X)

También podría gustarte