Upsampling an Image Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:27:02

4K+ Views

In this program, we will up sample an image. Up sampling is increasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming in on a small region of an image. We will use the pyrup() function in the openCV library to complete this task.Original ImageAlgorithmStep 1: Read the image. Step 2: Pass the image as a parameter to the pyrup() function. Step 3: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') print("Size of image before pyrUp: ", image.shape) image = cv2.pyrUp(image) print("Size of image after pyrUp: ", image.shape) cv2.imshow('UpSample', image)OutputSize ... Read More

Perform White and Blackhat Operations on Images Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:26:42

382 Views

In this program, we will perform the Blackhat operation on an image using OpenCV. BlackHat transform is used to enhance dark objects of interest in a bright background. We will use the morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) function.Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image. Step 3: Define the kernel size. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 5: Display the output.Example Codeimport cv2 image = cv2.imread('image_test.jpg') filter_size = (5,5) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size) image = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) cv2.imshow('BlackHat', image)Output

Perform White Tophat Operation on Images Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:26:06

679 Views

In this program, we will perform the TopHat operation on images. TopHat operation is a morphological operation that is used to extract small elements and details from given images. TopHat is used to enhance bright objects in a dark background. We will use the morphologyEx(image, cv2.MORPH_TOPHAT, kernel) functionOriginal ImageAlgorithmStep 1: Import cv2. Step 2: Read the image. Step 3: Define the kernel size. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 5: Display the output.Example Codeimport cv2 image = cv2.imread('tophat.jpg') filter_size = (5, 5) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filter_size) image = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel) cv2.imshow('TopHat', image)OutputExplanationAs ... Read More

Newline in Matplotlib Label with TeX in Python

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:25:47

4K+ Views

The following program code shows how you can plot a newline in matplotlib label with Tex.StepsSetup X-axis and Y-axis labels for the diagram with to plot a newline in the labels.Set the current .rcParams for axes facecolor; the group is axed.Use a cycler to set the color for the group of lines. The color list consists of ‘r’ for red, ‘g’ for green, ‘b’ for blue, and ‘y’ for yellow.The cycler class helps to create a new Cycler object from a single positional argument, a pair of positional arguments, or the combination of keyword arguments.Plot the number of lines ... Read More

Dilating Images Using the OpenCV Function dilate

Prasad Naik
Updated on 17-Mar-2021 08:18:49

681 Views

In this program, we will dilate an image using the dilate function in the OpenCV library. Dilation adds pixels to the boundaries of objects in an image, i.e., it expands the image on all sides.Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image using opencv.imread(). Step 3: Define the kernel using np.ones() function. Step 4: Pass the image and kernel to the dilate() function. Step 5: Display the imageExample Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((3, 3), np.uint8) image = cv2.dilate(image, kernel) cv2.imshow('Dilated Image', image)OutputExplanationAs you can see, the image ... Read More

Perform Opening Operation on an Image Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:18:29

679 Views

In this program, we will perform the opening operation on image. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening can be called erosion followed by dilation. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = ... Read More

Draw Average Line in Histogram using Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:18:07

2K+ Views

We can plot some expressions using the hist method. After that, we will plot the average graph for the expression using the plot method and bins that are returned while creating the hist.StepsGet the data for x using some equations, set num_bins = 50.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Get n, bins, patches value using ax.hist() method.Plot average lines using bins and y data that is obtained from some equations.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.Using tight_layout(), we can adjust the ... Read More

Perform Closing Operation on an Image Using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:17:42

2K+ Views

In this program, we will perform the closing operation using the cv2.morphologyEx() function. Closing removes small holes in the foreground, changing small holes of background into foreground. This technique can also be used to find specific shapes in an image. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((5, 5), np.uint8) image = ... Read More

Prevent Numbers from Changing to Exponential Form in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:17:18

1K+ Views

Using style='plain' in the ticklabel_format() method, we can restrict the value being changed into exponential form.StepsPass two lists to draw a line using plot() method.Use ticklabel_format() method with style='plain'. If a parameter is not set, the corresponding property of the formatter is left unchanged. Style='plain' turns off scientific notation.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt plt.plot([1, 2, 3, 4, 5], [11, 12, 13, 14, 15]) plt.ticklabel_format(style='plain')    # to prevent scientific notation. plt.show()Output

Using Colormaps to Set Line Color in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:16:54

1K+ Views

First, we can create a list of colors, and then, we can use the colors while plotting a line in a loop.StepsReturn evenly spaced numbers over a specified interval, store in x.Update x for four lines and get another variable for evenly_spaced_interval.Make a list of colors.Iterate color and set color for all the lines.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt, cm import numpy as np x = np.linspace(0, 10, 100) lines = [x, x+10, x+5, x+11] evenly_spaced_interval = np.linspace(0, 1, len(lines)) colors = [cm.rainbow(x) for x in evenly_spaced_interval] for i, color in enumerate(colors): ... Read More

Advertisements