Skip to main content

Posts

Showing posts with the label Python

OpenCV Read Video From Camera

 Opencv Python Read Video From Camera import cv2 captureImage = cv2.VideoCapture( 0 ) while ( True ): ret , frame = captureImage.read() gray = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY) cv2.imshow( 'image' , gray) if cv2.waitKey( 1 ) & 0xFF == ord ( 'q' ): break captureImage.release() cv2.destroyAllWindows()

Calculate the Gradient Magnitude and Angle

 How to Calculate the Gradient Magnitude and Angle? import cv2 import numpy as np def calculate (image): image = np.sqrt(image) gx = cv2.Sobel(np.float32(image) , cv2.CV_32F , 1 , 0 ) gy = cv2.Sobel(np.float32(image) , cv2.CV_32F , 0 , 1 ) mag , ang = cv2.cartToPolar(gx , gy) return mag , ang , gx , gy img=cv2.imread( "bird.jpeg" ) m , a , gx , gy=calculate(img) cv2.imshow( "gx" , gx) cv2.imshow( "gy" , gy) print (m) print (a) cv2.waitKey() Input: Output:

Opencv Image Blending: How to blend two images?

Opencv Image Blending This process is very similar to image addition, but in this process images are given different weights. Thus, a transparent mixture is obtained. Images are added according to the following equation: 𝑔(𝑥) = (1 − 𝛼)𝑓0(𝑥) + 𝛼𝑓1(𝑥) By changing the value of a between 0 and 1, transparency can be adjusted. In this study, 2 images were used to put them together. The weight was given 0.3 to the first picture and 0.7 to the second picture. cv2.addWeighted () applies the following equation to the image. 𝑑𝑠𝑡 = 𝛼 · 𝑖𝑚𝑔1 + 𝛽 · 𝑖𝑚𝑔2 + y Open images: image1 = cv2.imread( 'flower.jpg' ) image2 = cv2.imread( 'bird.jpg' ) Blending: rs = cv2.addWeighted(image1 , 0.3 , image2 , 0.7 , 0.0 ); Result: All Code: import cv2 image1 = cv2.imread( 'flower.jpg' ) image2 = cv2.imread( 'bird.jpg' ) image1 = cv2.resize(image1 , ( 300 , 300 )) image2 = cv2.resize(image2 , ( 300 , 300 )) rs = cv2.addWeighted(image1 , 0.3 , image2 , 0.7 , 0.0 ); cv...

How to crop image in OpenCV Python?

Crop image in OpenCV Cropping is the process of selecting and extracting a specific region from the image. For example, we may want to crop a car in an image or we can crop a house in the image. Read a image: img = cv2.imread( "flower.jpg" ) cv2.imshow( "image" , img) Select the boundaries of the image to be cropped: x= 50 y= 50 h= 120 w= 120 Cropping: crop_img = img[y:y+h , x:x+w] cv2.imshow( "cropped" , crop_img) Result: All Code: import cv2 img = cv2.imread( "flower.jpg" ) cv2.imshow( "image" , img) x= 50 y= 50 h= 120 w= 120 crop_img = img[y:y+h , x:x+w] cv2.imshow( "cropped" , crop_img) cv2.waitKey( 0 )

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 = capture.read()   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 = capture.read()   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 = capture.read()   cap.read () 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 cap.open (). The codes are all as follows: import cv2 capture = cv2.VideoCapture( 0 ) while ( True ):   #...

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([]) plt.show() 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([]) plt.show() 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...