-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice.cpp
117 lines (97 loc) · 3.74 KB
/
dice.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**Code to detect pips in a dice using c++ and opencv
Author:Bhavan Vasu,
Graduate research assistant,
Real time and computer vision lab,
Rochester Institute of technology,New york-14623
email:bxv7657@rit.edu**/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
std::ostringstream ttext;
cv::Mat src,dst,roi,roi_g;
int src_gray=0;
using namespace cv;
using namespace std;
int countpips(cv::Mat roi)
{
//resizing images of individual dices
cv::resize(roi, roi, cv::Size(150, 150));
cvtColor(roi, roi_g, CV_RGB2GRAY);
cv::threshold(roi_g, roi_g, 200, 255, cv::THRESH_BINARY | CV_THRESH_OTSU );
//Using flooding of corners for better isolation of the pits
cv::floodFill(roi_g, cv::Point(0,0), cv::Scalar(255));
cv::floodFill(roi_g, cv::Point(0,149), cv::Scalar(255));
cv::floodFill(roi_g, cv::Point(149,0), cv::Scalar(255));
cv::floodFill(roi_g, cv::Point(149,149), cv::Scalar(255));
vector<Vec3f> cir;
//Detecting all pits in a single dice
HoughCircles( roi_g, cir, CV_HOUGH_GRADIENT,1 , roi_g.rows/6,7, 10,9 , 30 );
//Iterating through all the pits in a dice
for( size_t i = 0; i < cir.size(); i++ )
{
Point center(cvRound(cir[i][0]), cvRound(cir[i][1]));
int radius = cvRound(cir[i][2]);
//Drawing a green circle around each pit in a dice
circle( roi, center, radius, Scalar(0,255,0), 3, 8, 0 );
}
//returns the number of pits in a dice
return cir.size();
}
int main()
{
std::vector<std::vector<cv::Point> > conto;
std::vector<cv::Vec4i> hirer;
cv::Mat ds,Th,ca,droi;
//Reading the image dice1.png(modify according to requirement)
cv::Mat image=cv::imread("/home/bxv7657/NASCENT/diced/im/dice6.png");
//Detecting the canny edges for isolating edges: Helps removing the wood baground
cv::Canny( image, ca, 200, 200*5, 3 );
int erosion_size = 35;
Mat element = getStructuringElement(cv::MORPH_RECT,cv::Size(2 * erosion_size + 1, 2 *erosion_size + 1),cv::Point(erosion_size, erosion_size));
// Perform erosion or dilation to improve segmentation :This was crucial
dilate(ca,dst,element);
erode(dst,ca,element);
int tot=0;
// Finding Contours helps us obtain co-ordinates of individual dice+pips
cv::findContours( ca,conto, hirer,CV_RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
//Iterate through all contours(including dice and pips)
for(int i=0; i<conto.size(); i++)
{
// Calculate contour area to check if the detected contour is a pip or dice
double contArea = cv::contourArea(conto[i]);
// filter contours based on our area
if (contArea > 320){
//Get bounding rectangle co-ordinates
cv::Rect Bounco = cv::boundingRect(cv::Mat(conto[i]));
// Crop individual dices
droi = image(Bounco);
// count number of pips in one dice
src_gray=countpips(droi);
// Total pips in the image
tot+=src_gray;
if(src_gray > 0)
{
// Individual dice value
std::ostringstream diceText;
diceText << "" << src_gray;
// Draw the value of each dice on image
cv::putText(image, diceText.str(),
cv::Point(Bounco.x,Bounco.y+Bounco.height+20),
cv::FONT_HERSHEY_SIMPLEX,0.8,cv::Scalar::all(255),1,8);
rectangle(image,Bounco.tl(), Bounco.br(),Scalar(0,255,0),2,8,0);
}
}
}
// Total dice value in the image
ttext << "Sum:" << tot;
// Draw the total value on the image
cv::putText(image,ttext.str(),cv::Point(100,100),cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar::all(255), 1, 8);
// Displaying final image
cv::namedWindow("DiceDot", CV_WINDOW_AUTOSIZE );
cv::imshow("DiceDot", image);
cv::waitKey(0);
//std::cout << "Number of pips :" << tot<< std::endl;
// Writing the final image 'Result_dice1.png'
cv::imwrite( "/home/bxv7657/NASCENT/diced/im/Result_dice1.png", image);
}