๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[v0.5]์์์ฒ๋ฆฌ_OpenCV_์ด๋ฏธ์ง ๋ฐ ๋์์ ์ฝ๊ธฐ ๋ณธ๋ฌธ
[v0.5]์์์ฒ๋ฆฌ_OpenCV_์ด๋ฏธ์ง ๋ฐ ๋์์ ์ฝ๊ธฐ
์ง์ง์ํ์นด 2021. 12. 27. 02:28211227 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ ๊ทํ์ด ์์ฌ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
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.')
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.')
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.')
+) ๋์์ ํ์ผ ์ฝ๊ธฐ
# ๋์์ ํ์ผ ์ฝ๊ธฐ
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()
+) ๋ ธํธ๋ถ ์บ ์ฝ๊ธฐ
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()
+) ๋น๋์ค ์ ์ฅํ๊ธฐ (์น์บ )
cv2.VideoCapture() (int or str)
: VideoCapture Object๋ฅผ ์์ฑํ๋ ํจ์
cv2.VideoWriter(filename, fourcc, fps, frameSize, isColor) (str, int, double, Size, bool)
: 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