Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB Reading and Manipulating Images - Lecture Notes | CS 1371, Study notes of Computer Science

Section 15 - Images Material Type: Notes; Professor: Omojokun; Class: Computing for Engineers; Subject: Computer Science; University: Georgia Institute of Technology-Main Campus;

Typology: Study notes

2011/2012

Uploaded on 04/08/2012

kara-evitt
kara-evitt 🇺🇸

1 document

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Reading and Manipulating Images - Lecture Notes | CS 1371 and more Study notes Computer Science in PDF only on Docsity! Section 15: Images MATLAB is capable of reading in and manipulating images much like Notepad or Excel files from file IO, although I can’t understand why anyone would want to use MATLAB to play with images. Fortunately, there are only a few important functions and concepts to learn, so this is one of the easiest topics in the course. Image arrays are HUGE. Never forget to suppress your outputs. Background on Images: Computers store and interpret images as enormous arrays of elements called pixels. Each pixel has three values associated with it: red, green, and blue. The computer interprets different combinations of these three numbers as different colors; each individual pixel becomes one solid color, and large collections of these colors become images. The pixel values are based on mixing the three primary colors of light: red, green, and blue. All colors can be made from different combinations of those three colors of light. A pixel with all zeros would represent the absence of light, the color black, while a pixel with the highest possible values for all three would be pure white. MATLAB stores images in MxNx3 three-dimensional arrays, with the third dimension representing the three primary colors. The image itself is an array of red values on top of an array of green values on top of an array of blue values. Uint8(): Because the addition of light must be bounded by a finite limit (the color white), images are stored in a special new data type called uint8, which stands for unsigned 8-bit integer. A thorough discussion of this definition would be pointless here, so I will cover the important points. Uint8 numbers function like regular doubles except for three differences. 1. All numbers lower than the minimum value of 0 are automatically set to 0. 2. All numbers above the maximum value of 255 are automatically set to 255. 3. All decimals are automatically rounded up or down to the nearest whole number. Basically, uint8 is a data type that only allows for whole numbers ranging from 0 to 255. This makes sense in terms of images because fractional pixel values would be meaningless, and black (0) and white (255) must have finite values. Luckily, the function that converts other data types to uint8 is very easy to remember. It’s called uint8(). arr = uint8(arr) In most image problems, we will first convert the image array to doubles because they can be more easily manipulated. Usually, the final step will be to convert all the data back to type uint8. uint8(300) ans = 255; uint8(-5) ans = 0; uint8(3.5) ans = 4; class(uint8(5)) ans = ‘uint8’ Imread(): MATLAB has a built-in function to read images called imread(). Imread() takes in a string representing an image file in the current directory and outputs an image array of type uint8. Image arrays are just like regular arrays with a third dimension. The number of rows and columns will vary between images, but there will always be three layers: red, green, and blue, in that order. My shorthand for an image array is ‘im’. im = imread(filename) In the same way that two-dimensional arrays are indexed by rows followed by columns, three- dimensional arrays are indexed by rows, then columns, then layers. You can create three-dimensional arrays either by manually creating each layer or by using the cat() function, explained below. A = magic(3); im(:,:,1) = A; im(:,:,2) = A; im(:,:,3) = A; Resizing Images: It is often desirable to make an image larger or smaller without significantly changing its aggregate appearance. Unfortunately, adding or deleting random elements from the image array would either add or delete information from the image itself. The solution is to use indexing tricks to either repeat or delete evenly spaced rows and columns. To make an image twice as large, we copy every row and column an extra time; to halve the size, we delete every other row and column. The easiest way to accomplish this is by using the linspace() function to create a range of the desired number of indices and then rounding the indices to the nearest whole number. If we index the original image array at this new array of position values, MATLAB will automatically repeat or delete information—the use of linspace() ensures that this information is evenly spaced and thus difficult to notice. Here we simply set ‘x’ and ‘y’ equal to the desired effect on rows and columns, respectively. To halve the rows and double the columns, use x=0.5 and y=2. [row, col, layer] = size(im); row_index = round(linspace(1, row, row.*x)); col_index = round(linspace(1, col, col.*y)); im = im(row_index, col_index, :); imshow(im) Once again, remember to index rows, columns, AND, all three layers or imshow() will produce a grayscale image based only on a single layer. Grayscaling Images: If a certain pixel has the same intensity value for red, green, and blue, MATLAB interprets the color as gray; smaller numbers generate darker shades of gray, as we would expect. To convert a colorful image to grayscale (shades of gray varying according to the intensity of the individual pixels), just take the average value of the three layers of the image array. That average value is then repeated three times to create identical red, green, and blue layers. Remember that taking averages does not work very well with data type uint8() because it automatically rounds fractions and caps numbers at 255. Some course administrators will tell you that you can avoid this issue by dividing through by 3 first when taking averages, but this method could also potentially fail if MATLAB decides to round a number in an unexpected direction. Take my advice and convert to double and back to uint8—it will probably save you some headache in the long run. im = double(im); gray = (im(:,:,1) + im(:,:,2) + im(:,:,3))/3; im = cat(3, gray, gray, gray); im = uint8(im); imshow(im) Taking the Negative of Images: Taking the negative is a fancy phrase for reversing the intensity of every pixel—white becomes black, and black becomes white. Just subtract the entire image array from 255. negative = 255 – im; Swapping Layers: Creative indexing also gives us the ability to change the order of the color layers in a three- dimensional array. Remember waaay back from vectors that an range of indices is simply a vector of position numbers. Thus, instead of using a colon (:) to access all layers of an image, we can use a vector to swap them instead. Red=1, Green=2, and Blue=3. Let’s swap red and green. im = im(:, :, [2 1 3]); imshow(im) As you can see, the table now looks purple. Dividing Images: Dividing images into halves or quadrants is easy but tedious. This just requires indexing and heavy use of the round(), floor(), or ceil() functions to avoid decimals. I always use round(). im(1:round(end/2),:,:); Top half im(round(end/2)+1:end,:,:); Bottom half im(:,1:round(end/2),:); Left half im(:,round(end/2)+1:end,:); Right half im(1:round(end/2),1:round(end/2),:); Top right corner im(1:round(end/2),round(end/2)+1:end,:); Top right corner im(round(end/2)+1:end,1:round(end/2),:); Bottom left corner im(round(end/2)+1:end,round(end/2)+1:end,:); Bottom right corner Transposing Images and Permute(): For some unknown reason, the designers of MATLAB decided that using the apostrophe (‘) to transpose three-dimensional arrays should cause an error. You can either divide the image into three layers and transpose them individually or use the permute() function. Permute() takes in an array and a vector indicating the order you wish to rearrange the dimensions. The natural order for a three-dimensional array is [1 2 3], or rows, columns, layers. To swap rows and columns, use [2 1 3] instead. Once again, remember that transposing is fundamentally different from rotating. Notice that the lizard is now in front of Salem’s RIGHT paw. red = im(:,:,1); green = im(:,:,2); blue = im(:,:,3); trans_im = cat(3,red',green',blue'); imshow(trans_im) OR trans_im = permute(im,[2 1 3]);
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved