Lecture 5

Lecture 5 - Lecture 5. Imaging and making histograms...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 5. Imaging and making histograms Welcome to lecture 5 of Matlab. Today we are going spend some time on how to create multi ­dimensional matrices and their applications in Matlab. This lecture will cover the following topics: 1. creating and accessing multi ­dimensional matrices 2. pixels, color channels, RGB coloring 3. histograms 4. using histograms to compare images 3D Matrix in MatLab We have already explored 2D and 1D matrices in lecture 2 of this course. However, there’s no reason to limit ourselves only to those two dimensions. In fact, matrices can have many more dimensions. Today we are going to take a look at 3 dimensional matrices in Matlab. If you imagine a 2D matrix as the pages of a book, a 3D matrix would be the book. The matrix is like a stack of multiple 2D matrices.  ­ http://www.mathworks.com/help/techdoc/math/f1 ­86528.html An easy way to generate a 3D matrix is to create a 2D matrix and extend it into 3 dimensions: Example: First, lets create our 2D matrix: >> A = [ 5, 7, 8; 0, 1, 9; 4, 3, 6]; Now lets add a 3rd dimension to our 2D array >> A(:,:,2) = [ 1, 0, 4; 3, 5, 6; 9, 8, 7]; Let’s see the first page of our matrix >> A(:,:,1) ans = 5 7 8 0 1 9 4 3 6 (Recall that using the colon operator when accessing the elements in a matrix means we want to get all of the values in that row/column.) …and the second page of our matrix >> A(:,:,2) ans = 1 0 4 3 5 6 9 8 7 Notice that we call on the third dimension by trying to access another dimension besides rows and columns for the matrix A (A(:,:,2)). The first colon represents the rows and the second colon represents the columns. The number 2 then represents the extension of the matrix into three dimensions. In this case you can think about it as if we are calling on the second page of the book. Instead of directly calling on the second page, we can also use the concatenate function, cat(). The cat() function takes the form of >>cat(dimension, matrix1, matrix2) Example: >> B = cat(3, [ 5, 7, 8; 0, 1, 9; 4, 3, 6],[ 1, 0, 4; 3, 5, 6; 9, 8, 7]) B(:,:,1) = 5 7 8 0 1 9 4 3 6 B(:,:,2) = 1 0 4 3 5 6 9 8 7 The concatenate function uses the format cat(dim, matrix1, matrix2). The first value tells the function which dimension you would like to concatenate into. In this case we want to concatenate into the 3rd dimension. Note that the two 2D matrices must have matching dimensions for you to be able to concatenate them in 3D. If the rows and columns don’t match then the function will throw an error. You can also use the concatenate function to concatenate 1D and 2D matrices together instead of using the square brackets (). Example: Concatenation in 2D: >> cat(1,[1:4],[5:8]) ans = 1 2 3 4 5 6 7 8 If you type in “help cat”, you will see that “CAT(1,A,B) is the same as [A;B]” Concatenation in 1D >> cat(2,[1:4],[1]) ans = 1 2 3 4 1 Again, in “help cat”, you will see that “CAT(2,A,B) is the same as [A,B]” Notice that the two matrices must have the same dimensions perpendicular to the direction you are concatenating for you to concatenate them. You can use the concatenate function to make an n dimension matrix. Example: >> B = cat(3, [2 8; 0 5], [1 3; 7 9]) B(:,:,1) = 2 8 0 5 B(:,:,2) = 1 3 7 9 >> A = cat(3, [5 8; 4 5], [9 3; 7 0]) A(:,:,1) = 5 8 4 5 A(:,:,2) = 9 3 7 0 >> cat(4,A,B) ans(:,:,1,1) = 5 8 4 5 ans(:,:,2,1) = 9 3 7 0 ans(:,:,1,2) = 2 8 0 5 ans(:,:,2,2) = 1 3 7 9 These matrices don’t make much sense in our world, but they can be very useful in solving certain types of problems. You can use the size() function to look at the number of dimensions you have: Example: >> C = cat(4,A,B) C(:,:,1,1) = 5 8 4 5 C(:,:,2,1) = 9 3 7 0 C(:,:,1,2) = 2 8 0 5 C(:,:,2,2) = 1 3 7 9 >> size(C) ans = 2 2 2 2 Note that the number of terms in the size function tells you the number of dimensions is in the matrix and the numbers tells you the size of each of these dimensions (like number of rows and number of columns). Accessing 3D Matrices Accessing values in 3D matrices is very similar to accessing values in 2D matrices. Remember there are 2 ways to access the same value. One way is to use the dimensions of the matrix to locate the value as if it is a point on the coordinate plane: Example: Given the 3D matrix A >> A A(:,:,1) = 5 8 4 5 A(:,:,2) = 9 3 7 0 To access the element at the first row, first column, second page, use >> A(1,1,2) ans = 9 Another way is to access the value through the index of the value (Using the same 3D matrix as above): Example: >> A(5) ans = 9 You can also call on specific rows, columns, or planes(pages) by using the colon operator (:). Example: Accessing a row: >> A(1,:,1) ans = 5 8 Accessing a column >> A(:,1,2) ans = 9 7 Accessing a plane(page): >> A(:,1,:) ans(:,:,1) = 5 4 ans(:,:,2) = 9 7 Now that we know what a 3D matrix in Matlab is and how to create and access the elements in them, it’s time for us to look at an application of it. Images in Matlab What is a pixel? A pixel is the smallest unit of the picture that can be manipulated. Each square above is a pixel. Images in any computer are represented by many small points of color. When the points are small enough we can represent almost any amount of detail. See example below: The above image is made up of hundreds of thousands pixels. The simplest way to represent an image is using 1 ­bit (a bit can be either 0 or 1) monochrome (black and white). Imagine taking an n*n matrix. Each element in the matrix is either a 1 or 0 where 1 is white and 0 is black. By placing the white and black in specific positions of a large enough matrix you can get the following picture. A slightly more complicated way to represent an image is using an 8 ­bit (1 ­byte) grayscale. In this case each element of your n*n matrix actually takes 8 bits (or eight 1’s and 0’s), and represents the intensity of light from white to black. Since eight 1’s or 0’s can represent a total of 2^8 = 256 different numbers, there is a total of 256 different intensities you can have between totally black (00000000) and totally white (11111111). Usually we use 8 bits to represent the numbers from 0 to 255. This allows you to create more complicated images as seen in the example below: To store colors in computers we use color channels. The three colors that we use for computers are: red, green, and blue (commonly referred to as RGB). When the colors are mixed properly your eye can perceive any color in the rainbow. Imagine your screen as made up of a matrix of pixels. Behind each pixel are three color channels. Each channel will output a specified intensity of red, green, or blue light into the pixels. The mixed colors will allow you to see some color on that pixel. Thus we need a total of three bytes of info for each pixel, one byte for each color channel. To properly represent a colored picture in Matlab we would need to use a 3 ­D array, specifically an n*n*3 array. Each separate page of the array is like a grayscale image where each pixel specifies some intensity. However, each intensity specified this time is for a color channel. The first page is red, the second page is green, and the third page is blue. See the example below: To call on a image in Matlab, use the imread() function. Save the following image as a .jpeg file to your Matlab folder and name it “dice.jpg”. Type the following into the command window: >> x = imread('dice.jpg'); Note: Make sure ‘dice.jpg’ is in your current directory folder. (Make use to add the semicolon to suppress the output or you will be seeing a lot of numbers in your command window!) When you type the above into Matlab after saving the file, the picture will be stored a 3 ­D matrix in the variable x. If you check on the size of x you will get the following: >>size(x) ans = 1200 1920 3 This says that the image is made of 1200 rows and 1920 and 3 pages. To show the matrix x as a picture in MatLab use the image() function. >> image(x) % should display the dice picture as a figure in MatLab. We can access the 3 ­D image matrix as we have before. Try image(x(:,:,1)). This should show you what the picture looks like when only the red channel is shown. To call on the other channels simply call on the 2nd and 3rd pages of the 3 ­D matrix. >> x(1,1,1) ans = 17 % Here we called on the intensity of the red channel for the pixel in row one and column one of the picture. You can directly change pictures by changing the intensity each channel of the picture. Example: >> x(1:100,1:100,1) = 255; >> x(1:100,1:100,2) = 255; >> x(1:100,1:100,3) = 255; >> image(x) Here we have set reset the intensities in the upper left corner of the pictures by simply changing the value of the intensity. When you image(x) you should see a white square in the upper left corner of your picture. See example below: 200 400 600 800 1000 1200 200 400 600 800 1000 1200 1400 1600 1800 Histograms in Matlab Recall the histogram function that we have briefly mentioned in the last lecture. The histogram function in MatLab comes in the form of “N = hist(array,bins)”. The function takes two inputs, the array of numbers that you want to graph and the number of bins/groups you want to separate your numbers into. The hist function gives you the frequency of the numbers that appears in each bin/group. If you do not specify an output, the function will automatically plot the histogram. If you do specify an output, then the output will store the frequency data in a row vector where each element represents the frequency of numbers that appears in a specific bin/group of numbers. Example: >> hist(randn(1,10000),100); 350 300 250 200 150 100 50 0 -4 -3 -2 -1 0 1 2 3 4 5 Typing the line above in Matlab should provide the graph above. randn() is a function that randomly gives values based on the Gaussian distribution. Thus the graph should appear as the commonly seen Gaussian bell curve. Setting the hist function to an actual output gives stores the frequency data into the output variable name as a row vector. Example: >> x = randn(1,10000); >> N = hist(x, 100) N = Columns 1 through 10 2 0 5 3 1 2 3 7 7 11 Columns 11 through 20 14 16 16 18 16 26 30 34 44 53 Columns 21 through 30 44 73 82 93 107 112 130 142 149 155 Columns 31 through 40 170 197 203 190 232 235 257 292 318 274 Columns 41 through 50 303 288 305 298 315 310 297 303 282 294 Columns 51 through 60 247 274 258 229 263 217 181 186 179 144 Columns 61 through 70 127 131 99 105 94 76 77 55 56 34 Columns 71 through 80 37 38 28 27 11 14 9 6 8 7 Columns 81 through 90 5 2 4 1 2 4 3 1 1 1 Columns 91 through 100 0 0 0 0 0 0 0 0 0 1 You can plot(N) to look at what the peaks of the histogram should be. Example: >> plot(N) Image histograms in Matlab Image histograms work a little differently from hist in Matlab. The built in function imhist() can graph one color channel of your picture into a histogram with 256 bins. There are 256 bins because there is a total of 256 different intensities for each color in MatLab. The function graphs the frequency of the appearance of each intensity value. Note: the imhist() function can only graph one color channel at a time so you can only input one page of your 3 ­D image matrix at a time. Example: x 10 3 >> x = imread('dice.jpg'); 2.5 >> imhist(x(:,:,1)) 2 1.5 4 1 0.5 0 0 50 100 150 200 250 Here we have graphed the red channel of the dice picture using the imhist() function. We see a large frequency of high intensity red in the picture which makes sense since most of the picture is white and white is made from pure red + pure green + pure blue. Image Matching Using Histograms Imagine a robot moving through New York City. When it is on the street, it can access military satellites and calculate its position using GPS to within a few centimeters of its actual position. However, once the robot enters a building and loses a direct line of sight with the satellites, the GPS will no longer works. The robot immediately becomes lost. How can we avoid this problem? One idea that robotic scientists have been trying out is matching images. If the robot had a memory bank of pictures at various points in the building, the robot could match the scenes it sees with the pictures in the memory bank to figure out where in the building it is. The robots use omnicams, a catadioptric camera, to see. A catadioptric system is a system that uses both lenses and mirrors. When positioned correctly it can give you a 360 degrees image of the world. See examples below: However, if you take an image and rotate it X degrees (lets say…45 degrees), you will find that you have a different picture. At least you can’t match the pictures pixel by pixel without rotating one of the pictures. The robot will face this problem when comparing the pictures. If the robot is in a slightly different orientation each time it is at the same area, we will be unable to match the pictures taken. However, the histograms generated from pictures will be the same even if the pictures are rotated. (It is still the same image, the color of the image does not change, only the orientation has changed. Therefore, the color channels will remain the same even if you rotate the picture!) Thus we can make a data bases of image histograms. Each time the robot takes a picture it can then compare histograms together to find its position. To compare two histograms, we can try to subtract one histogram from another and find the sum of the difference. If the difference is 0, then we are looking at the same image here. However, when you compare the histograms you need to be aware that just summing up the total difference for each pixel will not allow you to compare images of the same size. If there is the same amount of pixels for two images, then the sum of the difference for the frequency of each intensity will always equal 0. To get around this problem, make sure to square the difference or take the absolute value of the difference and then find the sum. Example: 9.jpg 5.jpg >> x = imread('5.jpg'); >> y = imread('9.jpg'); >> N = imhist(x(:,:,1)); >> M = imhist(y(:,:,1)); >> sum(N ­M) ans = 0 Note: By assigning a variable (N and M) to the output of the imhist() function, we saving the frequency data of the particular color channel into the variable. Since the pictures have the same number of pixels, even if they are completely different pictures the sum of the difference of the histograms will always be zero. >> sum((N ­M).^2) ans = 113399426 >> sum(abs(N ­M)) ans = 31082 To get a number that actually compares how close two pictures are use the squared difference or the absolute difference. ...
View Full Document

Ask a homework question - tutors are online