forked from simondlevy/UE4_OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdgeDetection.cpp
73 lines (58 loc) · 1.61 KB
/
EdgeDetection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
/*
* EdgeDetection.cpp: OpenCV demo algorithm for UnrealEngine4
*
* Copyright (C) 2017 Simon D. Levy
*
* MIT License
*/
#include "EdgeDetection.h"
#include "OnScreenDebug.h"
#include "Engine.h"
#include "Runtime/Core/Public/HAL/Runnable.h"
#include "GameFramework/Actor.h"
#include "OnscreenDebug.h"
#include <opencv2/video/tracking.hpp>
#include <opencv2/core.hpp>
#include <cstdint>
EdgeDetection::EdgeDetection(int width, int height) : VisionAlgorithm(width, height)
{
_vertexCount = 0;
}
EdgeDetection::~EdgeDetection()
{
}
void EdgeDetection::perform(void)
{
// Convert color image to grayscale
cv::Mat gray;
cv::cvtColor(*_bgrimg, gray, cv::COLOR_BGR2GRAY);
// Reduce noise with a kernel 3x3
cv::Mat detected_edges;
cv::blur(gray, detected_edges, cv::Size(3, 3));
// Run Canny edge detection algorithm on blurred gray image
cv::Canny(detected_edges, detected_edges, LOW_THRESHOLD, LOW_THRESHOLD*RATIO, KERNEL_SIZE);
// Find edges
cv::Mat nonZeroCoordinates;
cv::findNonZero(detected_edges, nonZeroCoordinates);
// Store vertices (edge coordinates)
_vertexCount = min((int)nonZeroCoordinates.total(), MAX_VERTICES);
for (int i = 0; i < _vertexCount; i++) {
cv::Point vertex = nonZeroCoordinates.at<cv::Point>(i);
_vertices[i].x = vertex.x;
_vertices[i].y = vertex.y;
}
}
void EdgeDetection::draw(AHUD* hud, int leftx, int topy)
{
for (int i = 0; i < _vertexCount; ++i) {
cv::Point vertex = _vertices[i];
hud->DrawRect(EDGE_COLOR, leftx + vertex.x, topy + vertex.y, 1, 1);
}
}
/*
VisionAlgorithm * getInstance(int width, int height)
{
return new EdgeDetection(width, height);
}
*/