상세 컨텐츠

본문 제목

OpenCV fundamentals

Coding/Image

by linguana 2021. 4. 9. 10:10

본문

OpenCV for 3 hours

youtu.be/WQeoO7MI0Bs

 

murtazahassan/Learn-OpenCV-in-3-hours (github.com)

 

murtazahassan/Learn-OpenCV-in-3-hours

Contribute to murtazahassan/Learn-OpenCV-in-3-hours development by creating an account on GitHub.

github.com


해상도

  • VGA = 640 * 480 (w * h)
  • HD = 1280 * 720
  • FHD = 1920 * 1080
  • 4K = 3840 * 2160

Binary image (8bits (2^8 = 256) levels shades of gray, grayscale) vs RGB image


Ch1. Read images, videos, and webcams

1. reading image

import cv2
print("Package Imported")

img = cv2.imread("Resources/lena.png")

cv2.imshow("Output", img)
cv2.waitkey(0) # 1000 == 1 second

2. reading video

import cv2

cap = cv2.VideoCapture("Resources/test_video.mp4")

while True:
	success, img = cap.read() # success = Boolean
    cv2.imshow("Video", img)
    if cv2.waitKey(1) & 0xFF == ord('q'): # adds delay and user's key press
    	break

3. reading webcam

import cv2

cap = cv2.VideoCapture(0) # 0: default webcam
cap.set(3, 640) # id3: width
cap.set(4, 480) # id4: height
cap.set(10, 100) # id10: brightness


while True:
	success, img = cap.read() # success = Boolean
    cv2.imshow("Video", img)
    if cv2.waitKey(1) & 0xFF == ord('q'): # adds delay and user's key press
    	break

Ch2. Basic Functions

1. cvtColor

import cv2

img = cv2.imread("Resources/lena.png")

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Gray Image", imgGray)
cv2.waitKey(0)

 

2. GaussianBlur

import cv2

img = cv2.imread("Resources/lena.png")

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7,7), 0) # kernel should be odd numbers

cv2.imshow("Gray Image", imgGray)
cv2.imshow("Blur Image", imgBlur)
cv2.waitKey(0)

 

3. Edge Detector (Canny)

import cv2

img = cv2.imread("Resources/lena.png")

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7,7), 0) # kernel should be odd numbers
imgCanny = cv2.Canny(img, 150, 200) # thresholds


cv2.imshow("Gray Image", imgGray)
cv2.imshow("Blur Image", imgBlur)
cv2.imshow("Canny Image", imgCanny)
cv2.waitKey(0)

 

4. Dilation

: Increase the thickness of edges

import cv2
import numpy as np # because we need matrix for kernel

img = cv2.imread("Resources/lena.png")
kernel = np.ones((5, 5), np.uint8) # 0 ~ 255

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7,7), 0) # kernel should be odd numbers
imgCanny = cv2.Canny(img, 150, 200) # thresholds
imgDilation = cv2.dilate(imgCanny, kernel, iterations=1) # increase iteration => thicker edges

cv2.imshow("Gray Image", imgGray)
cv2.imshow("Blur Image", imgBlur)
cv2.imshow("Canny Image", imgCanny)
cv2.imshow("Dilation Image", imgDilation)
cv2.waitKey(0)

 

5. Erosion

: Make edges thinner

import cv2
import numpy as np # because we need matrix for kernel

img = cv2.imread("Resources/lena.png")
kernel = np.ones((5, 5), np.uint8) # 0 ~ 255

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7,7), 0) # kernel should be odd numbers
imgCanny = cv2.Canny(img, 150, 200) # thresholds
imgDilation = cv2.dilate(imgCanny, kernel, iterations=1) # increase iteration => thicker edges
imgEroded = cv2.erode(imgDilation, kernel, iterations=1)


cv2.imshow("Gray Image", imgGray)
cv2.imshow("Blur Image", imgBlur)
cv2.imshow("Canny Image", imgCanny)
cv2.imshow("Dilation Image", imgDilation)
cv2.imshow("Eroded Image", imgEroded)
cv2.waitKey(0)

Ch3. OpenCV conventions

x, y의 좌표가 일반적으로 아는 수학과 다르다

shape 도 HWC BGR임

resize하면 WHC 되고

cvtColor 메소드를 적용해서 BGR2RGB 하는 것이 권장됨

 


1. resize

import cv2
import numpy as np

img = cv2.imread("Resources/lambo.png")
print(img.shape) # (467, 623, 3)

imgResize = cv2.resize(img, (300,200))
print(imgResize.shape) # (300, 200, 3)

cv2.imshow("Image",img)
cv2.imshow("Image Resize",imgResize)

cv2.waitKey(0)

 

2. crop

import cv2
import numpy as np

img = cv2.imread("Resources/lambo.png")
print(img.shape) # (467, 623, 3) HWC BGR

