๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[v0.5]์˜์ƒ์ฒ˜๋ฆฌ_OpenCV_์ด๋ฏธ์ง€ ๋ฐ ๋™์˜์ƒ ์ฝ๊ธฐ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Image Processing

[v0.5]์˜์ƒ์ฒ˜๋ฆฌ_OpenCV_์ด๋ฏธ์ง€ ๋ฐ ๋™์˜์ƒ ์ฝ๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2021. 12. 27. 02:28
728x90
๋ฐ˜์‘ํ˜•

211227 ์ž‘์„ฑ

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ๊ท€ํ‰์ด ์„œ์žฌ๋‹˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://bkshin.tistory.com/entry/OpenCV-3-%EC%9D%B4%EB%AF%B8%EC%A7%80-%EC%9E%85%EC%B6%9C%EB%A0%A5?category=1148027 

 

OpenCV - 3. ์ด๋ฏธ์ง€ ๋ฐ ๋™์˜์ƒ ์ฝ๊ธฐ

OpenCV๋ฅผ ํ™œ์šฉํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์€ ์ด๋ฏธ์ง€๋ฅผ ์ฝ์–ด์„œ ์ ์ ˆํ•œ ์—ฐ์‚ฐ์„ ์ ์šฉํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” OpenCV์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ ์ด๋ฏธ์ง€ ์ž…์ถœ๋ ฅ์— ๋Œ€ํ•ด

bkshin.tistory.com

 

 

 

 

1. ์ด๋ฏธ์ง€ ์ฝ๊ธฐ

cv2.imread(path, flag)

  • path : ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ
  • flag : ์ด๋ฏธ์ง€๋ฅผ ์–ด๋–ป๊ฒŒ ์ฝ์„์ง€ ๋ฐฉ์‹ ์„ค์ •
    cv2.IMREAD_COLOR : ์ƒ‰๊น” ์ด๋ฏธ์ง€๋กœ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์ด๋•Œ ํˆฌ๋ช…๋„(alpha๊ฐ’)๋Š” ๋ฌด์‹œ,  3 ์ฑ„๋„, BGR ์ด๋ฏธ์ง€ ์‚ฌ์šฉ
    cv2.IMREAD_GRAYSCLE : ์ด๋ฏธ์ง€๋ฅผ ํ‘๋ฐฑํ†ค์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ,  1 ์ฑ„๋„, ๊ทธ๋ ˆ์ด์Šค์ผ€์ผ ์ ์šฉ
    cv2.IMREAD_UNCHANGED : ํˆฌ๋ช…๋„(alpha๊ฐ’)๋ฅผ ํฌํ•จํ•ด ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ, ์›๋ณธ ์ถœ๋ ฅ
import cv2

# ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ
img_file = "img/sample.jpeg"
# ์ด๋ฏธ์ง€ ๋ณ€์ˆ˜ ํ• ๋‹น
img = cv2.imread(img_file)

# ์ด๋ฏธ์ง€ ํ™”๋ฉด์— ํ‘œ์‹œ
cv2.imshow("IMG", img)
# ํ‚ค๊ฐ€ ์ž…๋ ฅ์ด ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ
cv2.waitKey()
# ์ฐฝ ๋ชจ๋‘ ๋‹ซ๊ธฐ
cv2.destroyAllWindows()

 

์ด๋ฏธ์ง€ ์ฝ๊ธฐ

+) ์ฝ”๋žฉ์œผ๋กœ ํ•  ๋•Œ๋Š” cv2_imshow(img) ๋กœ ํ•œ๋‹ค

import cv2
from google.colab.patches import cv2_imshow

# ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ
img_file = "/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/sample.jpeg"
# ์ด๋ฏธ์ง€ ๋ณ€์ˆ˜ ํ• ๋‹น
img = cv2.imread(img_file)

# ์ด๋ฏธ์ง€ ํ™”๋ฉด์— ํ‘œ์‹œ
cv2_imshow(img)
# ํ‚ค๊ฐ€ ์ž…๋ ฅ์ด ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ
cv2.waitKey()
# ์ฐฝ ๋ชจ๋‘ ๋‹ซ๊ธฐ
cv2.destroyAllWindows()

 

