Skip to content

Commit 2f32845

Browse files
committed
Add Edge Detector module for PV model and TB
1 parent f1003ef commit 2f32845

File tree

6 files changed

+348
-0
lines changed

6 files changed

+348
-0
lines changed

modules/edge-detector/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Adding stb lib for image handling
2+
INCDIR_STB=lib/stb
3+
INCLUDES_STB := $(wildcard $(INCDIR_STB)/*.h)
4+
5+
# Include common Makefile
6+
include ../Makefile
7+
8+
# Include stb lib for compilation
9+
INCLUDES += INCLUDES_STB
10+
11+
# Defining preprocessor directive for debug
12+
ifdef IPS_DEBUG_EN
13+
CFLAGS += -DIPS_DEBUG_EN
14+
LFLAGS += -DIPS_DEBUG_EN
15+
endif # IPS_DEBUG_EN
16+
17+
# Defining preprocessor directive for dumping enable
18+
ifdef IPS_DUMP_EN
19+
CFLAGS += -DIPS_DUMP_EN
20+
LFLAGS += -DIPS_DUMP_EN
21+
endif # IPS_DUMP_EN
22+
23+
# Defining preprocessor directive for normalizing the resulting magnitude
24+
ifdef TEST_NORMALIZE_MAGNITUDE
25+
CFLAGS += -DTEST_NORMALIZE_MAGNITUDE
26+
LFLAGS += -DTEST_NORMALIZE_MAGNITUDE
27+
endif # TEST_NORMALIZE_MAGNITUDE
28+
29+
# Run the compiled file
30+
run:
31+
@./test
32+
33+
# Show waveform
34+
waveform:
35+
@gtkwave edge_detector.vcd

modules/edge-detector/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# **Edge Detector Module**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef SOBEL_EDGE_DETECTOR_HPP
2+
#define SOBEL_EDGE_DETECTOR_HPP
3+
4+
#include <systemc.h>
5+
6+
SC_MODULE(Edge_Detector)
7+
{
8+
9+
int localWindow[3][3];
10+
11+
const int sobelGradientX[3][3] = {{-1, 0, 1},
12+
{-2, 0, 2},
13+
{-1, 0, 1}};
14+
const int sobelGradientY[3][3] = {{-1, -2, -1},
15+
{ 0, 0, 0},
16+
{ 1, 2, 1}};
17+
18+
int resultSobelGradientX;
19+
int resultSobelGradientY;
20+
21+
SC_CTOR(Edge_Detector)
22+
{
23+
// SC_METHOD(hello_world);
24+
}
25+
26+
void set_local_window(int window[3][3]);
27+
28+
void compute_sobel_gradient_x();
29+
30+
void compute_sobel_gradient_y();
31+
32+
int obtain_sobel_gradient_x();
33+
34+
int obtain_sobel_gradient_y();
35+
36+
};
37+
38+
#endif // SOBEL_EDGE_DETECTOR_HPP
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "sobel_edge_detector_pv_model.hpp"
2+
3+
void Edge_Detector::set_local_window(int window[3][3])
4+
{
5+
for (int i = 0; i < 3; i++)
6+
{
7+
for (int j = 0; j < 3; j++)
8+
{
9+
this->localWindow[i][j] = window[i][j];
10+
}
11+
}
12+
}
13+
14+
void Edge_Detector::compute_sobel_gradient_x()
15+
{
16+
this->resultSobelGradientX = 0;
17+
18+
for (int i = 0; i < 3; i++)
19+
{
20+
for (int j = 0; j < 3; j++)
21+
{
22+
this->resultSobelGradientX += this->localWindow[i][j] * this->sobelGradientX[i][j];
23+
}
24+
}
25+
}
26+
27+
void Edge_Detector::compute_sobel_gradient_y()
28+
{
29+
this->resultSobelGradientY = 0;
30+
31+
for (int i = 0; i < 3; i++)
32+
{
33+
for (int j = 0; j < 3; j++)
34+
{
35+
this->resultSobelGradientY += this->localWindow[i][j] * this->sobelGradientY[i][j];
36+
}
37+
}
38+
}
39+
40+
int Edge_Detector::obtain_sobel_gradient_x()
41+
{
42+
this->compute_sobel_gradient_x();
43+
44+
return this->resultSobelGradientX;
45+
}
46+
47+
int Edge_Detector::obtain_sobel_gradient_y()
48+
{
49+
this->compute_sobel_gradient_y();
50+
51+
return this->resultSobelGradientY;
52+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#define int64 systemc_int64
2+
#define uint64 systemc_uint64
3+
#include <systemc.h>
4+
#undef int64
5+
#undef uint64
6+
#define int64 opencv_int64
7+
#define uint64 opencv_uint64
8+
#include <opencv2/opencv.hpp>
9+
#undef int64
10+
#undef uint64
11+
12+
#include <cmath>
13+
14+
#include "sobel_edge_detector_pv_model.hpp"
15+
16+
using namespace cv;
17+
18+
int sc_main(int, char*[])
19+
{
20+
Mat greyImage, colorImage;
21+
22+
int localWindow[3][3];
23+
int localGradientX, localGradientY;
24+
int localResult;
25+
#ifdef TEST_NORMALIZE_MAGNITUDE
26+
int** tmpValues;
27+
int maxTmpValue = 0;
28+
#endif // TEST_NORMALIZE_MAGNITUDE
29+
30+
// Pass command linke arguments
31+
sc_argc();
32+
sc_argv();
33+
34+
// Open VCD file
35+
sc_trace_file* wf = sc_create_vcd_trace_file("edge_detector");
36+
wf->set_time_unit(1, SC_NS);
37+
38+
Edge_Detector edge_detector("edge_detector");
39+
40+
colorImage = imread("../../tools/datagen/src/imgs/car.jpg", IMREAD_UNCHANGED);
41+
42+
if (colorImage.empty())
43+
{
44+
cout << "Image File " << "Not Found" << endl;
45+
46+
// wait for any key press
47+
return -1;
48+
}
49+
50+
cvtColor(colorImage, greyImage, cv::COLOR_BGR2GRAY);
51+
52+
Mat detectedImage(greyImage.rows, greyImage.cols, CV_8UC1);
53+
54+
#ifdef TEST_NORMALIZE_MAGNITUDE
55+
tmpValues = new int*[greyImage.rows];
56+
for (int i = 0; i < greyImage.rows; i++)
57+
{
58+
tmpValues[i] = new int[greyImage.cols];
59+
}
60+
#endif // TEST_NORMALIZE_MAGNITUDE
61+
62+
sc_start();
63+
64+
for (int i = 0; i < greyImage.rows; i++)
65+
{
66+
for (int j = 0; j < greyImage.cols; j++)
67+
{
68+
for (int k = 0; k < 3; k++)
69+
{
70+
for (int l = 0; l < 3; l++)
71+
{
72+
if ((i == 0) && (j == 0)) // Upper left corner
73+
{
74+
if ((k == 0) || (l == 0))
75+
{
76+
localWindow[k][l] = 0;
77+
}
78+
else
79+
{
80+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
81+
}
82+
}
83+
else if ((i == 0) && (j == greyImage.cols - 1)) // Upper right corner
84+
{
85+
if ((k == 0) || (l == 2))
86+
{
87+
localWindow[k][l] = 0;
88+
}
89+
else
90+
{
91+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
92+
}
93+
}
94+
else if (i == 0) // Upper border
95+
{
96+
if (k == 0)
97+
{
98+
localWindow[k][l] = 0;
99+
}
100+
else
101+
{
102+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
103+
}
104+
}
105+
else if ((i == greyImage.rows - 1) && (j == 0)) // Lower left corner
106+
{
107+
if ((k == 2) || (l == 0))
108+
{
109+
localWindow[k][l] = 0;
110+
}
111+
else
112+
{
113+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
114+
}
115+
}
116+
else if ((i == greyImage.rows - 1) && (j == greyImage.cols - 1)) // Lower right corner
117+
{
118+
if ((k == 2) || (l == 2))
119+
{
120+
localWindow[k][l] = 0;
121+
}
122+
else
123+
{
124+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
125+
}
126+
}
127+
else if (i == greyImage.rows - 1) // Lower border
128+
{
129+
if (k == 2)
130+
{
131+
localWindow[k][l] = 0;
132+
}
133+
else
134+
{
135+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
136+
}
137+
}
138+
else if (j == 0) // Left border
139+
{
140+
if (l == 0)
141+
{
142+
localWindow[k][l] = 0;
143+
}
144+
else
145+
{
146+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
147+
}
148+
}
149+
else if (j == greyImage.cols - 1) // Right border
150+
{
151+
if (l == 2)
152+
{
153+
localWindow[k][l] = 0;
154+
}
155+
else
156+
{
157+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
158+
}
159+
}
160+
else
161+
{
162+
localWindow[k][l] = (int)greyImage.at<uchar>(i + k - 1, j + l - 1);
163+
}
164+
}
165+
}
166+
167+
edge_detector.set_local_window(localWindow);
168+
localGradientX = edge_detector.obtain_sobel_gradient_x();
169+
localGradientY = edge_detector.obtain_sobel_gradient_y();
170+
171+
localResult = (int)sqrt((float)(pow(localGradientX, 2)) + (float)(pow(localGradientY, 2)));
172+
#ifdef TEST_NORMALIZE_MAGNITUDE
173+
tmpValues[i][j] = localResult;
174+
if (localResult > maxTmpValue)
175+
{
176+
maxTmpValue = localResult;
177+
}
178+
#else
179+
if (localResult > 255)
180+
{
181+
detectedImage.at<uchar>(i, j) = 255;
182+
}
183+
else
184+
{
185+
detectedImage.at<uchar>(i, j) = localResult;
186+
}
187+
#endif // TEST_NORMALIZE_MAGNITUDE
188+
}
189+
}
190+
191+
#ifdef TEST_NORMALIZE_MAGNITUDE
192+
for (int i = 0; i < detectedImage.rows; i++)
193+
{
194+
for (int j = 0; j < detectedImage.cols; j++)
195+
{
196+
detectedImage.at<uchar>(i, j) = (char)((int)(255.0 * (((float)(tmpValues[i][j])) / ((float)(maxTmpValue)))));
197+
}
198+
}
199+
#endif // TEST_NORMALIZE_MAGNITUDE
200+
201+
imshow("Window Name", colorImage);
202+
203+
waitKey(0);
204+
205+
imshow("Window Name", greyImage);
206+
207+
waitKey(0);
208+
209+
imshow("Window Name", detectedImage);
210+
211+
waitKey(0);
212+
213+
std::cout << "@" << sc_time_stamp() << " Terminating simulation" << std::endl;
214+
sc_close_vcd_trace_file(wf);
215+
216+
return 0;
217+
}

modules/edge-detector/utils.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
export SYSTEMC=/home/systemc-2.3.3
3+
export LD_LIBRARY_PATH=$SYSTEMC/lib-linux64:/usr/local/lib
4+
export USER_DEF_SYSTEMC_DIR=1
5+
export INCLUDE_OPENCV=1

0 commit comments

Comments
 (0)