주요 콘텐츠

GPU에서 houghlines 함수를 사용한 차선 검출

이 예제는 영상에서 차선 마커 경계를 검출하여 출력하는 MATLAB® 함수에 대한 CUDA® MEX를 생성하는 방법을 보여줍니다. 이 예제에서는 RGB 영상을 입력값으로 받고 Image Processing Toolbox™의 ordfilt2 (Image Processing Toolbox), hough (Image Processing Toolbox), houghpeaks (Image Processing Toolbox), houghlines (Image Processing Toolbox) 함수를 사용하여 차선이 검출된 출력 영상을 생성합니다.

타사 선행 조건

필수

이 예제는 CUDA MEX를 생성하며, 다음과 같은 타사 요구 사항이 있습니다.

  • CUDA 지원 NVIDIA® GPU 및 호환되는 드라이버.

선택 사항

정적, 동적 라이브러리 또는 실행 파일과 같은 비 MEX 빌드의 경우, 이 예제에는 다음과 같은 추가 요구 사항이 있습니다.

GPU 환경 확인하기

coder.checkGpuInstall 함수를 사용하여 이 예제를 실행하는 데 필요한 컴파일러와 라이브러리가 올바르게 설치되었는지 확인합니다.

envCfg = coder.gpuEnvConfig('host');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

lane_detection_houghlines 진입점 함수

lane_detection_houghlines.m 진입점 함수는 명암 영상을 입력값으로 받아 차선이 검출된 영상을 반환합니다.

type lane_detection_houghlines
function [lines] = lane_detection_houghlines(inputImage)%#codegen

%  Copyright 2019-2021 The MathWorks, Inc.
coder.gpu.kernelfun;

% Convert RGB image to grayscale image.
grayImage = im2gray(inputImage);

% Edge detection using ordfilt2.
input = grayImage(240:end,1:end);
dom = ones(2);
minOrder = 1;
maxOrder = 4;
padopt = 'zeros';

MinImg = ordfilt2(input,minOrder,dom,padopt);
MaxImg = ordfilt2(input,maxOrder,dom,padopt);

% Edge detected output.
outImage = MaxImg - MinImg;
BW = imbinarize(outImage);

[H,T,R] = hough(BW);
P  = houghpeaks(H,20,'threshold',1);
lines = houghlines(BW,T,R,P,'FillGap',200,'MinLength',150);

lane_detection_houghlines 함수에 대한 CUDA MEX 생성하기

GPU 코드 구성 객체를 만들고 codegen 함수를 실행합니다.

inputImage = imread('highway.png');
inputResizedImage = imresize(inputImage,[480 640]);
cfg = coder.gpuConfig('mex');
codegen -args {inputResizedImage} -config cfg lane_detection_houghlines
Code generation successful: View report

생성된 CUDA MEX 실행하기

생성된 lane_detection_houghlines_mex를 입력 영상과 함께 실행하고, 입력 영상과 차선 검출 영상을 플로팅합니다.

[lines] = lane_detection_houghlines_mex(inputResizedImage);

% Plot images.
inputImageVGAsize = imresize(inputImage,[480 640]);
outputImage = imresize(inputImage,[480 640]);
p1  = subplot(1, 2, 1);
p2 = subplot(1, 2, 2);
imshow(inputImageVGAsize, 'Parent', p1);
imshow(outputImage, 'Parent', p2);hold on
max_len = 0;
for k = 1:length(lines)
    if ((lines(k).theta <= 60 && lines(k).theta >10)||...
            (lines(k).theta <= -10 && lines(k).theta > -50) )
        xy = [lines(k).point1; (lines(k).point2)];
        plot(xy(:,1),xy(:,2)+240,'LineWidth',2,'Color','green');
        
        % Plot beginning and end of lines.
        plot(xy(1,1),xy(1,2)+240,'x','LineWidth',2,'Color','yellow');
        plot(xy(2,1),xy(2,2)+240,'x','LineWidth',2,'Color','red');
        
        % Determine the endpoints of the longest line segment.
        len = norm(lines(k).point1 - lines(k).point2);
        if ( len > max_len)
            max_len = len;
            xy_long = xy;
        end
    end
end
title(p1, 'Input Image');
title(p2, 'Lane Detected Output Image');

Figure contains 2 axes objects. Hidden axes object 1 with title Input Image contains an object of type image. Hidden axes object 2 with title Lane Detected Output Image contains 13 objects of type image, line. One or more of the lines displays its values using only markers

참고 항목

함수

객체

도움말 항목