import cv2 import numpy as np import time
# 큰 이미지를 사용해야 GPU를 사용한 효과를 확인할 수 있네요. height, width = 4096, 4096 image = np.zeros((height, width, 3), dtype=np.uint8)
# 허브 변환으로 검출하기위해 임의의 직선을 그려줍니다. cv2.line(image, (0, 0), (width, height), (255, 255, 255), 10) cv2.line(image, (width, 0), (0, height), (255, 255, 255), 10)
# 그레이 스케일로 변환합니다. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 캐니 에지 디텍터로 에지를 검출합니다. edges = cv2.Canny(gray_image, 50, 150, apertureSize=3)
# CPU에서 허프 변환 적용합니다. start_cpu = time.time() lines_cpu = cv2.HoughLines(edges, 1, np.pi / 180, 200) end_cpu = time.time() print(f"CPU 허프 변환 시간 : {end_cpu - start_cpu} seconds")
# CUDA가 가능한지 확인합니다. if not cv2.cuda.getCudaEnabledDeviceCount(): print("CUDA가 활성화된 장치가 없습니다.") else: # 이미지를 GPU 메모리로 업로드합니다. gpu_image = cv2.cuda_GpuMat() gpu_image.upload(gray_image)
# CUDA를 사용한 캐니 에지 디텍터로 에지를 검출합니다. gpu_edges = cv2.cuda.createCannyEdgeDetector(50, 150, 3) gpu_edge_output = gpu_edges.detect(gpu_image)
# GPU에서 허프 변환 적용합니다. start_gpu = time.time() hough_detector = cv2.cuda.createHoughSegmentDetector(1, np.pi / 180, 200, 10) result_gpu = hough_detector.detect(gpu_edge_output) end_gpu = time.time() print(f"GPU 허프 변환 시간 : {end_gpu - start_gpu} seconds")
# # 이미지에서 결과 보는것은 생략합니다. # result_image = image.copy() # if result_gpu is not None and not result_gpu.empty(): # lines = result_gpu.download() # for line in lines: # x1, y1, x2, y2 = line[0][0], line[0][1], line[1][0], line[1][1] # cv2.line(result_image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
# cv2.imshow('GPU 허프 변환 결과', result_image) # cv2.waitKey(0) # cv2.destroyAllWindows() |
Member discussion