Image processing is all about enhancing and extracting
information from an image, to analyze and understand
our world better.
In this video, we'll walk through a typical image
processing workflow.
We'll use MATLAB and Image Processing
Toolbox to analyze deforestation in the Amazon rainforest.
The Amazon rainforest is the largest and most diverse
rainforest in the world.
These are satellite images of the Jamanxim National Forests
in Brazil, which has one of the highest rates of deforestation
in the Amazon.
But there have been significant efforts in recent years
to slow the deforestation.
Let's analyze images taken every four years of the same region
to see the impact of those conservation efforts.
First, we'll have to identify the deforested regions
in each image, and then calculate their area in pixels.
We can then use this scale to convert the area from pixels
to square kilometers.
Let's head over to MATLAB to get started.
We have five images to work with,
representing various years.
Let's import the one from 2000.
When you bring the image into the workspace,
the name of the variable will match the name of the file
by default. We'll use a live script to document, type,
and run our code.
To display an image in MATLAB, you
can use the function imshow.
Let's also add a simple title to our image.
This image as a height of 800 pixels and a width
of 720 pixels.
In MATLAB, this means we have an array
with 800 rows and 720 columns.
Color images are 3D arrays because we
need to store the red, green, and blue values for each pixel.
These values are integers that range from 0 to 255.
Let's compare 2000 to 2016.
You can use the function imread to directly important image
into the workspace.
To display these images together,
use imshowpair with the montage option.
We can already see that the deforested area
is larger in 2016.
But to compare the two more accurately,
we'll need to isolate the deforested regions.
In image processing, this is called segmentation.
The deforested regions appear to be brighter.
So we can use this brightness or intensity for segmentation.
It's easier to visualize intensities
in a grayscale image.
You can use im2gray to convert an image to grayscale.
Grayscale images are 2D arrays because we only
need one value, the intensity for each pixel.
One way to examine these intensities
is to create an intensity histogram.
The x-axis represents the intensity values.
And the y-axis is the pixel count.
Most of the intensities are clustered here,
making it hard to isolate the bright, deforested regions
from the darker surrounding areas.
We'll want to adjust this histogram.
So we use more pixels that are darker and brighter, which
should increase the contrast of the image.
The function imadjust spreads the pixels out
to match the intensity range better, while still maintaining
the original shape.
Comparing the original and adjusted images,
the deforested regions are brighter and more prominent.
Now, we need to decide which pixels
belong to the deforested areas.
One way is to threshold the intensity values.
If the intensity of the pixel is higher than the threshold,
then it's deforested.
If it's lower, it's not.
Picking a good threshold is tricky.
To find the right one, you can use the Image Segmenter app.
You can find it in the Apps tab and the Image Processing
and Computer Vision section.
We'll start by loading our image.
We'll use the adjusted grayscale image from 2016.
We already adjusted the contrast,
so we don't have to do it again.
Let's start with a Threshold tool.
The blue areas indicate what will
be segmented based on your current threshold settings.
You can choose your method here.
The default tries to automatically pick
an optimal threshold.
But you can also manually choose a threshold with this option
A low threshold isolates the deforested areas well, but also
falsely segments some areas that aren't deforested.
A high threshold removes those false segmentations,
but then you lose some areas that are deforested.
It's all about balance.
In this case, a threshold around 100 does a good job.
To view the result, Click Show Binary.
It looks pretty good.
But we can see a few small problems.
For example, this squiggly line is not a deforested region,
but a river that shouldn't be segmented.
We can refine our segmentation using these options.
Here, let's try Morphology, which
will allow us to modify the shape of the segmented areas.
Choose the type of operation here.
Since we want to remove the river,
let's try the Open Mask operation, which removes areas
smaller than a given shape.
For the shape, choose one that resembles
the areas you want to keep.
In our case, the areas are square.
Next, set a size for the shape.
If it's too small, then you might not remove the river,
but if it's too big, you'll remove deforested areas.
In this case, a length of 3 removes the river
and leaves the rest mostly intact.
OK, we've segmented one image.
So let's try another one.
How about 2004?
We'll go back to our live script and import it,
convert it to grayscale, and adjust the contrast.
Then back in the app, let's import the adjusted image.
It looks like the same threshold also segments
most of the deforested areas.
However, this image contains some clouds
from the atmosphere that block our view.
Those clouds are falsely segmented,
due to being very bright.
Also, the text in the corners is being segmented.
This was the case in the 2016 image too.
It's not a big deal, but it'd be nice if we could remove it.
So it looks like we're not going to be able to use intensity
to segment this image.
How about color?
The deforested areas have a distinct brown color
that's different from the white and dark green colors
in the rest of the image.
We can segment based on color using the Color Thresholder
app.
Let's use the image from 2016, because it
has more of that brown color that we're looking for.
Next, choose the color space to segment in.
Different color spaces work better for different images.
So how do we choose?
Well, these point clouds can help.
They visualize the colors in a 3D space.
You'll want to pick the one that isolates the brown color
the most.
These three are all equally good.
So let's just pick one and start.
How about HSV?
Hue determines the color.
Saturation sets the shade.
And value describes the brightness.
Let's start with hue.
We can remove most of the green color with this alone.
As you move the slider, the preview and point clouds
update in real time.
This still leaves parts of the year text visible.
Let's zoom in and get a closer look.
The remaining text is white, which
is hard to remove using hue.
Instead, we can use the fact that it's very bright
and use the Value Slider.
So filter out the brightest pixels,
and it removes the remaining text.
OK, let's go back and look at the binary image.
Well, that looks like a pretty good segmentation.
Now we're ready to export the results.
You can export the image itself, but since we're
going to do this for multiple images,
let's generate a function instead.
The generated function takes in the RGB image as the input.
And it has two outputs--
the mast image, which is the preview from the app,
and BW, which is the binary image.
Let's save our function so that we can use it in our script.
We'll save it as createMask in our current folder.
All right, let's try it out on the image from 2004.
Last time, the clouds got in our way.
This time, it looks like our function
was able to remove the clouds while preserving
the deforested areas.
Now that we have our segmentation,
it's time to calculate the area of the deforested regions.
The regionprops function calculates
a lot of useful information about the segmented regions
in an image.
We're only interested in the area of each region.
So let's use the Area option.
Then, to get the total area, we'll add them all up.
Now, this area is in pixels.
So we need to convert it to square kilometers.
Remember that scale in the images?
We can use that.
To measure the scale, we can use the Image Viewer app.
All right, let's Zoom in.
The app has a ruler tool that we can
use to measure the length of this line.
It looks like 20 kilometers is about 51 pixels.
Back in our script, we can calculate the conversion
factor.
Then use that to find the total deforested area
in square kilometers.
Now, we'll need to do this for all the images, which
we can do using the Image Batch Processor app.
For this app, you'll have to choose the folder that
contains your images.
Once they're loaded, you'll see a preview of all the images
to be processed.
Next, select the function to process each image.
If you click New, the app generates a template
to help you write your own function I've already
written a processing function called Calculate Area.
Let's take a look at it.
To work with the app, the function
must have an image as the input, and any outputs
formatted as a structure.
Here, we'll output both the binary image and the area
in square kilometers.
Back in the app, we can browse for that function
and process all the images.
The results appear over here.
For each image, we can preview the segmentation and the area.
Let's export to the workspace so we
can analyze the results further.
We'll include everything in a table named allresults.
We can see the deforested area increases over the years,
but not at a constant rate.
Let's create a bar chart of the areas so we can see the trend.
There was a considerable jump between 2004 and 2008.
After that, there's a significant reduction
in the deforestation rate, showing the impact
of conservation efforts.
We can see this in the segment and images too.
Use the montage function to view a collection of images.
The segmented areas expand in 2008,
then remain fairly constant in the next two images.
It's pretty cool to see how conservation efforts really
can make a difference.
These files are available to download,
so you can try it out for yourself.
We've shown you some of the capabilities of Image
Processing Toolbox, but there's so much more to explore.
Our images focused on one small part of the Amazon.
If you had more images of the surrounding regions,
you could use image registration to stitch the images together.
Or if you had multispectral images,
you could process them to identify
different types of vegetation.
The sky is the limit.
Check out the Image Processing Onramp
or the Image Processing with MATLAB course to learn more.
Welcome to Image Processing with MATLAB.