-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc.cpp
58 lines (46 loc) · 1.44 KB
/
cc.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
//Opencv C++ Program to solve mazes using mathematical morphology
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cmath>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("eg1.png");
if( !src.data ) { printf("Error loading src \n"); return -1;}
//Convert the given image into Binary Image
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 10, 255, CV_THRESH_BINARY_INV);
//Detect Contours in an Image
vector<std::vector<cv::Point> > contours;
findContours(bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
if (contours.size() != 2)
{
// "Perfect maze" should have 2 walls
std::cout << "This is not a 'perfect maze' with just 2 walls!" << std::endl;
return -1;
}
Mat path = Mat::zeros(src.size(), CV_8UC1);
drawContours(path, contours, 0, CV_RGB(255,255,255), CV_FILLED);
//Dilate the Image
Mat kernel = Mat::ones(21, 21, CV_8UC1);
dilate(path, path, kernel);
//Erode the Image
Mat path_erode;
erode(path, path_erode, kernel);
//Subtract Eroded Image from the Dilate One
absdiff(path, path_erode, path);
//Draw the Path by Red Color
vector<Mat> channels;
split(src, channels);
channels[0] &= ~path;
channels[1] &= ~path;
channels[2] |= path;
Mat dst;
merge(channels, dst);
imshow("solution", dst);
waitKey(0);
return 0;
}