1) ์ƒ‰๊น” ์ด๋ฏธ์ง€๋กœ ์ถœ๋ ฅ

# ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ƒ‰๊น” ์ด๋ฏธ์ง€๋กœ ํ™”๋ฉด์— ํ‘œ์‹œ

import cv2
from google.colab.patches import cv2_imshow

img_file = "/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/sample.jpeg"
img = cv2.imread(img_file, cv2.IMREAD_COLOR)

if img is not None:
  cv2_imshow(img)
  cv2.waitKey()
  cv2.destroyAllWindows()
else:
    print('No image file.')

cv2.IMREAD_COLOR(๊ธฐ๋ณธ๊ฐ’)

 

2) ํ‘๋ฐฑ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ

# ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ํšŒ์ƒ‰์œผ๋กœ ํ™”๋ฉด์— ํ‘œ์‹œ

import cv2
from google.colab.patches import cv2_imshow

img_file = "/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/sample.jpeg"
img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)    # ํšŒ์ƒ‰์œผ๋กœ ์ฝ๊ธฐ

if img is not None:
  cv2_imshow(img)
  cv2.waitKey()
  cv2.destroyAllWindows()
else:
    print('No image file.')

cv2.IMREAD_GRAYSCALE

 

3) ํˆฌ๋ช…๋„ ํฌํ•จ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ

# ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ํˆฌ๋ช…๋„ ํฌํ•จํ•ด ๊ทธ๋Œ€๋กœ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ

import cv2
from google.colab.patches import cv2_imshow

img_file = "/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/sample.jpeg"
img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED)

if img is not None:
  cv2_imshow(img)
  cv2.waitKey()
  cv2.destroyAllWindows()
else:
    print('No image file.')

cv2.IMREAD_UNCHANGED

 

 

+) ๋™์˜์ƒ ํŒŒ์ผ ์ฝ๊ธฐ

# ๋™์˜์ƒ ํŒŒ์ผ ์ฝ๊ธฐ

import cv2
from google.colab.patches import cv2_imshow

video_file = "/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/แ„€แ…ฉแ„‹แ…ฃแ†ผแ„‹แ…ต.mp4" # ๋™์˜์ƒ ํŒŒ์ผ ๊ฒฝ๋กœ

cap = cv2.VideoCapture(video_file) # ๋™์˜์ƒ ์บก์ณ ๊ฐ์ฒด ์ƒ์„ฑ  ---โ‘ 
if cap.isOpened():                 # ์บก์ณ ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ํ™•์ธ
    while True:
        ret, img = cap.read()      # ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์ฝ๊ธฐ      --- โ‘ก
        if ret:                     # ํ”„๋ ˆ์ž„ ์ฝ๊ธฐ ์ •์ƒ
            cv2_imshow(img) # ํ™”๋ฉด์— ํ‘œ์‹œ  --- โ‘ข
            cv2.waitKey(5)            # 5ms ์ง€์—ฐ  --- โ‘ฃ
        else:                       # ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์ฝ์„ ์ˆ˜ ์—†์Œ
            break                   # ์žฌ์ƒ ์™„๋ฃŒ
else:
    print("can't open video.")      # ์บก์ณ ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์‹คํŒจ
cap.release()                       # ์บก์ณ ์ž์› ๋ฐ˜๋‚ฉ
cv2.destroyAllWindows()

๊ณ ์–‘์ด.mp4

 

+) ๋…ธํŠธ๋ถ ์บ  ์ฝ๊ธฐ

import cv2

cap = cv2.VideoCapture(0)               # 0๋ฒˆ ์นด๋ฉ”๋ผ ์žฅ์น˜ ์—ฐ๊ฒฐ ---โ‘ 
if cap.isOpened():                      # ์บก์ณ ๊ฐ์ฒด ์—ฐ๊ฒฐ ํ™•์ธ
    while True:
        ret, img = cap.read()           # ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์ฝ๊ธฐ
        if ret:
            cv2.imshow('video',img)             # ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์ด๋ฏธ์ง€ ํ‘œ์‹œ
            if cv2.waitKey(1) != -1:    # 1ms ๋™์•ˆ ํ‚ค ์ž…๋ ฅ ๋Œ€๊ธฐ ---โ‘ก
                break                   # ์•„๋ฌด ํ‚ค๋ผ๋„ ์ž…๋ ฅ์ด ์žˆ์œผ๋ฉด ์ค‘์ง€
        else:
            print('no frame')
            break
