Many image processing applications require analyzing objects within an image, such as measuring the size of these objects. Isolating regions of interests within an image is called segmentation. The most common form of segmentation is binary, where pixels are labeled as belonging to either the foreground or background. However, for some applications, like this X-ray image of an arm, it is appropriate to have multiple labels for different objects in the same image. In this video, you'll practice segmenting grayscale images using a global threshold, an adaptive threshold, and a multilevel threshold. To begin, let's use a global threshold to segment the cracks from these images of concrete. One of the most common and useful methods for segmenting images is to threshold them. This means choosing an intensity value, then labeling pixels below this value as false and pixels above as true. Start by reading in one of the images, converting it to grayscale, and viewing it using imshow. The crack is much darker than the concrete, so segmenting it with a threshold should be possible. You could investigate the pixel values and manually choose a threshold to separate the regions. Alternatively, the function imbinarize can choose an optimal value for you. The result is a matrix where each pixel has a logical value of zero for false and one for true. Compare the result to the original image using montage. Side by side, it's clear to see that binarizing an image turns dark pixels black for false, and light pixels white for true. However, because we are interested in the crack and not the solid concrete, we should invert the black and white image using the logical NOT operator. This swaps the values, so the white foreground pixels represent the crack instead of the concrete. You can now use the BW variable to calculate various properties, such as the total number of foreground pixels. To do this, use the nnz function, which stands for number of nonzero elements. In this case, the crack covers nearly 8,000 pixels of the image. The binary image is known as a mask, because you can overlay it onto the original image to mask out the false or black regions. To do this, first create a copy of the image variable. Then, use logical indexing with your binary image to specify all pixels where the mask is not true and set them to zero intensity. The result is an image of all zeros except where the mask was true. You might be wondering how the threshold value was determined and what it was. The default behavior of imbinarize is to use Otsu's method, which finds a threshold value based on the distribution of pixel intensities. Find this value using the graythresh function. This returns a value between zero and one, but you can convert it to an intensity value by multiplying by 255. Otsu's method often works well, but not always. For instance, look at the result with this image of a thin crack. Here the number of pixels representing the cracks is small, while the concrete dominates the image and has both brighter and darker regions. Therefore, Otsu's method prefers to separate regions of the concrete. Notice the chosen threshold is significantly higher than the previous image's value of 0.32. In this case, specifying your own threshold value may be a better approach. Do this using the second input of imbinarize. Let's try 0.5, this looks a bit better, but can we improve it even more? To quickly test different values, it's helpful to add a numeric slider control. After you insert it, make sure the minimum and maximum values are set correctly and the step size is appropriate. Now, a new threshold will appear whenever you adjust the slider. Feel free to experiment with different binarization thresholds for other images too. In some applications, a globally applied threshold doesn't capture all of the objects you'd like. To demonstrate, let's binarize this image of rice grains. We don't need to invert the mask as we did for the concrete images, because now, foreground objects are the high-intensity grains. At first this result looks fine, but look at the bottom of the mask. Some of the rice grains are missing. Why is this? This image has uneven illumination. This causes some of the grains at the bottom to have lower intensities than the background at the top. Therefore, a single threshold value will not work for the entire image, instead the value needs to vary depending on the local average intensity. This is called adaptive thresholding. You perform adaptive thresholding by specifying the method as a second input to the imbinarize function. This result looks much better. All the rice grains are now captured by the mask. Some extra background pixels are marked as foreground, but you'll learn techniques to address this later in the specialization. Segmentation is not always binary. Some applications require multiple labels, such as this X-ray image of an arm. There are four clearly defined regions, background, tissue, bone, and metal. Use the multithresh function to find multiple thresholds. Otsu's method is still used to determine threshold values, but now you must specify the number of thresholds as a second input, which in this case is three, because we need three thresholds to segment four regions. The output of multithresh is a vector containing the threshold values. To create a multi-label segmented image pass the image and the vector of threshold values to the imquantize function. This returns a label matrix containing an integer number representing the region each pixel belongs to. This variable is of type double, so you can't view it in its current form because the values aren't scaled correctly. The imshow function allows for a second input to specify how to scale the image values for viewing. To use the full range of values, enter a set of empty brackets. Now you can clearly see the four regions found by the multi-level thresholding. From this label matrix, you can still isolate individual regions. To do this, create a variable for the new mask and have it store the logical array corresponding to one of the labels. In this case, we'll isolate the third label, which is the bone. Look at that! In conclusion, you now have multiple tools that you can use to segment regions of interest from a grayscale image. Take some time to practice these techniques with other images and share your results in the forums.