imgResize = cv2.resize(img, (300,200)) # WH
print(imgResize.shape) # (300, 200, 3) WHC

imgCropped = img[0:200, 200:500] # HW  *** no need for cv2 ***

cv2.imshow("Image",img)
cv2.imshow("Image Resize",imgResize)
cv2.imshow("Image Cropped", imgCropped)

cv2.waitKey(0)

Ch4. Shapes and Texts

1. drawing line

import cv2
import numpy as np

# img = np.zeros((512,512), np.uint8) # Grayscale
img = np.zeros((512,512,3), np.uint8) # Color

img[:] = 255,0,0 # img[Height,Width] = Blue, Green, Red

cv2.line(img, (0,0), (300,300), (0,255,0), 3)
cv2.line(img,
	(0,0), # starting point W, H
    (img.shape[1],img.shape[0]), # end point (img.shape[1] is width)
    (0,255,0), # color
    3 # Thickness) 


cv2.imshow("Image", img)

cv2.waitKey(0)

 

2. drawing rectangle, circle, and text

import cv2
import numpy as np

# img = np.zeros((512,512), np.uint8) # Grayscale
img = np.zeros((512,512,3), np.uint8) # Color

img[:] = 255,0,0 # img[Height,Width] = Blue, Green, Red

cv2.line(img, (0,0), (300,300), (0,255,0), 3)
cv2.line(img,
	(0,0), # starting point W, H
    (img.shape[1],img.shape[0]), # end point (img.shape[1] is width)
    (0,255,0), # color
    3 # Thickness)
# cv2.rectangle(img, (0,0), (250,350), (0,0,255),2) # same convention as above
cv2.rectangle(img, (0,0), (250,350), (0,0,255),cv2.FILLED)
cv2.circle(img,(400,50),30,(255,255,0),5) # center point, radius, color, thickness
cv2.putText(img,"OPENCV",(300,200),cv2.FONT_HERSHEY_COMPLEX,1,(0,150,0),1)


cv2.imshow("Image", img)

cv2.waitKey(0)


Ch5. Warp Perspectives

: getting a bird-eye view

import cv2
import numpy as np

img = cv2.imshow("Resources/cards.jpg")

width,height = 250,350
pts1 = np.float32([[111,219],[287,188],[154,482],[352,440]]) # 4 points
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
imgOutput = cv2.warpPerspective(img,matrix,(width,height))

cv2.imshow("Image",img)
cv2.imshow("Output",imgOutput)
cv2.waitKey(0)

 


Ch6. Joining Images

import cv2
import numpy as np
img = cv2.imread('path/to/image.png')

# cannot reshape
# channels must be the same
imgHor = np.hstack((img, img))
imgVer = np.vstack((img, img))

cv2.imshow("horizontal", imgHor)
cv2.imshow("vertical", imgVer)

cv2.waitKey(0)
import cv2
import numpy as np


def stackImages(scale,imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        ver = hor
    return ver

img = cv2.imread('Resources/lena.png')
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

imgStack = stackImages(0.5,([img,imgGray,img],[img,img,img]))

cv2.imshow("ImageStack",imgStack)

cv2.waitKey(0)

OpenCV로 글자 컨투어 잡아내기

[1] https://m.blog.naver.com/monkey5255/221598376164

[2] c++ - Extracting text OpenCV - Stack Overflow

 

Extracting text OpenCV

I am trying to find the bounding boxes of text in an image and am currently using this approach: // calculate the local variances of the grayscale image Mat t_mean, t_mean_2; Mat grayF; outImg_gray.

stackoverflow.com

더보기
import cv2
import numpy as np

large = cv2.imread('1.jpg')
rgb = cv2.pyrDown(large)
small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel)

_, bw = cv2.threshold(grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
# using RETR_EXTERNAL instead of RETR_CCOMP
contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#For opencv 3+ comment the previous line and uncomment the following line
#_, contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

mask = np.zeros(bw.shape, dtype=np.uint8)

for idx in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[idx])
    mask[y:y+h, x:x+w] = 0
    cv2.drawContours(mask, contours, idx, (255, 255, 255), -1)
    r = float(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)

    if r > 0.45 and w > 8 and h > 8:
        cv2.rectangle(rgb, (x, y), (x+w-1, y+h-1), (0, 255, 0), 2)

cv2.imshow('rects', rgb)

'Coding > Image' 카테고리의 다른 글

[2] Selective Search  (0) 2021.04.12
[1] CNN Image Classifier to Object Detector  (0) 2021.04.12
matplot RGB vs opencv BGR vs caffe images  (0) 2021.04.09
Scene Text Recognition  (0) 2021.04.01
Object Detection (R-CNN)  (0) 2021.03.26

관련글 더보기