Skip to main content

Posts

Showing posts from May, 2021

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 , 10 ,

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 )