Found 184 Articles for OpenCV

Eroding an image using the OpenCV function erode()

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

392 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

Blurring an image using the OpenCV function Gaussian Blur()

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

387 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

317 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

4K+ 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

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

804 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

Draw an ellipse on an image using OpenCV

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

671 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

871 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

14K+ 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:

Advertisements