반응형
모폴로지 변환
import numpy as np
import cv2
src = cv2.imread('zebra.jpg')
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (9, 9))
dilate = cv2.dilate(src, kernel, anchor=(-1, -1), iterations=5)
erode = cv2.erode(src, kernel, anchor=(-1, -1), iterations=5)
dst = np.concatenate((src, dilate, erode), axis=1)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
모폴로지 연산
import numpy as np
import cv2
src = cv2.imread('office.jpg')
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (9, 9))
dst = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel, iterations=9)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
직선 검출
import numpy as np
import cv2
src = cv2.imread("road.jpg")
dst = src.copy()
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 5000, 1500, apertureSize = 5, L2gradient = True)
lines = cv2.HoughLines(canny, 0.8, np.pi / 180, 150, srn = 100, stn = 200, min_theta = 0, max_theta = np.pi)
for i in lines:
rho, theta = i[0][0], i[0][1]
a, b = np.cos(theta), np.sin(theta)
x0, y0 = a*rho, b*rho
scale = src.shape[0] + src.shape[1]
x1 = int(x0 + scale * -b)
y1 = int(y0 + scale * a)
x2 = int(x0 - scale * -b)
y2 = int(y0 - scale * a)
cv2.line(dst, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.circle(dst, (x0, y0), 3, (255, 0, 0), 5, cv2.FILLED)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
원 검출
import cv2
src = cv2.imread("colorball.jpg")
dst = src.copy()
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1 = 250, param2 = 10, minRadius = 80, maxRadius = 120)
for i in circles[0]:
cv2.circle(dst, (i[0], i[1]), i[2], (255, 255, 255), 5)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
이미지 연산 1
import numpy as np
import cv2
src = cv2.imread("pencils.jpg")
number1 = np.ones_like(src) * 127
number2 = np.ones_like(src) * 2
add = cv2.add(src, number1)
sub = cv2.subtract(src, number1)
mul = cv2.multiply(src, number2)
div = cv2.divide(src, number2)
src = np.concatenate((src, src, src, src), axis = 1)
number = np.concatenate((number1, number1, number2, number2), axis = 1)
dst = np.concatenate((add, sub, mul, div), axis = 1)
dst = np.concatenate((src, number, dst), axis = 0)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
이미지 연산 2
import numpy as np
import cv2
src = cv2.imread("pencils.jpg")
number = np.ones_like(src) * 127
_max = cv2.max(src, number)
_min = cv2.min(src, number)
_abs = cv2.absdiff(src, number)
compare = cv2.compare(src, number, cv2.CMP_GT)
src = np.concatenate((src, src, src, src), axis = 1)
number = np.concatenate((number, number, number, number), axis = 1)
dst = np.concatenate((_max, _min, _abs, compare), axis = 1)
dst = np.concatenate((src, number, dst), axis = 0)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
비트 연산
import numpy as np
import cv2
src = cv2.imread("analysis.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
_and = cv2.bitwise_and(gray, binary)
_or = cv2.bitwise_or(gray, binary)
_xor = cv2.bitwise_xor(gray, binary)
_not = cv2.bitwise_not(gray)
src = np.concatenate((np.zeros_like(gray), gray, binary, np.zeros_like(gray)), axis = 1)
dst = np.concatenate((_and, _or, _xor, _not), axis = 1)
dst = np.concatenate((src, dst), axis = 0)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
히스토그램
import cv2
import numpy as np
src = cv2.imread("road.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
result = np.zeros((src.shape[0], 256), dtype=np.uint8)
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
cv2.normalize(hist, hist, 0, result.shape[0], cv2.NORM_MINMAX)
for x, y in enumerate(hist):
cv2.line(result, (int(x), result.shape[0]), (int(x), result.shape[0] - int(y)), 255)
dst = np.hstack([gray, result])
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
픽셀 접근
import cv2
import numpy as np
gray = np.linspace(0, 255, num=90000, endpoint=True, retstep=False, dtype=np.uint8).reshape(300, 300, 1)
color = np.zeros((300, 300, 3), np.uint8)
color[0:150, :, 0] = gray[0:150, :, 0]
color[:, 150:300, 2] = gray[:, 150:300, 0]
x, y, c = 200, 100, 0
access_gray = gray[y, x, c]
access_color_blue = color[y, x, c]
access_color = color[y, x]
print(access_gray)
print(access_color_blue)
print(access_color)
cv2.imshow("gray", gray)
cv2.imshow("color", color)
cv2.waitKey(0)
cv2.destroyAllWindows()
트랙바
import cv2
def onChange(pos):
pass
src = cv2.imread("cherryblossom.jpg", cv2.IMREAD_GRAYSCALE)
cv2.namedWindow("Trackbar Windows")
cv2.createTrackbar("threshold", "Trackbar Windows", 0, 255, onChange)
cv2.createTrackbar("maxValue", "Trackbar Windows", 0, 255, lambda x : x)
cv2.setTrackbarPos("threshold", "Trackbar Windows", 127)
cv2.setTrackbarPos("maxValue", "Trackbar Windows", 255)
while cv2.waitKey(1) != ord('q'):
thresh = cv2.getTrackbarPos("threshold", "Trackbar Windows")
maxval = cv2.getTrackbarPos("maxValue", "Trackbar Windows")
_, binary = cv2.threshold(src, thresh, maxval, cv2.THRESH_BINARY)
cv2.imshow("Trackbar Windows", binary)
cv2.destroyAllWindows()
반응형
'OpenCV' 카테고리의 다른 글
컴퓨터 비전(Computer Vision) - 이수안컴퓨터연구소 (14강, 동영상) (1) | 2024.06.28 |
---|---|
OpenCV Example 36강 (0) | 2024.06.27 |
OpenCV Python 고급 활용 강좌 소스 코드 1 (1) | 2023.12.05 |
OpenCV Python 고급 활용 강좌 소스 코드 4 (0) | 2023.12.05 |
OpenCV Python 고급 활용 강좌 소스 코드 2 (0) | 2023.12.05 |
OpenCV 파이선 강의 실습 코드 4 (2) | 2023.11.29 |
OpenCV 파이선 강의 실습 코드 3 (1) | 2023.11.22 |
OpenCV 파이선 강의 실습 코드 2 (2) | 2023.11.22 |
더욱 좋은 정보를 제공하겠습니다.~ ^^