Skip to main content


Showing posts from February, 2021

OpenCV Crop an image in Python

 Crop operation in OpenCV is very simple. You should first read the image. Then the values x, y, h, w must be determined to specify the region to be cropped. You can do this using the code below. import cv2 image = cv2.imread( 'image.jpg' ) y= 10 x= 20 h= 200 w= 250 crop = image[y:y+h , x:x+w] cv2.imshow( 'Image' , crop) cv2.waitKey( 0 )

OpenCV Create Video From Array Of Images

  OpenCV Create Video From Array Of Images in Python  The following code can be used to generate video from image array in openCV. How many seconds you want an image to stay on the screen, you have to add that much of the image. For example, image 1 will wait for 4 seconds on the screen, while image 2 will wait for 1 second. import cv2 frames = [cv2.imread( 'image1.jpg' ) , cv2.imread( 'image1.jpg' ) , cv2.imread( 'image1.jpg' ) , cv2.imread( 'image1.jpg' ) , cv2.imread( 'image2.jpg' )] height , width , _ = frames[ 0 ].shape out = cv2.VideoWriter( 'output.avi' , cv2.VideoWriter_fourcc(* 'DIVX' ) , 1 , (width , height)) [out.write(f) for f in frames] out.release()

OpenCV Drawing a Line

How To Draw a Line In OpenCV Python? To draw a line, we need to select the starting and ending coordinates of the line. First, let's create a black image and draw a green line. We need to use the code below to create a black image: image = np.zeros(( 360 , 360 , 3 ) , np.uint8) Now we can create our line. cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) The code for creating a line is as follows: import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output: Another Example import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 180 ) , ( 360 , 180 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output:

Saving a Video OpenCV Python

Saving a Video In this lesson, we will learn how to record video in OpenCV. This process is very simple for images. Only the cv2.imwrite () function is used. Recording videos is a bit more difficult. Let's create a VideoWriter object for this process. We must determine the name of the file to be saved. Then we have to specify the FourCC code.  Then the number of frames per second and frame size must be selected. import cv2 capture = cv2.VideoCapture( 0 ) # Create VideoWriter object fourcc = cv2.VideoWriter_fourcc(* 'XVID' ) out = cv2.VideoWriter( 'recorded.avi' , fourcc , 20.0 , ( 640 , 480 )) while (capture.isOpened()):  ret , frame =   if ret== True :   frame = cv2.flip(frame , 0 )   out.write(frame)   cv2.imshow( 'frame' , frame)   if cv2.waitKey( 1 ) & 0xFF == ord ( 'e' ):    break   else :    break capture.release() out.release() cv2.destroyAllWindows()

Playing Video From File OpenCV Python

Playing Video From File Video playback from file is similar to capturing video from camera. For this operation, only the video file name and the camera directory should be changed. Also, cv2.waitKey () is used when viewing the frame.  capture = cv2.VideoCapture( 'testvideo.avi' ) while (capture.isOpened()):   ret , frame =   cv2.imshow( 'frame' , frame)   if cv2.waitKey( 1 ) & 0xFF == ord ( 'e' ):   break capture.release() cv2.destroyAllWindows()  

OpenCV Capture Image From Camera

Capture Image From Camera Using OpenCV, you can capture video from your computer's webcam and use these videos for many purposes. OpenCV provides a very simple interface for video capture process. It takes preparation to capture video. To capture a video, you must create a VideoCapture object. Its argument may be the device index. capture = cv2.VideoCapture( 0 )   The device index is just the number that determines which camera to use. Normally 0 is selected if a camera will be connected. You can select the second camera by giving a value of 1. After that, you can shoot frame by frame. ret , frame = () returns a bool value. If the frame reads correctly, the value will be True. With the Cap.isOpened () method, you can check whether the video capture process is started. If it's true, it's okay. If not, you need to open it using (). The codes are all as follows: import cv2 capture = cv2.VideoCapture( 0 ) while ( True ):   # C

Show OpenCV Image With Matplotlib

Show Image With Matplotlib Matplotlib is a drawing library that offers a wide variety of drawing methods for Python. Matplotlib; The basic python library we use in data visualization. It allows us to make 2 and 3 dimensional drawings. In order to use Matplotlib, we need to add it to our project as follows: from matplotlib import pyplot as plt The image is read as follows: img = cv2.imread( 'bird.jpg' )   Finally, the captured image is displayed on the screen with the following code: plt.imshow(img) plt.xticks([]) , plt.yticks([]) Code and output are as follows: import cv2 from matplotlib import pyplot as plt img = cv2.imread( 'bird.jpg' ) plt.imshow(img) plt.xticks([]) , plt.yticks([]) Output:  

Opencv Read-Write-Display Image Python

Read-Write-Display Image In this lesson, you will learn how to read, how to view and save. The functions cv2.imread (), cv2.imshow (), cv2.imwrite () will be used for these operations. To read an image, the function cv2.imread () is used. The image to be read must be in the working directory or a full visual path must be given. The second argument is a sign that indicates how the image should be read. cv2.IMREAD_COLOR: Indicates that a color image will be loaded. It is the default flag. The integer value for this flag can be assigned to 1. cv2.IMREAD_GRAYSCALE: Loads the image in grayscale mode. cv2.IMREAD_UNCHANGED: Loads the image as included in the alpha channel. import cv2 # The code below reads the image and assigns it to the image variable. image = cv2.imread( 'bird' ) The function cv2.imshow () is used to display an image. The window opens automatically in image size. The first argument is the name of the window to open. The second argument is the image we re