Draw a Filled Polygon Using OpenCV Function fillPoly

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

8K+ Views

In this program, we will draw a filled polygon using the opencv function fillPoly(). The function takes in an image and the endpoints of the polygon.AlgorithmStep 1: Import cv2 and numpy. Step 2: Define the endpoints. Step 3: Define the image using zeros. Step 4: Draw the polygon using the fillpoly() function. Step 5: Display the output.Example Codeimport cv2 import numpy as np contours = np.array([[50,50], [50,150], [150,150], [150,50]]) image = np.zeros((200,200)) cv2.fillPoly(image, pts = [contours], color =(255,255,255)) cv2.imshow("filledPolygon", image)Output

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

Multiple Axes in Matplotlib with Different Scales

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:55:01

4K+ Views

In the following code, we will see how to create a shared Y-axis.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Plot line with lists passed in the argument of plot() method with color="red".Create a twin of Axes with a shared X-axis but independent Y-axis.Plot the line on ax2 that is created in step 3.Adjust the padding between and around subplots.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax1.plot([1, 2, 3, 4, 5], [3, 5, 7, 1, 9], color='red') ax2 = ax1.twinx() ax2.plot([11, 12, 31, 41, 15], [13, 51, ... Read More

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

Prevent Scientific Notation in Matplotlib Pyplot

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:53:27

20K+ Views

To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.StepsPass two lists to draw a line using plot() method.Using 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

Draw an Ellipse on an Image Using OpenCV

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

930 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

Manually Add Legend Items in 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

Show Only Certain Items in Legend using Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:51:23

2K+ Views

Using plt.legend(), we can add or show certain items just by putting the values in the list.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Plot the lines using the lists that are passed in the plot() method argument.Location and legend_drawn flags can help to find a location and make the flag True for 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", ... Read More

Blurring an Image Using OpenCV Gaussian Blur Function

Prasad Naik
Updated on 17-Mar-2021 07:50:47

556 Views

In this program, will blur an image using the openCV function GaussianBlur(). Gaussian blur is the process of blurring an image using the gaussian function. It is widely used in graphics software to remove noise from the image and reduce detail.AlgorithmStep 1: Import cv2. Step 2: Read the original image. Step 3: Apply gaussian blur function. Pass the image and the kernel size as parameter. Step 4: Display the image.Original ImageExample Codeimport cv2 image = cv2.imread("testimage.jpg") Gaussian = cv2.GaussianBlur(image, (7,7), 0) cv2.imshow("Gaussian Blur", Gaussian)OutputGaussian Blur:

Advertisements