Introduction to MATLAB with Image Processing Toolbox
This session is an introduction to MATLAB®, a high-level language and interactive environment for numerical computation, visualization, and programming.
MATLAB includes built-in mathematical functions fundamental to solving engineering and scientific problems, and an interactive environment ideal for iterative exploration, design, and problem solving. An image processing example is used to show you how to get started using MATLAB.
- Interactively importing and visualizing image data from files and webcams
- Iteratively developing an image processing algorithm
- Automating your work with scripts
- Sharing your results with others by automatically creating reports
Recorded: 26 Mar 2014
Hello, and welcome to the introduction to MATLAB. My name is Andy The, and I'm a product marketing manager at MathWorks. In this session, we're going to give you an introduction to MATLAB by using an image processing example. But before we get started, let's go ahead and talk about MATLAB a little bit.
MATLAB is essentially a high-level language. But more than that, it's also a full interactive development environment. This allows you the ability to do scripting, to do numerical computation, to do visualization like you see on the right-hand side, using plots and a lot of different type of graphing tools. And also, it allows you to do algorithm development and programming, and finally application deployment.
So let's take a step back and talk about a typical technical computing workflow. Traditionally, you'll start with accessing data. And this can come in the form of files, maybe output from third-party software, maybe even hardware. Once you acquire data, you're then going to enter in what we call the exploration and discovery phase. This is where you're going to do a lot of algorithm development and do things like graphing, finding trends, and developing your application. And then finally, going to share it. You're going to share your ideas. You're going to share your algorithms with either co-workers in the form of reports and documentation, or maybe in the form of outputs such as C files, libraries, things that can be consumed by other third-party applications.
So although I've shown this as a linear path, in reality this is an iterative path. You're usually moving between these different phases, increasing the sizes of your datasets, doing more development, refining the algorithm and application, sharing the results, getting feedback, and then again doing more refinement. So what I'm going to show you in this session is that MATLAB lends itself very well to this kind of workflow.
The goal of this demonstration today is to build an intruder detection system. Using MATLAB and Image Processing Toolbox we're going to explore images to create and share this application. We're going to work first with static images. We're going to publish reports to help share the algorithm. And then finally, we're going to work with live video.
So let me give you a preview of what we'll be building. If you look here, this is actually the application running with a nice user interface. We have the algorithm running. Here's our reference image with no intruder. And on the right-hand side, you see our intruder highlighted in red. So in the end, we're going to see how we put this entire application together.
Here we are in MATLAB. And for those of you who are new to the environment, I'll go over the different features as we come across them throughout the demonstration. So let's go ahead and get started. Here at the command window, this is a great place where you can start interacting with the environment. For example, you can start assigning some variables, such as a equals 3, b equals 5. And we can create another variable c and set that equal to a times b. And so nothing very complex—we have the answer, 15.
But what I wanted to point out is the fact that MATLAB stores all of these variables in the workspace. And if I double click to open this up, you'll see here the variables I created, a equals 3, b and so on. And this is where you can store them; you can work and perform operations on them. But more importantly, if I double click on this, you'll see something interesting. MATLAB essentially treats all of the variables as though they're matrices or two-dimensional arrays. So in this case, the variable a is a single value 3, but it's a one-by-one matrix. So this is very beneficial when you do things like linear algebra, or in our case, image processing, where every image is essentially a matrix.
So let's do a little more math just to show you what we can do. And I created this little shortcut button to clear my environment in the workspace. And by the way, you can create shortcuts of your liking. You can create a label here. You can enter in any commonly entered commands that you might enter. And this just helps you with your productivity to speed you along.
Now I'm going to create a matrix. Let's go ahead and call it matrix 1. And we're going to create a three-by-three matrix. So for the sake of time, I'm just going to do some auto completion here. Okay, and now if I hit Return, you'll see it's a simple three-by-three matrix. And you'll see it's 1's and a 0 in the center. And we can actually plot this. If we look down here in the workspace—let me go ahead and open this up.
So I didn't show you this before, but now that we have some more interesting data here, you can actually right click. And you can see I can pull up some more variables, such as what's the standard deviation, the mean, the median. So as this matrix gets larger and more complicated, you can actually provide some interesting properties to be shown right inside of the workspace.
But now we want to plot this. So if I go ahead and highlight that and go to the Plots tab, you'll see all the relevant plots that are associated with this data type. In our case, we want to treat this like an image. So I could use imshow. This is the equivalent of a printf in the imaging world. You simply are peeking or looking at the values. But I want to use the image tool, or imtool, because this is such a tiny little image. As you'll see, this image tool allows me to zoom in.
So here I am zooming into the image. You'll see nothing very interesting. But you can do such things, with the image tool, as measure. So here you can see I can measure this little black square. It's about 1.39 pixels across. I can actually play with contrast. I can crop the image. But what's also interesting—let me go ahead and right size this a little bit—I can click on this button right here to inspect pixel values. So if I click on that, here it's showing you that this is my matrix. All the different elements—here all the 1's are the white. And the center is 0, which is black. So that's essentially a picture. It's not a very interesting one. But nonetheless, my little matrix is a picture.
So here we go. I'm going to now perform some operations. So remember, I mentioned MATLAB treats everything as though it's a two-dimensional array or matrix. You can do operations on the entire array. So here we're going to go ahead and perform some logical operations. For example, I'm going to create another matrix—2. And I'm going to set this equal to everything in matrix 1 that is, let's say, less than 1.
So what I'm going to do here is, essentially, I created the inverse. Here now, everything that was 1 is now 0. Everything that is 0 is now 1. And I can print this one out, too. I'm going to just go up through my history. I'm going to print out matrix 2. And here—let's go ahead and zoom in—is my image. And you can see it's the inverse. Here now the center is white. It's 1. All the surrounding pixels are black, because they're 0.
So now you can see I've been interacting with MATLAB quite a bit. I've entered in a lot of commands. But while this is great at first, this can get kind of laborious. As your commands get more intense and you want to do more interaction, you probably want to move to something called a script. So let me show you one way of how to get that or how to transition to that.
If we go to the command history, you can see that everything that I've entered at the command window since I've opened up this session of MATLAB I have captured here. And you can essentially say, Okay, I want to capture this. I hold my Control key and start highlighting all the commands I want. Let's see, I created some matrices here, called imtool. And I can right click here. And I can evaluate that section. So I can rerun everything in history. Or I can right click and go to create script.
And so it opens up this editor window. And here you can see all of my commands I've entered in. And I can save this. For example, I can just save and create a test script. And then all the scripts are labeled .m for MATLAB. And here's my script. And I can essentially clear out my workspace. And I can rerun my script just by going to the Editor tab up here and hitting run. And essentially, here are all my variables. They've all been populated, created with my matrix, and also fired off imtool. And here's my little image where I can actually zoom in and see.
So it's a great way to move to an environment working with scripts now where you can iterate. You can debug with it. You can save and really make yourself a lot more productive. So command window is great. But when you want to become a lot more automated and save these things and iterate, I recommend moving towards a script.
So let's go ahead and move on to our image processing example. Let me go ahead and clear the environment. Now with our image processing example, if you look here on the left-hand side, I haven't shown this to you yet. But here's our folder browser, our file browser. So here's where we can see different files. We can navigate to any folder in the file system we want. And here are our two test images. Let's go ahead and now import those into the workspace.
So if we double click on test image one, we'll see that the import wizard came up. So here it's a JPEG image. So it's actually pretty straightforward. All it does is tell me the name. It's telling me that it's a 266-by-484 pixeled image. And it's three layers. The reason why it's three layers, it says there's an R, a G, and a B—a red, blue, and green layer. And we'll go ahead and import this into the workspace by clicking Finish. But I should point out that this import wizard will customize itself depending upon your data type. For example, if you have tabular data or another type of data format, you will see options that will help you to control how and what's being imported into the workspace. So just remember the import wizard will customize based on your data type.
Let's go ahead and click Finish here. And then you'll see we have our test image populated there, test image 1. Let's go ahead and import in the second image. And here it is. And we'll click Finish. So here we have the two images. Let's go ahead and preview them to make sure they got imported into the environment properly. If we click on one of them, go to the Plots tab, you'll see any relevant plot will be brought up to the surface here. We can do a simple imshow. And this, again, is like an equivalent of a printf. We're just looking at that variable.
See, nothing's interesting. Here's my office. It's relatively clean, as you can see, today. But you see there's nothing of interest. There's no intruders here. So everything looks good there. But if we go to test image 2—and then let's use the image tool here. I want to show you something interesting about that. Here is our picture of our office. But there's someone inside of it. So we have someone here.
So this is kind of interesting. Using the image tool, we can start to do some pre-processing or start analyzing the data. For example, we can click on this little ruler here. And I can measure how tall is this person? Oh, it's about 150 pixels tall. So this can be important later. Because we might want to do some length-based filtering, maybe making sure that we detect a person and not maybe someone's pet. So that's how you can do some quick measurements. Let me go ahead and delete that distance tool.
But you can also do things like crop the image. You can go back to this inspect pixel values. Here we see the pixel region. This time—let me go ahead and zoom in so you can see it a little better—now it's a little more interesting. As I move this little viewing window around to look at the region of pixels, here's the red, blue, and green intensity levels. So to make this red bag, you can see the values that were required to make that color. If you look in the black here, you can see it's all near zero, essentially. And then if you come near to the wall, it's all very high in intensity, up near 200, 256, which is the highest, which is essentially going to be pure white inside of this image. So really great information-- and this is important because you might want to do color-based processing later, so just another way to analyze your data and to see maybe what type of processing you might want to do in the future.
But in our case, this is a little more straightforward. So if I go back and grab my other image—let me go ahead and close this pixel region. And I'm going to grab my before. So in our case, what we're trying to do is we're trying to figure out when we have an intruder here. And if you look at these two images, you see there's a lot of consistencies. All of the office is pretty much constant except the person is the one thing that's new.
And so you have, essentially, a background—this is the background—and a foreground. So here, this person, we want to isolate. And we want to identify it as an intruder. And we're going to use a technique in image processing called image segmentation. So essentially, we're going to try to segment this person out from the image so we can do some analysis. And we want to ignore all of the things that are constant, because this is of non-interest.
So let's go ahead and step you through how we're going to segment and analyze this image. Let's go ahead and close some windows here. And usually the first thing on your pathway to segmenting an image is you're going to want to convert the images to grayscale. So the first question you might ask is, how do I even do that? Well, one way is you can go right up here to the search bar. And you can type in grayscale. So if I do that, you'll see some search suggestions. Or you can just hit Return here. And you'll see—here's the help coming up—you'll see you can sort things, refine it by product type. Or you can just scroll down and start examining some of the functions. And you'll see here, here's an RGB to gray. So this sounds like what I want. If I click on it, you'll see the description—convert RGB image or color map to grayscale.
So this sounds like what I want. Here's an example of the syntax you can see where it came from, the Image Processing Toolbox—import, export, and conversion. It was an image type conversion. And before I go back and use this function, I just wanted to show you at a higher level, if I click here, this is a great way to browse all the different categories of functions for the Image Processing Toolbox. And this goes along for actually all the other toolboxes as well. If you go to help, and if you look at all the different types of toolboxes that are out there, you can get some really great guidance as to how to use the different functions.
So if we go back to Image Processing Toolbox, you see how they're categorized. You see that there is image enhancement functions. You can click here. There's contrast adjustment. You can come back to import, export, conversion, if you want a type conversion. And here's the function we want, RGB to gray. So Help is a great resource. You can use the search bar, or you can just navigate and explore. And there's a lot of great examples in the documents, as well.
So let's go ahead and start using the function. So I'm going to create two new variables. I'm going to call it image gray, so imgray1. I'm going to use RGB. And I hit Tab to kind of help me—Okay, so it brings up the common functions that are prefixed with RGB. So I want RGB to gray. And I'm going to do this on test image and hit Tab. I want test image 1. And then I want to create another one, image gray 2, and set this equal to RGB gray, and set this to be test image 2.
So here are my two grayscale images. You see they are now populated in the workspace. I can go ahead and plot those out just to validate that they're there. There's the before. Let me click here. Here's the after. And so we're definitely on our way.
So here we are. We're essentially converted to grayscale. Now we want to get to the next step. But before I do that, again remember, if you're entering in the command window, it can get kind of laborious. I'm going to go ahead and open up a script for the sake of time that I've already written earlier. So here it is, image detection.
Here you'll see I'm reading in the images. So remember before I simply double clicked on the images in the file browser and used the import wizard to pull them in. Well, you can also use this imread function in MATLAB. So if I click F1, this brings up our help. And you can see it's a function that reads an image or a graphics file. And here's the syntax. And if you scroll down, here are the different file types it works with. So this is a great way of just importing in our image at a command line or inside of a script.
So I'm going to go ahead and clear out our environment just to rerun this script from the beginning. And you can see here I have this area of code highlighted in yellow. So what this is, is actually a code section. And if I click around, you can see this yellow area moving around. So this is an interesting aspect of MATLAB, as well. You can create these code sections with these double percent signs. So if I go to a single percent sign, that's simply a comment. But if I go to double percent sign, I create a section of code.
And this is essentially a complex break point, if you will. So everything inside of this yellow area is a single execution point. So if I run this section, it will run all the code inside of there. I can actually move out of order. So I can come back and run a previous section. So it's a really great way of iterating and converging on a solution very quickly. And you don't have to run the entire file or the entire application and run everything sequentially or chronologically like you do in a lot of other languages, so a really great way to work with the environment.
So let's go ahead and step through this script. And so here we have the first section. I'm going to go to my editor tab here and click Run in Advance. Let's go ahead and run that. Here we populated our workspace with the two color images. I'm now going to do an imshow on both of them. Here it is. Here's my after image. There's the before. Let's go ahead and convert them to gray.
So here we have two more images in the workspace—print those out with imshow. Here they are. And here is where we're going to start to segment the image. So coming back to our two images here—here's our grayscales. So here's my before. Here's my after. Remember, there's a lot of constants here, right? All of the background in both images are static. The only thing that's changing is the thing we want to isolate or segment. And so that brings us to another simple but powerful aspect of how you do segmentation. And that's background subtraction.
So we're going to take our reference image here and subtract it away from our second image. And hopefully we'll be left with just what's different right here. So if we go back to MATLAB, you'll see here in this section, subtract images, that there's multiple ways of doing this. The one way I did it here, which is the one that's going to run, I have the two images, my before and after. And I'm going to subtract one from the other. And I'm going to do an absolute value to make sure I don't have any negative numbers. So this is just using basic math. And remember, MATLAB treats everything as matrices. This is just simply a math operation. There is no for loops. There's nothing really tricky. Just by doing a simple subtraction, I'm able to remove the background.
You could have also used imsubtract. So image subtract is an image processing function as well. So this is just showing you that there's multiple ways to do the same thing in MATLAB. And it's a pretty flexible environment.
So let's go ahead and subtract the images. So let's go ahead and run this section in advance. Now what you're left with—so this is very interesting. All of the background has turned black. And the thing that I want segmented, you can see he's starting to appear. He's a little faded. But you can see it's done a pretty good job. I actually have the intruder somewhat highlighted. But we're going to have to do a little more processing so we can really make this more defined and a little easier to deal with. But we're definitely on the right track.
So here in this section of code, we're going to find the maximum location of difference. So all this is really doing is finding the highest intensity. It's going to go through every single row and column and look for the largest pixel intensity. So if we want to look at what that is here in the next section of code, here we're using imtool to look at image diff here. And let's go ahead and run this section. And I can just hit Control-Enter to run this section of code. Here we have our image. Let's go ahead and zoom in a little bit.
And if we click on our inspect pixels button right here, here are the intensities. I can go ahead and drag this around. And we can kind of look around here. And we can see, okay, in the black regions, of course, it's near zero. On the person itself, you can see there's around anywhere between 40 all the way up to 150. So you could see kind of where the intensities are. They're kind of low, some of them. But some are as high as 150, the highest being around 256, which we're pretty far from.
So what we're going to do here is run this section of code. And so you saw me running out of order here. Let's go ahead and find the max difference location. And if we run this, you'll see here, here's my image. And we drew a nice little star where we have the highest intensity. And I believe we saw that using the imtool. We saw that to be about 155. So this, essentially, is marking like the centroid or the center of mass for this object here. So let's go ahead—we used imtool already. Let's go ahead and move to this threshold area.
So thresholding is another simple yet powerful image processing technique. Essentially, what I'm going to do is convert this grayscale image into a binary image where it's only 1's and 0's. And the way I do that is I'm going to create this new variable image thresh. And I'm going to take the image difference l and I'm going to say everything that has an intensity value greater than 8, I want to change that to a 1. Everything below 8, I'm going to set at 0. So again, this is the threshold, if you will, of where we're going to cut it. And if you remember some of the intensities, most of them were around 20 or 40 and up. And then everything that was background was near black. So we should have good results with this. Let's go ahead and run this section. Again, I'm just going to hit Control-Enter.
And you can see, hmm, a lot more obvious, now. So we have our object, our intruder, here, pretty well highlighted. Here's our center of mass to make sure we have this right object identified. However, we do have some random noise leaking through. And so this could have been from camera movement or something that the background when it was subtracted left some residue behind. So we're going to try to clean this up. Because once we go to our next step, we want this image pretty clean. Because to do region analysis or object analysis, all of these little dots, essentially, are little objects. And this can really throw off some of our computations.
So let's go ahead and work on the image a little more. What we're going to use here in this fill regions section, we're going to use a function called bwareaopen. So this function is actually a morphological operator. Morphology is really just a fancy word for shape-based filters. So this is an image processing technique. If I hit F1, this is where we can filter images based on shapes.
In this case it's a little more flexible. We're going to input our black and white image here. But what the letter p or that input value is, is what is the size of the object we're going to screen for? What's the area? And so you're going to put in an area in pixels that you want it to screen for. In our case, we want to say—here's our binary image. We're going to pass that in as a variable. And we're going to say everything that is an area of 15 pixels or larger we're going to keep. Everything below that, we're going to drop out and essentially turn to 0. So this should hopefully clean up the image quite a bit.
And so let's go ahead and run this section—Control-Enter. And you'll see it worked pretty well. All that random noise disappeared. We have the object, the main one here. It looks like his hand, and this might be part of his shadow. But otherwise, it's still really pretty good. And we can definitely work with this. So let's go ahead and come back to here.
And in this section, we're going to do what's called an image overlay. And what's interesting about this function is this function is not from MATLAB. It's not part of Image Processing Toolbox. It's actually a utility that we downloaded from the File Exchange. So this is from our user community. Let's go ahead and I'll show you where I got this.
And here we are in MATLAB central. So this is our user community for all of MATLAB. And here you can find a lot of great resources, such as code examples. People ask questions, a lot of discussion topics. But also you can get some really nice utilities as well. Here's the image overlay. If I click on this, you'll see a description. This simply overlays one image over the other. And you get a really nice visual. You get to highlight anything of interest. And in our case, we want to highlight our intruder. So this is a perfect function for our use case.
So let's go ahead and get back to MATLAB. So here is our image overlay function. You can actually see it. We downloaded it. It's inside of the current folder as well. And we're going to pass the two images. So let's go ahead and run this section of code—Control-Enter. And here we have our image here. And you can see we've highlighted in red our intruder. So this is a great visual way now to show the user or whoever we're sharing our code with of what has really changed and what we've identified inside of the image. And here's our intruder.
So let's go ahead and come back here. And we're going to do another step of processing. Remember, I was measuring that person. And I was figuring out how tall they are. Well again, maybe we want to screen out and not have any false triggers, say, when a cat goes into the field of view. That is not really something we care about. But when a person comes in, we want to definitely sound an alarm.
So here we're saying we only care about things that are larger than 80 pixels in length or more. So here we're going to use a function to help extract that value. We're going to use a function called regionprops. And if I hit F1, you'll see this is from our Image Processing Toolbox. And this is a very powerful function. Because essentially what it's able to do is extract statistics from the objects in the image. So by passing images, you can get things like areas. You can find things like the centroids, diameters, pixel values, the intensities, the eccentricities, how round something is, the perimeters. So a lot of powerful statistics can be extracted. And even in our case, we want to look for axes lengths, so how tall this person is.
So if we call this function, we're going to store some new variables here. Here we're going to go through and look for all of the major axes lengths. We're going to compare them. Anything that's greater than 80, we're going to store them. And then if it is greater than 80, we're going to do this check in this last section here to see if we have anything of interest and if there's truly something different in the image.
So let's go ahead and run this section of code. And you'll see here in the workspace, we've populated some new variables. So here we have our image stats and image stats final. If we open up these arrays here, you'll see that I have a bunch of major axes, so a bunch of lengths have been found. However, the only one that's of interest to us is this one that's over 80: 107. And just to validate that the filtering worked, here we can see our major axes above 80. We found it. So here's the 107.
So if this is working properly, if we run our last section of code right here, this is saying if this matrix is empty we're going to say nothing is different. In our case, there is something there. So we should be able to print out that something is in the image. So let's go and run this last section. And if I go to the command window, you can see the output here printed out the major length was above 80. It was 107. And something was in the image.
So we see our algorithm's working pretty well now. And this is at a great stage where we can start to share it and get feedback. And so there's many ways of sharing your code. You can definitely ship the MATLAB files around. And if your co-workers have MATLAB they can run it and test it. But another great way of sharing is if you click on your code here—here we're in the Editor window. You can see we have a new Editor tab. This is the same one where I was controlling all of the stepping through the different sections. But what's interesting next to that, we have the Publish tab as well.
So here you'll see you can actually do things like highlight and make some more emphasis on the code. Because what you actually put inside of the MATLAB file here, the .m file, is going to show up in your end report. So you can make a lot of additional comments to really provide some more description and context for when this actually is being generated.
So here, if we go to publish—let's go ahead and publish image detection. And essentially, when you do this, it's going to run the entire algorithm. So let's go ahead and run it. And now what you have is a really nice HTML file. And this document now can be shared with others. And if you remember before, we created those sections of code with the double percent signs—each one of the comments there now becomes a navigation point inside of this table of contents. So here's that section called read in images. You'll see it represented here. Here's the code that was in that section. So here's the display color images. And you can see the output from that section of code, and so on.
So this is a great way to document your work. You can show your co-workers very easily how you logically came to your solution or converged on your results. And this just presents in a really nice form, not only just for sharing, but also for your own notes to help you document your work, so really a great way of sharing.
So now let's move to working with live video. So although this has been very interesting—we've worked with some static images. We really developed, we think, what is the crux of the algorithm right here. Now we want to scale up some more to move towards our intruder detection system. So the big piece of that is working with live video. And so we're going to work with some low-cost hardware like a simple webcam. And to do that, we're going to go ahead and just validate that we have the right hardware connections.
If we go to the Home tab right here and click on add-ons, you can see that there's a selection here—get hardware support packages. And if we click here, you see that it brings up this nice wizard. Here, you can see we have selections. I'm going to install from the internet. Click Next. And here are the different selections.
So here are all the different types of hardware targets you can access directly from MATLAB, everything from ARM processors, Raspberry Pi, BeagleBoard, a lot of different types of inputs such as Lego Mindstorm for nice robotics. And then even scrolling down, you'll see here's the USB webcams. And if I highlight that, you'll see I already have it installed. So it's just asking me if I want to reinstall it. And it's just telling me the versions. So this is how you would go on and just select the different support package to support the type of hardware you want to interface with.
So let's go ahead and start working with video. So I'm going to open up another script that I had written earlier. So let me go ahead and close this one. And we'll see here in the left-hand side I have this one called segmentation underscore fn. So all this is, is my same image detection algorithm I wrote earlier. The only difference is I commented out all of the imshows. Because I don't need to debug it anymore at this point. I pretty much know all the steps are working pretty well. I used imshow a lot to kind of tweak and adjust my images. So the algorithm's working quite well right now.
What I did do, if you look at the top, is use a keyword called function. And so what this does is it essentially makes this whole file just a lot more usable. So what it does is allows me to call this algorithm as a function. The same way I called a function like RGB to gray, I can now call this entire routine as segment underscore function. And what's nice is not only can I call it, I can actually pass in input arguments the same way you passed in an image to convert RGB to gray. You can pass in, in this case, a before, a reference image of my office, and then the after image, the one that we're looking for our intruder.
And so it's going to run the algorithm. And what's nice is you're going to even have, or you could have, a return value. So here we're going to pass back the highlighted image, the one where I used the image overlay function from the file exchange. And if I scroll down here, you can see where I made that assignment. So after I perform my image overlay, I passed it back to the variable here, highlighted image. And then the function, or wherever this function was called, will return this highlighted image.
So again, functions are very powerful and are a great way to make your algorithms a lot more accessible and easier to use. And where I'm calling this function from is another function called security_cam. So security underscore cam is my next function here. And all this is doing is setting up the webcam that I downloaded from the web as a support package. I'm setting that up. Here, I'm taking my reference image. So I'm taking a picture of my office before anyone has entered it. So that's my reference or my background image. Here, I'm going to enter into a for loop. And as I go through every iteration of the loop, I'm going to take a new snapshot to look for an intruder. And then I'm going to call my segmentation function. I'm going to pass my reference image as well as my newly taken picture, and I'm going to hopefully come back with nothing. But if it does detect an object, it will return it here and will print that out to the display.
So that's really all it is. I'm referencing my algorithm. I made it into a function. I'm running in this nice for loop here. And I actually made this security function right here, as well. So I can go to my command line right here and enter this in. So if I go ahead and type in security_cam and hit Return, here I have my live video running.
You can see my hand is being segmented well. It's highlighted in red. You can actually see on my command window that something is being detected. But if I move my hand away, you can see that it's gone back to nothing is in the picture. So it's really validating. I've increased my data sets considerably. And now I'm working with live video.
And the final stage I'm going to do is I'm going to put a nice user interface on top of my algorithm. So I've created it. I've validated it with real video. And now I'm going to create a nice little user interface. And to do that, I actually use something called guide. So guide, if you go into our search right here, you'll see what I'm talking about. So inside Help, you'll see open a GUI Layout Editor. So graphical user interface is what GUI is short for. And you'll see here there's different ways of building these user interfaces.
So there's ways of building dialog boxes. You can make some very complex UIs. And so there's a really nice way of building nice interfaces to allow you to interface with the algorithms and tune it. And it also allows you to easily share this. And when you build these GUIs, essentially, you're creating an app. And so when you have these apps, there's all sorts of ways to install them. You can provide them as installations for other people who have MATLAB, so a really nice way of making your code a lot more shareable and making your code into a full application.
Even all of those little tools, if I go back to MATLAB here—all right, so let's go ahead and show you what was created here. Here is a figure window. So using guide, if I open this up, you'll see that we actually used a lot of nice utilities to create these nice dialog boxes, to create these graphs, which you'll see populated in a minute. You saw that, actually, in the beginning. This was all running. We created these nice slider bars, a start and stop button, even the button to take the reference image, so really nice ways of just laying out your interface.
And here's—I'm not going to bog you down with the code, but essentially, this is the functions that are hooked into that user interface. So all of the code is right here. And in the end result, if I run this, you'll see what can happen is we get this really nice interface. I can go ahead and take my reference image here. You'll see that's my picture of my office. Here's my Start button. If I go ahead and hit Start you can see, okay, it's working pretty well. But here I have tuning knob. So I can essentially move this around and get it to the point where it's segmented properly. Here it's not actually picking the right length of objects. So let's go ahead and tune this till we get an alert. And now we've essentially tuned it. And we have a really nice working system.
So again, here we have a nice user interface. We have our algorithm running underneath. And we have nice interaction with it. And we could have also shared with our users a nice published HTML file as well to help document what you've done. So let's go ahead and get back to our presentation.
To summarize our demonstration here we come back to our typical technical computing workflow. And you saw how we access data. First we dealt with standard JPEG images. We imported them in with the import wizard. We did some development. We then later even integrated some hardware using a low-cost webcam. We did our algorithm development. And we shared it using our nice publish functions. So we were able to move all throughout the different phases. And you saw how we used script to help automate things. And in doing so we used MATLAB and some of the functions from the Image Processing Toolbox to make our intruder detection system.
And so to learn more about MATLAB, please visit our website at www.mathworks.com/matlab. There you'll find a lot of helpful videos, code examples to get you started and inspired in creating your own applications. If you need any help or assistance, of course we have support. But also MATLAB Central is a great resource where you'll find a lot of experts, both from MathWorks as well as in our community that are helping to answer questions, bring up great topics, as well as share code just to really inspire and to get you started on your journey.
And to learn more about Image Processing Toolbox, please visit our website at www.mathworks.co m/products/image. And there you'll find a lot of other videos and examples, all related to image processing. And you'll see my picture there on the right-hand side as well. So if you have any questions, maybe you're new to the field and just want to discuss some of the capabilities, feel free to email me. And I'll do my best to answer you.
So please stick around. We've come to the end of this session. Hopefully you've learned something, got a good introduction to MATLAB, saw some of the capabilities as well as the possibilities. And please stick around. We've got the Q&A session coming up. So please enter in your questions. We'll take a short break. And then we'll come back and answer them for you. So thanks again, and see you in a minute.
Download Code and Files
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本Japanese (日本語)
- 한국Korean (한국어)