Skip to main content

Posts

How to Read Video in Opencv Python

How to Read Video Δ°n Opencv Python The following code is used to read video in OpenCV python. The process of reading video in OpenCV is very similar to reading images. The video is a series of images. All frames in a video sequence must be looped over and then rendered one frame at a time. import cv2 capture = cv2.VideoCapture( 'video.mp4' ) while (capture.isOpened()): r , f = capture.read() gray = cv2.cvtColor(f , cv2.COLOR_BGR2GRAY) cv2.imshow( 'video' , gray) if cv2.waitKey( 1 ) & 0xFF == ord ( 'q' ): break capture.release() cv2.destroyAllWindows()

OpenCV Resize Image With Aspect Ratio

OpenCV Resize Image With Aspect Ratio The OpenCV resize() method, as the name suggests, resizes the image.  import cv2 def resize (i , window_height = 500 ): aspect_ratio = float (i.shape[ 1 ]) / float (i.shape[ 0 ]) window_width = window_height/aspect_ratio i = cv2.resize(i , ( int (window_height) , int (window_width))) return i img = cv2.imread( 'image.jpg' ) img_resized = resize(img , window_height = 640 ) cv2.imshow( "ResizedImage" , img_resized) cv2.waitKey( 0 ) 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 )

Image Addition in OpenCV Python Image Processing

Image Addition In OpenCV, images can be addition in two different ways. One of them is adding with the command cv2.add (). The other is additioning with numpy. additioning with numpy can simply be summed as image = img1 img2. import cv2 import numpy as np x = np.uint8([ 200 ]) y = np.uint8([ 100 ]) print (cv2.add(x , y)) print (x+y) Result: