Segmentation and Superimposition
Image Segmentation and Superimposition. In this section of the course, you’ll use the JPEG files,
monastery, which you can find in the course materials. On-screen, you can see the images, and as their names suggest, one is of a cat.
00:16 The other is of a cloister in a monastery. You can use Pillow to extract the cat from the first image and place it on the floor of the cloister. You’ll use a number of image processing techniques to achieve this.
00:29 You’ll start by working on the image of the cat. You’ll need to remove the cat from the background using image segmentation techniques. In this example, you’ll segment the image using thresholding. First, you can crop the image to a smaller one to remove some of the background.
01:15 The cropped image contains the cat and some of the background that’s too close to the cat for you to crop it with a rectangle. As you’ve already seen, each pixel in a color image is represented digitally by three numbers corresponding to the red, green, and blue values of that pixel.
Here you can see the grayscale image and the result from the thresholding process. All the points in the grayscale image that had a pixel value greater than
100 are converted to white, and all the other pixels are changed to black.
02:39 You can change the sensitivity of the thresholding by varying the threshold value. Thresholding can be used to segment images when the object to segment is distinct from the background, and you can achieve better results with versions of the original image that have higher contrast. In this particular case, you can achieve higher contrast by thresholding the blue channel of the original image rather than the grayscale one.
03:03 This is because the dominant colors in the background are brown and green colors, which have a weak blue component. You can extract the red, green, and blue channels from the color image as you did earlier on.
03:29 and blue channels. They’re seen on-screen together with red, green, and blue from left to right displayed as grayscale images. The blue channel has a higher contrast between the pixels representing the cat and those of the background, so you use the blue channel image to threshold.
04:29 Different operating systems often come with different default JPEG decoders. Therefore, the results that you get when processing images may vary slightly depending on the operating system and JPEG decoder that you are using. As a result, you may need to slightly alter the threshold value if your results don’t match the ones you see on-screen.
05:06 You can certainly identify the cat in this black-and-white image, but you’d like to have an image where all the pixels that correspond to the cat are white and all other pixels are black. In this image, you still have some black areas on the cat, such as the eyes, nose, and mouth, and you also have some white areas in the background.
05:24 You can use two image processing techniques called erosion and dilation to create a better mask that represents the cat, and you’ll learn about these two techniques in the next section of the course.
Become a Member to join the conversation.