Found 10476 Articles for Python

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

682 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

Dilating images using the OpenCV function dilate()

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

688 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

Eroding an image using the OpenCV function erode()

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

572 Views

In this program, we will erode an image using the OpenCV function erode(). Erosion of image means to shrink the image. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on image is that the image should be a grayscale image.Original ImageAlgorithmStep 1: Import cv2 Step 2: Import numpy. Step 3: Read the image using imread(). Step 4: Define the kernel size using numpy ones. Step 5: Pass the image and kernel to the erode function. Step 6: Display the output.Example ... Read More

Show only certain items in legend 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 the OpenCV function Gaussian Blur()

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

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

Blurring an image using the OpenCV function blur()

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

417 Views

In this program, we will blur an image using the opencv function blur().AlgorithmStep 1: Import OpenCV. Step 2: Import the image. Step 3: Set the kernel size. Step 4: Call the blur() function and pass the image and kernel size as parameters. Step 5: Display the results.Original ImageExample Codeimport cv2 image = cv2.imread("testimage.jpg") kernel_size = (7,7) image = cv2.blur(image, kernel_size) cv2.imshow("blur", image)OutputBlurred ImageExplanationThe kernel size is used to blur only a small part of an image. The kernel moves across the entire image and blurs the pixels it covers.

Display text on an OpenCV window by using the function putText()

Prasad Naik
Updated on 17-Mar-2021 07:49:09

5K+ Views

In this program, we will write text on an image using the opencv function putText(). This function takes in the image, font, coordinates of where to put the text, color, thickness, etc.Original ImageAlgorithmStep 1: Import cv2 Step 2: Define the parameters for the puttext( ) function. Step 3: Pass the parameters in to the puttext() function. Step 4: Display the image.Example Codeimport cv2 image = cv2.imread("testimage.jpg") text = "TutorialsPoint" coordinates = (100,100) font = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 color = (255,0,255) thickness = 2 image = cv2.putText(image, text, coordinates, font, fontScale, color, thickness, cv2.LINE_AA) cv2.imshow("Text", image)Output

Draw a filled polygon using the 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

Advertisements