else:
    print("can't open camera.")
cap.release()                           # ์ž์› ๋ฐ˜๋‚ฉ
cv2.destroyAllWindows()

 

๋‚ด ๋…ธํŠธ๋ถ ์บ 

 

 

2. ์ด๋ฏธ์ง€ ์ €์žฅ

cv2.imwirte(path, img, params)

  • filename : ์ €์žฅํ•  ์˜์ƒ ํŒŒ์ผ ์ด๋ฆ„
  • img : ์ €์žฅํ•  ์˜์ƒ ๋ฐ์ดํ„ฐ
  • params : ํŒŒ์ผ ์ €์žฅ ์˜ต์…˜ ์ง€์ •
# ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ (img_write.py)

import cv2

img_file = '/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/house.jpg'
save_file = '/content/drive/MyDrive/แ„€แ…กแ„‹แ…ณแ†ซ/2022/img/house_gray.jpg'

img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)
cv2_imshow(img)
cv2.imwrite(save_file, img) #ํŒŒ์ผ๋กœ ์ €์žฅ, ํฌ๋งท์€ ํ™•์žฅ์— ๋”ฐ๋ฆ„
cv2.waitKey()
cv2.destroyAllWindows()

house_gray.jpg

 

 

+) ๋น„๋””์˜ค ์ €์žฅํ•˜๊ธฐ (์›น์บ )

cv2.VideoCapture() (int or str)

: VideoCapture Object๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜

cv2.VideoWriter(filename, fourcc, fps, frameSize, isColor) (strintdoubleSizebool)

: VideoWriter Object๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜

- ์ž…๋ ฅ ๋ณ€์ˆ˜๋Š” ๊ฐ๊ฐ ์ €์žฅํ•  ํŒŒ์ผ๋ช…(filename), codec ์ข…๋ฅ˜(fourcc), ์ดˆ๋‹น ํ”„๋ ˆ์ž„(frame per sec)(fps), ๋™์˜์ƒ ํ”„๋ ˆ์ž„์˜ ํฌ๊ธฐ(frameSize), ์ปฌ๋Ÿฌ๋กœ ์ฝ์„ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋ถˆ ๋ณ€์ˆ˜(isColor)๋ฅผ ์˜๋ฏธ

cv2.VideoWriter_fourcc(c1, c2, c3, c4) (char, char, char, char)

: Video๋ฅผ ์ €์žฅํ•  codec์˜ ์ข…๋ฅ˜๋ฅผ ์ •ํ•ด์คŒ

import cv2

cap = cv2.VideoCapture(0)  # 0์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ๋…ธํŠธ๋ถ์— ๋‚ด์žฅ๋œ ์นด๋ฉ”๋ผ์˜ ๊ณ ์œ  ์ง€์ •๋ฒˆํ˜ธ

width = int(cap.get(3)) # ๊ฐ€๋กœ ๊ธธ์ด ๊ฐ€์ ธ์˜ค๊ธฐ 
height = int(cap.get(4)) # ์„ธ๋กœ ๊ธธ์ด ๊ฐ€์ ธ์˜ค๊ธฐ
fps = 20

fcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
out = cv2.VideoWriter('webcam_record.avi', fcc, fps, (width, height), isColor=False)
print(out.isOpened())
while (True) :
    ret, frame = cap.read()
    if ret :
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        out.write(gray)
        cv2.imshow('frame', gray)

        if cv2.waitKey(1) & 0xFF == ord('q') : break
    else :
        print("Fail to read frame!")
        break

cap.release()
out.release()
cv2.destroyAllWindows()

๋น„๋””์˜ค ์ €์žฅ

 

<์œ„๋Š” everyday image processing๋‹˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://everyday-image-processing.tistory.com/162?category=972721

728x90
๋ฐ˜์‘ํ˜•
Comments