본문 바로가기

OpenCV9

[OpenCV] bitwise.py: BITWISE Operations import cv2 as cv import numpy as np # Blank blank = np.zeros((400,400), dtype='uint8') # Rectangle, Circle rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1) circle = cv.circle(blank.copy(), (200,200), 200, 255, -1) cv.imshow('Rectangle', rectangle) cv.imshow('Circle', circle) # Bitwise Operations # 1. Bitwise AND bitwise_and = cv.bitwise_and(rectangle, circle) cv.imshow('Bitwis.. 2024. 4. 11.
[OpenCV] smoothing.py: Blurring Techniques import cv2 as cv # Reading an image img = cv.imread('Photos/cats.jpg') cv.imshow('Cats', img) # 1. Averaging Blur average = cv.blur(img, (7,7)) cv.imshow('Average Blur', average) # 2. Gaussian Blur gauss = cv.GaussianBlur(img, (7,7), 0) cv.imshow('Gaussian Blur', gauss) # 3. Median Blur median = cv.medianBlur(img, 7) cv.imshow('Median Blur', median) # 4. Bilateral Blur bilateral = cv.bilateralFi.. 2024. 4. 11.
[OpenCV] splitmerge.py: Color Channels import cv2 as cv import numpy as np # Reading an image img = cv.imread('Photos/boston.jpg') cv.imshow('Boston', img) # Blank image blank = np.zeros(img.shape[:2], dtype='uint8') # 1. Splitting a color b,g,r = cv.split(img) blue = cv.merge([b, blank, blank]) green = cv.merge([blank, g, blank]) red = cv.merge([blank, blank, r]) cv.imshow('Blue', blue) cv.imshow('Green', green) cv.imshow('Red', red.. 2024. 4. 10.
[OpenCV] contours.py Contour Detection import cv2 as cv import numpy as np # Reading an image img = cv.imread('Photos/cats.jpg') cv.imshow('Cats', img) # Blank image blank = np.zeros(img.shape, dtype='uint8') cv.imshow('Blank', blank) # Converting to grayscale gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.imshow('Gray', gray) # Blur blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT) cv.imshow('Blur', blur) # Edg.. 2024. 4. 10.
[OpenCV] transformations.py Image Transformations import cv2 as cv import numpy as np # Reading an image img = cv.imread('Photos/boston.jpg') cv.imshow('Boston', img) # 1. Translation def translate(img, x, y): transMat = np.float32([[1,0,x], [0,1,y]]) dimensions = (img.shape[1], img.shape[0]) return cv.warpAffine(img, transMat, dimensions) # -x --> Left # x --> Right # -y --> Up # y --> Down translated = translate(img, 100.. 2024. 4. 10.
[OpenCV] basic.py Basic Functions in OpenCV import cv2 as cv # Reading an image img = cv.imread('Photos/boston.jpg') cv.imshow('Boston', img) # 1. Converting to grayscale gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.imshow('Gray', gray) # 2. Blur blur = cv.GaussianBlur(img, (7,7), cv.BORDER_DEFAULT) cv.imshow('Blur', blur) # 3. Edge cascade canny = cv.Canny(img, 125, 175) cv.imshow('Canny Edges', canny) # 4. Dil.. 2024. 4. 10.
[OpenCV] draw.py import cv2 as cv import numpy as np # Blank image # np.zeros: height, weight, number of color channels blank = np.zeros((500,500,3), dtype='uint8') cv.imshow('Blank', blank) # 1. Paint the image a certain color (BGR) blank[200:300, 300:400] = 0,255,0 cv.imshow('Green', blank) # 2. Draw a rectangle cv.rectangle(blank, (0,0), (250,500), (0,255,0), thickness=2) cv.imshow('Rectangle', blank) # 3. Dr.. 2024. 4. 10.
[OpenCV] rescale.py Resizing an image import cv2 as cv # Scale the image in a particular size def rescaleFrame(frame, scale=0.5): width = int(frame.shape[1]*scale) height = int(frame.shape[0]*scale) dimensions = (width, height) return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA) img = cv.imread('Photos/cat.jpg') cv.imshow('Cat', img) cv.imshow('Resize Cat', rescaleFrame(img)) cv.waitKey(0) Resizing a v.. 2024. 4. 10.
[OpenCV] read.py Reading an image import cv2 as cv # Reading an image img = cv.imread('Photos/cat_large.jpg') cv.imshow('Cat', img) cv.waitKey(0) Reading a video import cv2 as cv # Reading a video capture = cv.VideoCapture('Videos/dog.mp4') while True: isTrue, frame = capture.read() cv.imshow('Video', frame) if cv.waitKey(20) & 0xFF==ord('d'): break capture.release() cv.destroyAllWindows() 2024. 4. 10.