Found 10476 Articles for Python

Draw a circle using OpenCV function circle()

Prasad Naik
Updated on 17-Mar-2021 07:56:11

1K+ Views

In this article, we will draw a circle on an image using the OpenCV function circle().Original ImageAlgorithmStep 1: Import OpenCV. Step 2: Define the radius of circle. Step 3: Define the center coordinates of the circle. Step 4: Define the color of the circle. Step 5: Define the thickness. Step 6: Pass the above arguments into cv2.circle() along with the image. Step 7: Display the output.Example Codeimport cv2 image = cv2.imread('testimage.jpg') radius = 100 center = (350, 175) color = (255,255,0) thickness = 15 image = cv2.circle(image, center, radius, color, thickness) cv2.imshow('Circle', image)Output

Draw rectangle on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 07:54:32

4K+ Views

In this program, we will draw a rectangle using the OpenCV function rectangle(). This function takes some parameters like starting coordinates, ending coordinates, color and thickness and the image itself.Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image using imread(). Step 3: Define the starting coordinates. Step 5: Define the ending coordinates. Step 6: Define the color and the thickness. Step 7: Draw the rectangle using the cv2.reactangle() function. Step 8: Display the rectangle.Example Codeimport cv2 image = cv2.imread('testimage.jpg') height, width, channels = image.shape start_point = (0, 0) end_point = (width, height) color = (0, 0, 255) thickness ... Read More

Making matplotlib scatter plots from dataframes in Python's pandas

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:54:06

1K+ Views

Using Pandas, we can create a dataframe and can create a figure and axes variable using subplot() method. After that, we can use the ax.scatter() method to get the required plot.StepsMake a list of the number of students.Make a list of marks that have been obtained by the students.To represent the color of each scattered point, we can have a list of colors.Using Pandas, we can have a list representing the axes of the data frame.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Set the “Students count” label using plt.xlabel() method.Set the “Obtained marks” ... Read More

Manually add legend Items Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:52:12

23K+ Views

Using plt.legend() method, we can create a legend, and passing frameon would help to keep the border over there.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Draw lines using plot() method.Location and legend drawn flags can help to find a location and make the flag True for the border.Set the legend with “blue” and “orange” elements.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", "orange"], loc=0, frameon=legend_drawn_flag) plt.show()OutputRead More

Draw an ellipse on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 07:52:58

932 Views

In this program, we will draw an ellipse on an image in using the OpenCV library. We will use the OpenCV function ellipse() for the same.Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image using imread(). Step 3: Set the center coordinates. Step 4: Set the axes length. Step 5: Set the angle. Step 6: Set start and end angle. Step 6: Set the color. Step 7: Set the thickness. Step 8: Draw the ellipse by passing the above parameters in the cv2.ellipse function along with the original image. Step 9: Display the final output.Example Codeimport cv2 image = ... Read More

Draw a line on an image using OpenCV

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

1K+ Views

In this program, we will draw a simple line on an image using the OpenCV function line().Original ImageAlgorithmStep 1: Import cv2. Step 2: Read the image using imread(). Step 3: Get the dimensions of the image using the image.shape method. Step 4: Define starting point of the line. Step 5: Define the end point of the line. Step 6: Define the thickness of the line. Step 7: Draw the line using the cv2.line() function and pass Step 3 to Step 4 as parameters.Example Codeimport cv2 image = cv2.imread('testimage.jpg') height, width, channels = image.shape startpoint = (0, 0) endpoint = ... Read More

Converting an image from colour to grayscale using OpenCV

Prasad Naik
Updated on 17-Mar-2021 07:57:34

16K+ Views

In this program, we will change the color scheme of an image from rgb to grayscaleAlgorithmStep 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2.cvtcolor() function.Example Codeimport cv2 image = cv2.imread('colourful.jpg') cv2.imshow('Original',image) grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale', grayscale)OutputOriginal Image:Grayscale Image:

Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:58:03

3K+ Views

In this article, we can take a program code to show how we can make a 3D plot interactive using Jupyter Notebook.StepsCreate a new figure, or activate an existing figure.Create fig and ax variables using subplots method, where default nrows and ncols are 1, projection=’3d”.Get x, y and z using np.cos and np.sin function.Plot the 3D wireframe, using x, y, z and color="red".Set a title to the current axis.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * ... Read More

How do you plot a vertical line on a time series plot in Pandas?

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:58:29

4K+ Views

Using Pandas, we will create a dataframe and set the vertical lines on the created axes, using axvline lines.StepsUsing panda we can create a data frame.Creating a data frame would help to create help.Using axvline(), add a vertical line across the axes, where color is green, linestyle="dashed".Using axvline(), add a vertical line across the axes, where color is red, linestyle="dashed".Using plt.show(), show the plot.Exampleimport pandas as pd from matplotlib import pyplot as plt df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31")) df["y"] = 1 ax = df.plot() ax.axvline("2019-07-24", color="green", linestyle="dashed") ax.axvline("2019-07-31", color="red", linestyle="dashed") plt.show()OutputRead More

Reading and displaying images using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:00:05

2K+ Views

In this article, we will learn how to read and display images using the OpenCV library.OpenCV is a library of programming functions mainly aimed at real time computer vision. Before reading an image, make sure that the image is in the same directory as your program.AlgorithmStep 1: Import OpenCV. Step 2: Read an image using imread(). Step 3: Display the image using imshow().Example Codeimport cv2 as cv image = cv.imread ('ronaldo.jpg') cv.imshow('Cristiano Ronaldo', image)Output

Advertisements