Found 33676 Articles for Programming

Performing white TopHat operation on images using OpenCV

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

687 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

Putting a 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

Generating a movie from Python without saving individual frames to files

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:14:06

377 Views

Using the FuncAnimation method, we can create a film. We will create a user-defined method, update, to keep on changing the position of particles and at the end, the method would return the scatter instance.StepsGet the particles initial position, velocity, force, and size.Create a new figure, or activate an existing figure with figsize = (7, 7).Add an axes to the current figure and make it the current axes, with xlim and ylim.Plot the scatter for initial position of the particles.Makes an animation by repeatedly calling a function *func*. We can pass a user-defined method that helps to change the position ... Read More

Using Colormaps to set the color of line 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

How to prevent numbers being changed 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

Calculating the median of all pixels for each band in an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:16:31

889 Views

In this program, we will calculate the MEDIAN of all the pixels in each channel using the Pillow library. There are a total 3 channels in an image and therefore we will get a list of three values.Original ImageAlgorithmStep 1: Import the Image and ImageStat libraries. Step 2: Open the image. Step 3: Pass the image to the stat function of the imagestat class. Step 4: Print the median of the pixels.Example Codefrom PIL import Image, ImageStat im = Image.open('image_test.jpg') stat = ImageStat.Stat(im) print(stat.median)Output[41, 43, 40]

Drawing average line in histogram in 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

Performing a 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

Performing an opening operation on an image using OpenCV

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

683 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

Plotting dates on the X-axis with Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 28-Aug-2023 12:43:55

34K+ Views

Using Pandas, we can create a dataframe and can set the index for datetime. Using gcf().autofmt_xdate(), we will adjust the date on the X-axis.StepsMake the list of date_time and convert into it in date_time using pd.to_datetime().Consider data = [1, 2, 3]Instantiate DataFrame() object, i.e., DF.Set the DF['value'] with data from step 2.Set DF.index() using date_time from step 1.Now plot the data frame i.e., plt.plot(DF).Get the current figure and make it autofmt_xdate().Using plt.show() method, show the figure.Exampleimport pandas as pd import matplotlib.pyplot as plt date_time = ["2021-01-01", "2021-01-02", "2021-01-03"] date_time = pd.to_datetime(date_time) data = [1, 2, 3] DF = ... Read More

Advertisements