Posted on

Posted on

Posted on

Posted on

Learn MATLAB Episode #21: Gaussian Filter Blur and Edge Detection

So now let’s take our Gaussian and convolve it with the image. So now remember that A is 512 x 512 x 3, which is a three-dimensional matrix, and H is a two-dimensional matrix. So if I try to do this I’m going to get an error I’m sorry we are using the CONV2 function, and you still get an error because A is not supposed to be a three-dimensional matrix, right. So if we look at the definition of convolution we’re working with two dimensional matrices, if we have two dimensional convolution. So what we want to do is let’s just take the red channel and it doesn’t matter which one, because remember when we were looking at the red, green, and blue channels we pretty much saw the same image for every channel. Ok, so, we still get another warning and that’s that A is still in values of UINT8, so let’s change that to double. Ok, so, now let’s imshow(C) and see what we get. So it’s all white, and so what does white mean? That means all of the values are too high, right, so there’s too much intensity in this image. So, you have to play around with the values a little bit, so let’s try making a smaller filter. Ok, so, let’s say H is equal to my_gaussian 25 and then sigma is 5. So now the filter’s smaller and the sigma is smaller, so the blur is going to be less, but the intensity is still too high. So that could mean just the values are too high and we need to multiply by lower values. So let’s divide the filter values H x 1000, and imshow(C) again. So now we’re starting to see some black, right, so that’s good. Ok, so, I’m decreasing values and I’m starting to see the image. Okay, so, here’s the original image but blurred using a Gaussian of sigma equals 5. Now one thing I didn’t show you guys because I wanted you to go through the exercise of creating a two dimensional Gaussian filter yourself, is that we already have a function called fspecial in matlab that create filters for us, and so fspecial can create many different types of filters. So, in addition to the Gaussian it can create laplacian filters, an averaging filter which is another thing we’ve used, the Sobell filter which is useful for finding edges, so all different types of filters. So let’s try using fspecial instead. fspecial(‘gaussian’, 25, 5); Now let’s do our convolution. So I’m going to divide by a thousand, because I know I’m going to have to divide. So let’s just see what we see. Ok, and so it’s a little dark so I didn’t have to divide by a thousand, maybe I could had divided a lower number, but you can see the idea is that we’ve blurred the original image using the Gaussian filter given to us by fspecial instead of our own Gaussian filter that we built. Ok, so, like we did before I want to plot the filter that we created. So I’m going to go imshow little h, which is the filter we created with fspecial, and notice how it’s just all black. Remember that 0 is black and 1 is white. So what we could do if we want to look at what’s in h, let’s check the maximum value of h. So max, max because it’s two-dimensional. The maximum value is .0065, so of course when we plot that it’s going to be pretty close to black. So what we could do if we want to scale it by one, if we want the maximum value to be one, we could do imshow(h) divided by the maximum value. Ok, so, now we see what we expect to see which is white in the middle. So one question you might have is why is the value of h so small when we use fspecial? That’s because it does a thing called normalization. So if you have ever studied probability you know that a probability distribution has to sum to 1, so here it is a similar thing. So if we sum across both dimensions of h we get 1, and so that’s why the values of h are so small. And so now just for completion sake, I want to do sort of what’s the opposite of blurring, I want to do edge detection of the image. So I want to find all the edges and set those values to 1. So matlab has a method called edge that will do this, and it’s very simple to use. You pass in again the gray-scale image, right, so only a two-dimensional matrix that won’t work if you pass in the entire image matrix. Ok, so, edge just does all of that automatically for you. So if imshow(E) I can more or less see where all the sharpest edges are, and of course there are parameters you can pass into edge to make it more or less sensitive, but this is by default what it does.

Posted on

FREE MATLAB Course on Teachable

MATLAB is much easier to learn when you can try everything for yourself in this course for beginners! With more than a million users, MATLAB is a must know programming language for science, engineering, and economics professionals.

Learn about solving equations in MATLAB, data structures, probability, and how to plot data in MATLAB from a software engineer with proven experience using MATLAB. If you want a screencapture course that shows you exactly how to use MATLAB, you’re ready to take this course!

https://goo.gl/qNqhlD

Posted on

