반응형
배열 병합
import cv2
src = cv2.imread("Image/tomato.jpg", cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
lower_red = cv2.inRange(hsv, (0, 100, 100), (5, 255, 255))
upper_red = cv2.inRange(hsv, (170, 100, 100), (180, 255, 255))
added_red = cv2.addWeighted(lower_red, 1.0, upper_red, 1.0, 0.0)
red = cv2.bitwise_and(hsv, hsv, mask = added_red)
red = cv2.cvtColor(red, cv2.COLOR_HSV2BGR)
cv2.imshow("red", red)
cv2.waitKey()
cv2.destroyAllWindows()
채널 분리 혹은 병합
import cv2
src = cv2.imread("Image/tomato.jpg", cv2.IMREAD_COLOR)
b, g, r = cv2.split(src)
inverse = cv2.merge((r, g, b))
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
cv2.imshow("inverse", inverse)
cv2.waitKey()
cv2.destroyAllWindows()
도형 그리기
import cv2
import numpy as np
src = np.zeros((768, 1366, 3), dtype=np.uint8)
src = cv2.line(src, (100, 100), (1200, 100), (0, 0, 255), 3, cv2.LINE_AA)
src = cv2.circle(src, (300, 300), 50, (0, 255, 0), cv2.FILLED, cv2.LINE_4)
src = cv2.rectangle(src, (500, 200), (1000, 400), (255, 0, 0), 5, cv2.LINE_8)
src = cv2.ellipse(src, (1200, 300), (100, 50), 0, 90, 180, (255, 255, 0), 2)
pts1 = np.array([[100, 500], [300, 500], [200, 600]])
pts2 = np.array([[600, 500], [800, 500], [700, 600]])
src = cv2.polylines(src, [pts1], True, (0, 255, 255), 2)
src = cv2.fillPoly(src, [pts2], (255, 0, 255), cv2.LINE_AA)
src = cv2.putText(src, "CARELAB", (900, 600), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255), 3)
cv2.imshow("src", src)
cv2.waitKey()
cv2.destroyAllWindows()
기하학적 변환
import cv2
import numpy as np
src = cv2.imread("Image/harvest.jpg", cv2.IMREAD_COLOR)
height, width, channel = src.shape
srcPoint = np.array([[300, 200], [400, 200], [500, 500], [200, 500]], dtype=np.float32)
dstPoint = np.array([[0, 0], [width, 0], [width, height], [0, height]], dtype=np.float32)
matrix = cv2.getPerspectiveTransform(srcPoint, dstPoint)
dst = cv2.warpPerspective(src, matrix, (width, height))
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
영상 캡쳐와 녹화
import datetime
import cv2
capture = cv2.VideoCapture("/Image/Star.mp4")
fourcc = cv2.VideoWriter_fourcc(*'XVID')
record = False
while True:
if(capture.get(cv2.CAP_PROP_POS_FRAMES) == capture.get(cv2.CAP_PROP_FRAME_COUNT)):
capture.open("/Image/Star.mp4")
ret, frame = capture.read()
cv2.imshow("VideoFrame", frame)
now = datetime.datetime.now().strftime("%d_%H-%M-%S")
key = cv2.waitKey(33)
if key == 27:
break
elif key == 26:
print("캡쳐")
cv2.imwrite("D:/" + str(now) + ".png", frame)
elif key == 24
print("녹화 시작")
record = True
video = cv2.VideoWriter("D:/" + str(now) + ".avi", fourcc, 20.0, (frame.shape[1], frame.shape[0]))
elif key == 3
print("녹화 중지")
record = False
video.release()
if record == True:
print("녹화 중..")
video.write(frame)
capture.release()
cv2.destroyAllWindows()
윤곽선 검출
import cv2
src = cv2.imread("Image/contours.png", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
binary = cv2.bitwise_not(binary)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
for i in range(len(contours)):
cv2.drawContours(src, [contours[i]], 0, (0, 0, 255), 2)
cv2.putText(src, str(i), tuple(contours[i][0][0]), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 1)
print(i, hierarchy[0][i])
cv2.imshow("src", src)
cv2.waitKey(0)
cv2.destroyAllWindows()
다각형 근사
import cv2
src = cv2.imread("Image/car.png", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
binary = cv2.bitwise_not(binary)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_KCOS)
for contour in contours:
epsilon = cv2.arcLength(contour, True) * 0.02
approx_poly = cv2.approxPolyDP(contour, epsilon, True)
for approx in approx_poly:
cv2.circle(src, tuple(approx[0]), 3, (255, 0, 0), -1)
cv2.imshow("src", src)
cv2.waitKey(0)
cv2.destroyAllWindows()
코너 검출
import cv2
src = cv2.imread("Image/coffee.jpg")
dst = src.copy()
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 5, blockSize=3, useHarrisDetector=True, k=0.03)
for i in corners:
cv2.circle(dst, tuple(i[0]), 3, (0, 0, 255), 2)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
블록 껍질
import cv2
src = cv2.imread("Image/convex.png")
dst = src.copy()
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
for i in contours:
hull = cv2.convexHull(i, clockwise=True)
cv2.drawContours(dst, [hull], 0, (0, 0, 255), 2)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
모멘트
import cv2
src = cv2.imread("Image/convex.png")
dst = src.copy()
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
for i in contours:
M = cv2.moments(i)
cX = int(M['m10'] / M['m00'])
cY = int(M['m01'] / M['m00'])
cv2.circle(dst, (cX, cY), 3, (255, 0, 0), -1)
cv2.drawContours(dst, [i], 0, (0, 0, 255), 2)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
반응형
'OpenCV' 카테고리의 다른 글
OpenCV Example 36강 (0) | 2024.06.27 |
---|---|
OpenCV Python 고급 활용 강좌 소스 코드 1 (1) | 2023.12.05 |
OpenCV Python 고급 활용 강좌 소스 코드 4 (0) | 2023.12.05 |
OpenCV Python 고급 활용 강좌 소스 코드 3 (0) | 2023.12.05 |
OpenCV 파이선 강의 실습 코드 4 (2) | 2023.11.29 |
OpenCV 파이선 강의 실습 코드 3 (1) | 2023.11.22 |
OpenCV 파이선 강의 실습 코드 2 (2) | 2023.11.22 |
OpenCV 파이선 강의 실습 코드 1 (1) | 2023.11.22 |
더욱 좋은 정보를 제공하겠습니다.~ ^^