-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectLine.cpp
171 lines (138 loc) · 4.71 KB
/
DetectLine.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* File: DetectLine.cpp
* Author: heshan
*
* Created on April 10, 2017, 9:06 PM
*/
#include <iostream>
#include <Magick++.h>
#include "DetectLine.h"
DetectLine::DetectLine() {}
DetectLine::DetectLine(const DetectLine& orig) {}
DetectLine::~DetectLine() {}
int DetectLine::getLines(std::string inputImage, std::string outputImage, int kernelNo){
initializeImage(inputImage);
applyKernel(kernelNo);
writeImage(outputImage);
}
int DetectLine::initializeImage(std::string path){
Magick::InitializeMagick(NULL);
Magick::Image image(path);
this->img = image;
try {
image.type( Magick::GrayscaleType );
image.modifyImage();
int w = image.columns(),h = image.rows();
int row = 0,column = 0;
int range = 256; //pow(2, image.modulusDepth());
Magick::PixelPacket *pixels = image.getPixels(0, 0, w, h);
// creating the pixel matrix
this->imageMatrix = new float*[h];for(int i = 0; i < h; ++i) this->imageMatrix[i] = new float[w];
// storing meta data
this->width = w; this->height = h;
this->range = range;
for(row = 0; row < h; row++)
{
for(column = 0; column < w; column++)
{
Magick::Color color = pixels[w * row + column];
this->imageMatrix[row][column] = (color.redQuantum()/range)/256;
}
//std::cout<< std::endl;
}
} catch(std::exception &error_ ) {
std::cout << "Caught exception: " << error_.what() << std::endl;
return 1;
}
return 0;
}
int DetectLine::applyKernel(int kernelNo){
/**
* light lines against a dark background (0 - 3)
* 0 - horizontal
* 1 - vertical
* 2 - diagonal (/)
* 3 - diagonal (\)
*
* dark lines against a light background (4 - 7)
* 4 - horizontal
* 5 - vertical
* 6 - diagonal (/)
* 7 - diagonal (\)
*
**/
float kernel[8][3][3] = {
{ {-1,-1,-1},{2,2,2},{-1,-1,-1} },
{ {-1,2,-1},{-1,2,-1},{-1,2,-1} },
{ {-1,-1,2},{-1,2,-1},{2,-1,-1} },
{ {2,-1,-1},{-1,2,-1},{-1,-1,2} },
{ {1,1,1},{-2,-2,-2},{1,1,1} },
{ {1,-2,1},{1,-2,1},{1,-2,1} },
{ {1,1,-2},{1,-2,1},{-2,1,1} },
{ {-2,1,1},{1,-2,1},{1,1,-2} }
};
this->resultMatrix = new float*[this->height];for(int i = 0; i < this->height; ++i) this->resultMatrix[i] = new float[this->width];
for(int row = 0; row < (this->height)-3; row++)
{
for(int column = 0; column < (this->width)-3; column++)
{
float pSum = 0;
for(int kernelRow = 0; kernelRow < 3; kernelRow++)
{
for(int kernelColumn = 0; kernelColumn < 3; kernelColumn++)
{
pSum += (float)(this->imageMatrix[row + kernelRow][column + kernelColumn]*kernel[kernelNo][kernelRow][kernelColumn]);
}
}
this->resultMatrix[row][column] = pSum;
}
}
return 0;
}
int DetectLine::writeImage(std::string path){
Magick::Image image;
image = this->img;
image.modifyImage();
image.type(Magick::TrueColorType);
ssize_t columns = this->width;
Magick::PixelPacket *pixel_cache = image.getPixels(0,0,this->width, this->height);
for(int i = 0; i < this->width; i++)
{
for(int j = 0; j < this->height; j++)
{
// thresholding the values
float pVal = this->resultMatrix[j][i];
//std::cout<<pVal<<"\n";
if ( pVal > 2 ) { pVal = 1; }
else pVal = 0;
Magick::ColorGray gColor(pVal);
Magick::PixelPacket *pixel = pixel_cache+j*columns+i;
*pixel = gColor;
}
}
image.syncPixels();
image.write(path);
return 0;
}
int DetectLine::printImageMatrix(){
for(int row = 0; row < (this->height); row++)
{
for(int column = 0; column < (this->width); column++)
{
std::cout<<this->imageMatrix[row][column]<<" ";
}
std::cout<< std::endl;
}
return 0;
}
int DetectLine::printResultMatrix(){
for(int row = 0; row < (this->height); row++)
{
for(int column = 0; column < (this->width); column++)
{
std::cout<<this->resultMatrix[row][column]<<" ";
}
std::cout<< std::endl;
}
return 0;
}