Skip to main content

Posts

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()

OpenCV Python Webcam Control

 OpenCV Python Webcam Control import cv2 cap = cv2.VideoCapture( 0 ) if not cap.isOpened(): raise IOError ( "webcam not available" ) while True : ret , f = cap.read() f = cv2.resize(f , None, fx = 0.5 , fy = 0.5 , interpolation =cv2.INTER_AREA) cv2.imshow( 'I' , f) c = cv2.waitKey( 1 ) if c == 27 : break cap.release() cv2.destroyAllWindows()

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:

OpenCV Add Border to Image in Python

How to add border to image in python? To create a frame around the image, the function cv2.copyMakeBorder () is used. This function takes the following arguments: src - login image top, bottom, left, right - edge width in number of pixels in corresponding directions borderType - defines what type of border to add. There may be the following types cv2.BORDER_CONSTANT  cv2.BORDER_REFLECT - cv2.BORDER_REFLECT_101 cv2.BORDER_REPLICATE cv2.BORDER_WRAP  value  - Color of border if border type is cv2.BORDER_CONSTANT import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [ 255 , 0 , 0 ] img1 = cv2.imread( 'flower.jpg' ) replicate = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1 ...

Opencv Image Properties - rows, columns and channels, type of image data, number of pixels

Opencv Image Properties  OpenCV allows access to image properties. These properties are rows, columns and channels, type of image data, number of pixels. The img.shape command is used to access the shape of the image. Returns rows, columns and channels. import  cv2 img = cv2.imread( 'flower.jpg' ) print (img.shape) Result: (400, 400, 3) The data type of the image is obtained with img.dtype import cv2 img = cv2.imread( 'flower.jpg' ) print (img.dtype) Result: uint8

How to create trackbar in Opencv Python?

Create trackbar in Opencv and Python import cv2 import numpy as np def func ( x ): pass img = cv2.imread( "flower.jpg" ) cv2.namedWindow( 'image' ) cv2.createTrackbar( 'R' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'G' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'B' , 'image' , 0 , 255 , func) switch = '0 : OFF \n 1 : ON' cv2.createTrackbar(switch , 'image' , 0 , 1 , func) while ( 1 ): cv2.imshow( 'image' , img) k = cv2.waitKey( 1 ) & 0xFF if k == 27 : break r = cv2.getTrackbarPos( 'R' , 'image' ) g = cv2.getTrackbarPos( 'G' , 'image' ) b = cv2.getTrackbarPos( 'B' , 'image' ) s = cv2.getTrackbarPos(switch , 'image' ) if s == 0 : img[:] = 0 else : img[:] = [b , g , r] cv2.destroyAllWindows() Trackbar

How to count pixel in Python?

 How to count pixel in Image? Tutorial How to count pixel in image using Python and Opencv? First let's read our image: image=cv2.imread( 'flower.jpg' )  Convert the image to rgb: image=cv2.cvtColor(image , cv2.COLOR_BGR2RGB) Create a numpy array: array = np.array(image) Arrange all pixels into a tall column of 3 RGB values and find unique rows: colours , counts = np.unique(array.reshape(- 1 , 3 ) , axis = 0 , return_counts = 1 ) Prints: print (colours) print (counts) All Code: import numpy as np import cv2 image=cv2.imread( 'flower.jpg' ) cv2.imshow( "Image" , image) image=cv2.cvtColor(image , cv2.COLOR_BGR2RGB) array = np.array(image) colours , counts = np.unique(array.reshape(- 1 , 3 ) , axis = 0 , return_counts = 1 ) print (colours) print (counts) 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()