Learn MATLAB Episode #20: Gaussian Image Noise Reduction

Now we’re going to move on to the next step in order to implement our blurring tool, or our blurring filter. So the first thing we need to do is we need to extend this idea of convolution to two dimensions. So, the first modification we did so we looked at convolution at it’s most basic definition where it’s of a continuous variable filtered by another continuous variable, so we discretize it by turning it into a sum, and so we already have discrete signals because we’re working in matlab. Now, when we do two-dimensional convolution notice we now have two dummy variables. So before tau or k, now we have two dummy variables tau 1 and tau 2, or n1 and n2,. And so it’s basically what you would expect when you have one deconvolution you’re going from minus infinity to infinity along the one dimension that’s the independent variable, so you can think of that as time, and in two dimensions we go from minus infinity to infinity for both independent variables so that the x and y, or in other words the two spatial dimensions of the image. So now that we’ve extended our idea of convolution to two dimensions let’s think about how we could implement a blur using a Gaussian. So a Gaussian is basically a spread, right, there’s a middle point and then it spreads out over a radius in a circular fashion. So, my question to you is how can we build a two-dimensional Gaussian image that we can use as a filter on the original image, so that we can do a convolution between those two? So, I’m going to give you a minute to think about that, please pause this video and come back when you have figured it out. Ok, so, I have in fact actually been showing you the solution to this for multiple videos now. So I’ve created a function called my Gaussian, and it takes in two parameters n and Sigma. So n is going to be the size of the square, so the output is going to be an n x n matrix, and Sigma is going to represent the standard deviation of the Gaussian as is convention when we’re talking about Gaussians. So we have the output called H, now I only need the value of two Sigma squared ever in this equation, so I’m just going to calculate 2 sigma squared at the beginning so I don’t have to do that on every iteration of the loop. I’m going to initialize h to be an n x n matrix of all zeros, and then I’m going to use a for loop to assign every value of age. So, I going from 1 to n, and j going from one to n. Now I assign x to equal i minus n/2, and y to equal j minus n/2. Why is that? Because I want the center point of H, so that would be H of n over 2 over 2, to to be the highest point of the Gaussian, and so that’s when x is equal to 0 and y is equal to 0, and the exponent of 0 is 1. So that would be the maximum value of the Gaussian and then every point from there would be smaller, right, so when I is n/2 x=0, when j is n/2 y=0. And the formula for a Gaussian is x squared plus y squared over 2 sigma squared, and then you take the exponent of the negative of that. Ok, so, what does this actually give us? So let’s use the my Gaussian function, say n is a hundred and Sigma is 10. Now let’s imshow the H that I got. Ok, so you can see so remember that white is the maximum value 1, and black is the minimum value zero, and so this is a Gaussian what a Gaussian looks like when you plot it on an image. So what would I do if I wanted to see more white? I could increase Sigma, right? So, let’s say Sigma is 25, imshow H, and so you see the radius of the white part has increased.

Posted on

Learn MATLAB Episode #19: Convolution

In this matlab video we’re going to talk about convolution. So I mentioned this before when we were talking about the low-pass filter, because they are very similar and related concepts. So, when we’re talking about the low-pass filter we did a very simple filter called the moving average, and so to give you a sense of what that’s doing again you’re taking a window, say five samples at a time, and then you’re sliding that along the signal and taking the average, and then that is the output of the filter. So this sliding motion and then applying some function to that window that’s sliding along is called convolution. So what you’re really doing when you’re applying a filter is you’re convoluting one function with another. So let’s look at the definition of convolution. OK, so, convolution is also known as the star operator, and it’s the integral of one function with this dummy variable. So you can see this sliding motion that I was talking about. Now this will not make a lot of sense to you if you haven’t studied calculus before, but there is one important result from convolution, or the study of convolution, that we should talk about and then you should know, and that is that convolution in the time domain, and we use time as a sort of dummy variable so time to mean actual time, or time can mean space, the important distinction is that you’re going from time in one domain to the frequency in the other domain, so the result that’s important is that convolution in the time domain, so if I convolve one signal with another, this is equivalent to multiplication in the frequency domain. So, what is the significance of this? So that means they’re two equivalent ways of computing the convolution of the signal with its filter. So one way is to just do the convolution using the formula for convolution which is here and of course in matlab it would be a sum not an integral since we have to discretize the signals, but the other way we could do is since convolution in time is equal to multiplication in frequency, we could take the Fourier transform of both signals first, multiply them, and then do the inverse fourier transform to go back to the time domain, and so that would be equivalent to doing convolution. One application of this is the Fourier transform, or the Laplace transform, can be used to solve differential equations. So you can solve them in the frequency domain and then convert that to the time domain to get the signal of interest back. So, now again when we think about convolution a very useful analogy is this sliding motion, and so that’s exactly what the moving average was doing. And so one physical manifestation of this is the blurring of an image, and so you’ll see in a later lecture that I’m going to do that the blurring of an image is actually convolution, but the sliding motion you will actually do you cando by hand and it has same effect. So this is to say if you’ve ever used photoshop and you use the blur tool on an image, you notice that you click on the blur tool and you get a point, and then you slide it across the image on the places where you want to blur, and then it blurs those points.

Posted on

Learn MATLAB Episode #18: Image Manipulation Exercises

So, in this matlab tutorial we’re going to do some basic exercises just to get you working with image matrices and they’re going to be very similar to the exercises we did for sound. So the first exercise I want to do is how do we flip and image upside down? I’m going to give you a minute to think about that, so please pause this video and then come back when you have figured it out. Ok, so, we want to flip an image. It’s going to be exactly the same as we did for sound. So, if you recall sound was stored in using different samples in different rows, and essentially that’s what’s happening with an image also. Alright, so, the top of the image is the top of the matrix and the bottom of the image is the bottom of the matrix. So if we do flipud, which stands for flip-up down, of the image that we loaded and then we imshow this image, we see the original image flipped upside down. So now here’s something we couldn’t do with sound because sound is one-dimensional, so you can only flip it one way. Now let’s suppose I want to flip the image in the horizontal direction. How would I do that? So I’m going to give you a minute to think about that. Please pause this video and then come back when you figure it out. Ok, so, if you remember when we were talking about sound we looked at two different kinds of flip. There was flipud which flipped vertically, and fliplr which flipped horizontally. So all we have to do is call the other flip function. I’m going to imshow this flipped image, and then so this is the image flipped horizontally. And of course you could flip the image both vertically and horizontally to get this. Ok, so, now the next exercise we’re going to do involves working with color. So, I said before that the third dimension in the matrix are the red, green, and blue channels of the color. So how can we show this? So let’s say I only want to view the blue channel. How can I visualize that? So, I’m going to give you a minute to think about this, please pause the video and then come back once you’ve figured it out. Ok, so, usually we say RGB because red is the first component, green as a second component, and blue is the third component. So I’m going to assign the original matrix a to a temporary variable B. I want to view blue so what I want to do is I’m going to set red and green to 0. So how would I do that? I say colon to select all of the rows, another colon to select all of the columns, and then I put a 1 because I want to set the first channel which is red to 0. I’m going to do the same for the green channel, and so now only the blue channel has values that are not zero. So if I imshow B, now we see only the blue components of the image. Alright, and so if I imshowed one of the other channels that was set to zero, it’s just pure black. So 0 is black, it means no intensity at all. So, next we’re going to look at the green channel. So I’m gonna set G = A; G colon, colon, so I’m setting again red to 0, but now I don’t want to set green to 0 I want to set blue to 0, so it’s G (:, :, 3) = 0; and imshow(G), so here’s the green channel. So it’s a little bit more intense, or it has a higher intensity than the blue channel, we can see visually. So now let’s say we want to view the red channel. So we’re going to do something very similar to be above. So I don’t want to set red to 0 iIwant to set green to 0 which is the second channel, and I want to set blue to 0 which is the third channel. So imshow(R) and this is the red channel. So one thing to notice is that no matter which channel we look at, we can still pretty much just view the image. Now why is that? It’s because every color contains a red component, a green component, and a blue component. So, when you see colors think of them as sort of a mixture of those three.

Posted on

Learn MATLAB Episode #17: Image Processing

Image processing in MATLAB