Posts

How to Adding and Blending image in OpenCV.

Image
import cv2 import numpy as np img = cv2.imread( 'Mehjabin.png' ) img2 = cv2.imread( 'ImageCV.jpg' ) resize_img = cv2.resize(img2 , (img.shape[ 1 ] , img.shape[ 0 ])) add_img = cv2.add(img , resize_img) Blending_img = cv2.addWeighted(img , 0.6 , resize_img , 0.4 , 0 ) while True : cv2.imshow( 'window' , img) cv2.imshow( 'window2' , img2) cv2.imshow( 'window3' , Blending_img) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()

Making Border on Image, splitting and merging Color Channels in opencv.

import cv2 import numpy as np img = cv2.imread( 'ImageCV.jpg' ) blue , green , red = cv2.split(img) img2 = cv2.merge((green , blue , red)) replicate = cv2.copyMakeBorder(img , 20 , 20 , 20 , 20 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img , 20 , 20 , 20 , 20 , cv2.BORDER_REFLECT) reflect_101 = cv2.copyMakeBorder(img , 20 , 20 , 20 , 20 , cv2.BORDER_REFLECT_101) constant = cv2.copyMakeBorder(img , 20 , 20 , 20 , 20 , cv2.BORDER_CONSTANT , None, ( 0 , 0 , 255 )) wrap = cv2.copyMakeBorder(img , 20 , 20 , 20 , 20 , cv2.BORDER_WRAP) while True : cv2.imshow( 'original' , img) cv2.imshow( 'replicate' , replicate) cv2.imshow( 'reflect' , reflect) cv2.imshow( 'reflect_101' , reflect_101) cv2.imshow( 'constant' , constant) cv2.imshow( 'wrap' , wrap) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()

Core operation on an image in opencv.

Image
import cv2 import numpy as np img = cv2.imread( 'mehjabin.png' ) pixel = img[ 100 , 100 ] print (pixel) blue = img[ 100 , 100 , 2 ] print (blue) print (img.shape) print (img.size) print (img.dtype) roi = img[ 80 : 300 , 0 : 500 ] while True : cv2.imshow( 'window' , img) cv2.imshow( 'window2' , roi) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()

How to create a simple application which show the color you specify.

Image
import cv2 import numpy as np black_image = np.zeros(( 400 , 600 , 3 ) , np.uint8) print (black_image) def nothing ( x ): pass cv2.namedWindow( 'window' ) cv2.createTrackbar( 'Red' , 'window' , 0 , 255 , nothing) cv2.createTrackbar( 'Green' , 'window' , 0 , 255 , nothing) cv2.createTrackbar( 'Blue' , 'window' , 0 , 255 , nothing) while True : cv2.imshow( 'window' , black_image) if cv2.waitKey( 1 ) == 27 : break r = cv2.getTrackbarPos( 'Red' , 'window' ) g = cv2.getTrackbarPos( 'Green' , 'window' ) b = cv2.getTrackbarPos( 'Blue' , 'window' ) black_image[:] = [b , g , r] cv2.destroyAllWindows()

How to Create a simple application which draw a circle on an image wherever you click on it.

Image
import cv2 import numpy as np black_image = np.zeros(( 500 , 700 , 3 ) , np.uint8) def draw_circle (events , x , y , flags , param ): if events == cv2.EVENT_LBUTTONUP: cv2.circle(black_image , (x , y) , 90 , ( 0 , 0 , 255 ) , 3 ) cv2.namedWindow( 'window' ) cv2.setMouseCallback( 'window' , draw_circle) while True : cv2.imshow( 'window' , black_image) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()

How draw Line, Rectangle, Circle, Triangle, and write text in openCV.

import cv2 import numpy as np black_image = np.zeros(( 600 , 800 , 3 ) , np.uint8) cv2.line(black_image , ( 0 , 0 ) , ( 800 , 600 ) , ( 255 , 0 , 0 ) , 3 ) cv2.rectangle(black_image , ( 0 , 0 ) , ( 300 , 400 ) , ( 255 , 0 , 0 ) , 3 ) cv2.circle(black_image , ( 400 , 300 ) , 100 , ( 255 , 0 , 0 ) , 2 ) cv2.circle(black_image , ( 200 , 150 ) , 100 , ( 255 , 0 , 0 ) , - 1 ) pts = np.array([[ 0 , 600 ] , [ 800 , 600 ] , [ 400 , 300 ]]) cv2.polylines(black_image , [pts] , True, ( 255 , 255 , 0 ) , 2 ) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(black_image , 'you are best' , ( 0 , 300 ) , font , 4 , ( 0 , 255 , 0 )) while True : cv2.imshow( 'window' , black_image) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()

Playing a video from file with color BGR, RGB, GRAY, HSV in OpenCV

Image
import cv2 capture = cv2.VideoCapture( 'video_1.mp4' ) while True : ret , frame = capture.read() gray = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY) HSV = cv2.cvtColor(frame , cv2.COLOR_BGR2HSV) RGB = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB) cv2.imshow( 'Normal plus BGR' , frame) cv2.imshow( 'Gray' , gray) cv2.imshow( 'HSV' , HSV) cv2.imshow( 'RGB' , RGB) if cv2.waitKey( 1 ) == 27 : break cv2.destroyAllWindows()