Python Lesson 48: Image Borders (AI pt. 14)

Advertisements

Hello everybody,

Michael here, and in today’s post, we’ll explore image border creation using OpenCV!

Let’s create some image borders!

Before we start creating image borders, here’s the image we’ll be using for this lesson:

This is an image of Fry and Bender pumpkins (two main characters from the animated sitcom Futurama if you weren’t familiar) that I created with a few Sharpie markers this past Halloween (creative I know).

Now, let’s read in our image to the IDE in RGB form:

import cv2

import matplotlib.pyplot as plt

pumpkins = cv2.imread(r'C:\Users\mof39\Downloads\20231031_231020.jpg', cv2.IMREAD_COLOR)
pumpkins = cv2.cvtColor(pumpkins, cv2.COLOR_BGR2RGB)

Now that we’ve done that, let’s add a simple yellow border around our image:

pumpkinsNormalized = pumpkins / 255.0


pumpkinsWithYellowBorder = cv2.copyMakeBorder(pumpkinsNormalized, 30, 30, 30, 30, cv2.BORDER_CONSTANT, value=[1, 1, 0])

plt.figure(figsize=(10, 10))
plt.imshow(pumpkinsWithYellowBorder)
plt.show()

To add a simple yellow rectangular border around our image, we’ll need to use the cv2.copyMakeBorder() method and include the following parameters:

  • The image where you will add a border
  • Four integers indicating the border’s thickness (in pixels) on the top, bottom, left, and right sides of the border, respectively
  • One of five possible OpenCV image border modes-in this case, I used the border mode cv2.BORDER_CONSTANT, which adds a simple colored rectangular border to the image.
  • A three-integer array indicating the border color in RGB notation (more on that shortly)

Now, you may see something unfamiliar above-pumpkinNormalized. This indicates that I have normalized the image. What does that mean?

In this case, normalizing an image means scaling all the pixels-and in turn, colors-to ensure that all of the pixels and colors in the image are using the same scale. This is important since I discovered that OpenCV has a bug (as of this writing) where when you try to add a border to an image, it will add a border to the greyscale version of the image (even if you read it into the IDE in RGB scale). Normalizing the image ensures that the border will be added to the RGB version of the image.

  • It likely goes without saying, but if you want the border on the RGB image, please remember to add the border to the normalized image, not the initial image you read into the IDE (even if you did convert it to RGB colorscale).

Now, as for the RGB color array, let’s dive into that. The array works the same way as other forms of RGB notation (e.g. RGB(210, 12, 12) represents the intensity of the red, green and blue colors) but with one key difference-the values used only range from 0 to 1 (including decimals between these two integers). The values still represent the intensity of the red, green and blue colors in the image, respectively, but the representation looks more like the percent of a certain color and less like an integer. In this example, since I wanted a simple yellow border on the image, I used the array [1, 1, 0] which is the same as saying RGB(255, 255, 0) because in both notations creating yellow requires full (or 100%) red and green but no blue.

Other border modes

Now, one thing to keep in mind with OpenCV’s image border modes is that, as of December 2023, there are no ways to make fun dotted/dashed/dotted-and-dashed borders yet (though of course, that could change).

However, aside from the simple cv2.BORDER_CONSTANT mode that creates a simple rectangular border around the image, there are four other OpenCV image border modes. Let’s explore one of them-cv2.BORDER_REFLECT, which adds a reflective border to the image. To change the border from a simple colored border to a reflective one, let’s change this one line of code:

pumpkinsWithReflectiveBorder = cv2.copyMakeBorder(pumpkinsNormalized, 40, 40, 40, 40, cv2.BORDER_REFLECT)

All I had to do to modify the code to get a reflective border was change this one line by changing the border thickness (30 to 40 pixels), removing the color (since this border mode doesn’t require a color), and changing the border mode to cv2.BORDER_REFLECT and, well, check out the image with a reflective border:

In this image, there are a few spots where the reflective border is hard to find, but it’s there (and quite prominent in the bottom side of the image where if you look close enough, you can see the reflections of the pumpkins.

Thank you,

Michael

Leave a ReplyCancel reply