어제에 이어서 OpenCV의 기본적인 사용 방법들에 대해서 공부했다. 이미지의 색을 변경하고 여러 도형을 그리는 등 NumPy를 사용해서 이미지의 데이터를 변경하는 과정을 계속 배우는 중이다.
img = np.zeros((460, 640, 3), dtype=np.uint8)
# 일부 색 변경
img[200:300, 300:400] = (0,0,255)
# 전체 하얀색으로 변경
img[:] = (255,255,255)
cv.imshow("Color", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
DOG_PATH = "../images/dog.jpg"
img = cv.imread(DOG_PATH)
img[200:300, 300:400] = (0,0,255)
cv.imshow("Photo", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
img = cv.imread(DOG_PATH)
crop = img[70:320, 300:450].copy()
img[0:250, 0:150] = crop
cv.imshow("img", img)
cv.imshow("Cropped", crop)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
img = np.zeros((460, 640, 3), dtype=np.uint8)
# 얕은 복사
shallow_copy = img[200:300, 300:400]
shallow_copy[:] = (255,0,255)
# 깊은 복사
deep_copy = img[200:300, 300:400].copy()
deep_copy[:] = (255,255,0)
cv.imshow("original", img)
cv.imshow("shalliw copy", shallow_copy)
cv.imshow("deep copy", deep_copy)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.cvtColor(img, code)img = cv.imread(DOG_PATH)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.imshow("img", img)
cv.imshow("gray", gray)
cv.imshow("rgb", rgb)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
img = cv.imread(DOG_PATH)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
inverted = 255 - gray
cv.imshow("gray", gray)
cv.imshow("inverted", inverted)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.split(img) : 이미지 채널 분리cv2.merge(b,g,r) : 이미지 채널 병합# 이미지 채널 분리
img = cv.imread(DOG_PATH)
b, g, r = cv.split(img)
cv.imshow("Blue", b)
cv.imshow("Green", g)
cv.imshow("Red", r)
# 이미지 채널 병합
merged = cv.merge([b,g,r])
cv.imshow("Merged", merged)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.resize(img, (size), fx, fy, interpolation)img = cv.imread(DOG_PATH)
dst = cv.resize(img, (1280, 800))
near = cv.resize(img, None, fx=3, fy=3, interpolation=cv.INTER_NEAREST)
cubic = cv.resize(img, None, fx=3, fy=3, interpolation=cv.INTER_CUBIC)
cv.imshow("original", img)
cv.imshow("resized", dst)
cv.imshow("near", near)
cv.imshow("cubic", cubic)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
DOG_VIDEO_PATH = "../videos/dog.mp4"
cap = cv.VideoCapture(DOG_VIDEO_PATH)
fps = cap.get(cv.CAP_PROP_FPS)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
dst = cv.resize(frame, None, fx=1.5, fy=1.5, interpolation=cv.INTER_CUBIC)
cv.imshow("Original Video", frame)
cv.imshow("Resized Video", dst)
if cv.waitKey(int(1000/fps)) == ord("q"):
break
cap.release()
cv.destroyAllWindows()
cv.waitKey(1)
cv2.pyrUp(img) : 이미지를 2배 키움cv2.pyrDown(img) : 이미지를 1/2로 줄임img = cv.imread(DOG_PATH)
size_up = cv.pyrUp(img)
size_down = cv.pyrDown(img)
cv.imshow("original", img)
cv.imshow("Up", size_up)
cv.imshow("Down", size_down)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.flip(img, code)img = cv.imread(DOG_PATH)
flip_y = cv.flip(img, 1)
flip_x = cv.flip(img, 0)
flip_xy = cv.flip(img, -1)
cv.imshow("Original", img)
cv.imshow("Flip y", flip_y)
cv.imshow("Flip x", flip_x)
cv.imshow("Flip xy", flip_xy)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.line(img, start, end, color, thickness, lineType)img = np.zeros((460, 640, 3), dtype=np.uint8)
COLOR = (0,255,255)
THICKNESS = 5
cv.line(img, (100,100), (300,300), COLOR, THICKNESS, cv.LINE_4)
cv.line(img, (150,100), (350,300), COLOR, THICKNESS, cv.LINE_8)
cv.line(img, (200,100), (400,300), COLOR, THICKNESS, cv.LINE_AA)
cv.imshow("Line", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.circle(img, center, radius, color, thickness, lineType)cv2.FILLED : 도형의 속을 채우는 옵션, thickness 위치에 적용img = np.zeros((460, 640, 3), dtype=np.uint8)
RADIUS = 50
COLOR = (255,0,0)
THICKNESS = 10
# 속이 비어있는 원
cv.circle(img, (200, 200), RADIUS, COLOR, THICKNESS, cv.LINE_AA)
# 속이 채워진 원
cv.circle(img, (400, 200), RADIUS, COLOR, cv.FILLED, cv.LINE_AA)
cv.imshow("Circle", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType)img = np.zeros((460, 640, 3), dtype=np.uint8)
COLOR = (255,0,0)
THICKNESS = 10
cv.ellipse(img, (320, 230), (100, 50), 30, 0, 270, COLOR, THICKNESS, cv.LINE_AA)
cv.imshow("Ellipse", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.rectangle(img, start, end, color, thickness, lineType)img = np.zeros((460, 640, 3), dtype=np.uint8)
COLOR = (255,0,0)
THICKNESS = 5
# 속이 비어있는 사각형
cv.rectangle(img, (150, 100), (250, 200), COLOR, THICKNESS, cv.LINE_AA)
# 속이 채워진 사각형
cv.rectangle(img, (300, 100), (400, 200), COLOR, cv.FILLED, cv.LINE_AA)
cv.imshow("Rectangle", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.polylines(img, pts, isClosed, color, thickness, lineType)img = np.zeros((460, 640, 3), dtype=np.uint8)
pts = np.array([[[100,100], [200,100], [100,200]]])
COLOR = (255,255,255)
THICKNESS = 10
cv.polylines(img, pts, True, COLOR, THICKNESS)
cv.imshow("Poltlines", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.fillpoly(img, pts, color)img = np.zeros((460, 640, 3), dtype=np.uint8)
pts = np.array([[ [200,200], [300,200], [200,300] ]])
COLOR = (255,255,255)
cv.fillPoly(img, pts, COLOR)
cv.imshow("FillPoly", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
cv2.putText(img, text, position, font, scale, color, thickness)img = np.zeros((460, 640, 3), dtype=np.uint8)
SCALE = 1
THICKNESS = 2
COLOR = (255,255,255)
cv.putText(img, "Hello World!", (220,200), cv.FONT_HERSHEY_DUPLEX, SCALE, COLOR, THICKNESS)
cv.putText(img, "OpenCV Study", (200,260), cv.FONT_HERSHEY_COMPLEX | cv.FONT_ITALIC, SCALE, COLOR, THICKNESS)
cv.imshow("Text", img)
cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)
아직까지는 기능을 익히는 과정이라 어려운 건 없었는데, 내일부터 본격적으로 어려운 내용들을 배운다고 한다. OpenCV에 NumPy가 상당히 많이 사용되는 것 같아서 NumPy도 계속 복습하면서 잘 배워가야 할 것 같다.