Updated April 10, 2023
Introduction to OpenCV GUI
The interactive visual components in OpenCV is called Graphical User Interface or GUI and features in OpenCV GUI are reading an image, displaying the image, writing an image to a file, to draw a line, to draw a circle, to draw a rectangle, to draw an ellipse, to draw a text, etc. using imread() function, imwrite() function, imshow() function, line() function, rectangle() function, ellipse() function and polylines() function, and all these features are available as processing functions and can be accessed from the Graphical User Interface or GUI.
The syntax to define imread() function in OpenCV is as follows:
imread('path_to_the_file', flag)
where path_to_the_file specifies path to the location of the file where the file is present which it to be read and
flag specifies the mode in which the file must-read. There are three modes in which the file can be read namely color mode, grayscale mode and the image as is. This parameter is optional.
The syntax to define imshow() function in OpenCV is as follows:
imshow(window_name, image)
where window_name is the name of the window within which the given image must be displayed and
image is the image to be displayed in a window.
The syntax to define imwrite() function in OpenCV is as follows:
returnvalue = imwrite(file_path, image)
where file_path is the location of the file in which the image is to be written or saved,
image is the image to be written or saved to the file and
returnvalue is the Boolean value returned by the imwrite() function.
The syntax to define line() function in OpenCV is as follows:
line(input_image, start_point, end_point, color, thickness)
where input_image is the image on which the line must be drawn,
start_point represents the x coordinate of the line,
end_point represents the y coordinate of the line,
color represents the color of the line to be drawn and
thickness represents the thickness of the line to be drawn.
The syntax to define rectangle() function in OpenCV is as follows:
rectangle(input_image, int left, int top, int right, int bottom)
where input_image is the image on which the rectangle must be drawn,
left represents the x coordinate in the top left corner,
top represents the y coordinate in the top left corner,
right represents the x coordinate in the bottom right corner and
bottom represents they coordinate in the bottom right corner.
The syntax to define ellipse() function in OpenCV is as follows:
ellipse(input_image, center_coordinates, axeslength, angle, startangle, endangle, color, thickness)
where input_image is the image on which the ellipse must be drawn,
center_coordinates represents the center of the ellipse,
axeslength is a tuple of two variables representing major and minor axis of ellipse,
angle represents the rotation angle of ellipse in degrees,
startangle is the starting angle of the elliptic angle in degrees,
endangle is the ending angle of the elliptic angle in degrees and
color is the color of the ellipse to be drawn and
thickness is the thickness of the ellipse to be drawn.
The syntax to define polylines() function in OpenCV is as follows:
polylines(input_image, array, isclosed, color, thickness)
where input_image is the image on which a polygon must be drawn,
array is the polygonal curves array,
isclosed is a Boolean value whose value is true if the polygon to be drawn is closed or the value is false if the polygon to be drawn is not closed.
color is the color of the polygon to be drawn and
thickness is the thickness of the polygon to be drawn.
Working of GUI Features in OpenCV
Working of GUI features in OpenCV is as follows:
- imread() function is used to read the image from a given specified file.
- imshow() function is used to display the given file.
- imwrite() function is used to save the given image to a file.
- line() function is used to draw a line
- rectangle() function is used to draw a rectangle.
- ellipse() function is used to draw an ellipse.
- polylines() function is used to draw a polygon.
Examples
Let us discuss examples of OpenCV GUI.
Example #1
OpenCV program in python to demonstrate imread(), imwrite() and imshow() function.
import cv2
readimage = cv2.imread('C:/Users/admin/Desktop/Images/logo.png', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('C:/Users/admin/Desktop/Images/results/logo.png', readimage)
cv2.imshow("resulting_image", readimage)
cv2.waitKey(0)
The output of the above program is shown in the snapshot below:
In the above program, we are reading an image using imread() function and then writing the image to a file using imwrite() function and finally displaying the image using imshow() function. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate line() function
import cv2
readimage = cv2.imread('C:/Users/admin/Desktop/Images/logo.png', cv2.IMREAD_GRAYSCALE)
result = cv2.line(readimage,(0,0),(511,511),(255,0,0),5)
cv2.imshow("resulting_image", readimage)
cv2.waitKey(0)
The output of the above program is shown in the snapshot below:
In the above program, we are reading an image using imread() function and then drawing a line on the image using line() function and finally displaying the image using imshow() function. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate rectangle() function:
import cv2
readimage = cv2.imread('C:/Users/admin/Desktop/Images/plane.jpg', cv2.IMREAD_GRAYSCALE)
result = cv2.rectangle(readimage,(384,0),(510,128),(0,255,0),3)
cv2.imshow("resulting_image", readimage)
cv2.waitKey(0)
The output of the above program is shown in the snapshot below:
In the above program, we are reading an image using imread() function and then drawing a rectangle on the image using rectangle() function, and finally displaying the image using imshow() function. The output is shown in the snapshot above.
Example #4
OpenCV program in python to demonstrate ellipse() function
import cv2
readimage = cv2.imread('C:/Users/admin/Desktop/Images/plane.jpg', cv2.IMREAD_GRAYSCALE)
result = cv2.ellipse(readimage,(256,256),(100,50),0,0,180,255,-1)
cv2.imshow("resulting_image", readimage)
cv2.waitKey(0)
The output of the above program is shown in the snapshot below:
In the above program, we are reading an image using imread() function and then drawing an ellipse on the image using ellipse() function and finally displaying the image using imshow() function. The output is shown in the snapshot above.
Example #5
OpenCV program in python to demonstrate polylines() function:
import cv2
import numpy as np
readimage = cv2.imread('C:/Users/admin/Desktop/Images/plane.jpg', cv2.IMREAD_GRAYSCALE)
arraypoints = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
arraypoints = arraypoints.reshape((-1,1,2))
result = cv2.polylines(readimage,[arraypoints],True,(0,255,255))
cv2.imshow("resulting_image", readimage)
cv2.waitKey(0)
The output of the above program is shown in the snapshot below:
In the above program, we are reading an image using imread() function and then drawing a polygon on the image using polylines() function, and finally displaying the image using imshow() function. The output is shown in the snapshot above.
Conclusion
In this article, we have learnt the concept of GUI features in OpenCV with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV GUI” was beneficial to you. You can view EDUCBA’s recommended articles for more information.