Skip to content

Commit 8d820b2

Browse files
committed
Working calibration and thresholding
1 parent 3998797 commit 8d820b2

File tree

2 files changed

+116
-48
lines changed

2 files changed

+116
-48
lines changed

src/gui.cpp

Lines changed: 113 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include <opencv2/opencv.hpp>
22
#include "gui.h"
3+
#include <SDL2/SDL.h>
4+
#include <SDL2/SDL_ttf.h>
5+
#include <SDL2/SDL_image.h>
36

47
using namespace cv;
58

@@ -10,20 +13,45 @@ void check_pointer(void* ptr) {
1013
}
1114
}
1215

13-
int gui_window::calibrate() {
16+
class gui_window_implementation {
17+
Solver * solver;
18+
int window_width;
19+
int window_height;
20+
SDL_Window* sdl_window;
21+
SDL_Renderer* sdl_renderer;
22+
SDL_Texture* sdl_display;
23+
SDL_Texture *sdl_texture;
24+
SDL_Surface* sdl_surface;
25+
uchar4* outputBitmap;
26+
SDL_Rect srcrect, dstrect;
27+
cv::VideoCapture cap;
28+
cv::Size target_size;
29+
cv::Mat H;
30+
int calibrate();
31+
public:
32+
gui_window_implementation(int window_width_, int window_height_, Solver * solver_);
33+
int eventloop();
34+
~gui_window_implementation();
35+
};
36+
37+
gui_window::gui_window(int window_width_, int window_height_, Solver * solver_) :
38+
impl(new gui_window_implementation(window_width_, window_height_, solver_)) {
39+
}
40+
41+
int gui_window::eventloop() {
42+
return impl->eventloop();
43+
}
44+
45+
gui_window::~gui_window() {
46+
if (impl) delete impl;
47+
}
48+
49+
50+
int gui_window_implementation::calibrate() {
1451
int board_width = 7;
1552
int board_height = 5;
1653
double mar = 0.5;
17-
namedWindow("Video Player");//Declaring the video to show the video//
18-
VideoCapture cap(0);//Declaring an object to capture stream of frames from default camera//
19-
if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
20-
cout << "No video stream detected" << endl;
21-
system("pause");
22-
return-1;
23-
}
24-
cap.set(CAP_PROP_FRAME_WIDTH,1920);
25-
cap.set(CAP_PROP_FRAME_HEIGHT,1080);
26-
54+
2755
// SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
2856
SDL_SetRenderDrawColor(sdl_renderer, 128, 128, 128, 255);
2957
SDL_RenderClear(sdl_renderer);
@@ -88,32 +116,36 @@ int gui_window::calibrate() {
88116
printf("%d %d\n",(int) corners.size().width,(int) corners.size().height);
89117

90118

91-
Size target_size(window_width,window_height);
119+
target_size.width = window_width;
120+
target_size.height = window_height;
92121
std::vector<cv::Point3f> object_points;
93122
double square_size = target_size.width/(board_width+1);
94-
for (int i = 0; i < board_height; ++i) {
95-
for (int j = 0; j < board_width; ++j) {
96-
object_points.push_back(cv::Point3f(check_x[j+1], check_y[i+1], 0));
97-
}
98-
}
99-
100-
Mat H = findHomography(object_points, corners);
101-
while (true) {
102-
Mat myImage, warped_image;
103-
cap >> myImage;
104-
warpPerspective(myImage, warped_image, H, target_size,WARP_INVERSE_MAP);
105-
imshow("Video Player", warped_image);//Showing the video//
106-
char c = (char)waitKey(1);//Allowing 25 milliseconds frame processing time and initiating break condition//
107-
if (c == 27){ //If 'Esc' is entered break the loop//
108-
break;
123+
printf("%d %d\n",(int) check_x.size(),(int) check_y.size());
124+
printf("%d %d\n",(int) board_width,(int) board_height);
125+
for (int iy = 1; iy<=board_height; iy++) {
126+
for (int ix = 1; ix<=board_width; ix++) {
127+
object_points.push_back(cv::Point3f(check_x[ix], check_y[iy], 0));
109128
}
110129
}
111-
130+
131+
H = findHomography(object_points, corners);
132+
133+
return 0;
112134
}
113135

114136

115-
gui_window::gui_window(int window_width_, int window_height_, Solver * solver_) : solver(solver_), window_width(window_width_), window_height(window_height_) {
137+
gui_window_implementation::gui_window_implementation(int window_width_, int window_height_, Solver * solver_)
138+
: solver(solver_), window_width(window_width_), window_height(window_height_), cap(0) {
116139
output("Initializing SDL window\n");
140+
141+
if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
142+
cout << "No video stream detected" << endl;
143+
exit(-1);
144+
}
145+
cap.set(CAP_PROP_FRAME_WIDTH,1920);
146+
cap.set(CAP_PROP_FRAME_HEIGHT,1080);
147+
148+
117149
// sdl_window = SDL_CreateWindow("Graphical Window", SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), sx, sy, SDL_WINDOW_FULLSCREEN);
118150
sdl_window = SDL_CreateWindow("Graphical Window", 0, 0, window_width, window_height, 0);
119151
check_pointer(sdl_window);
@@ -149,14 +181,17 @@ gui_window::gui_window(int window_width_, int window_height_, Solver * solver_)
149181
dstrect.x = 0;
150182
dstrect.x = (window_width - dstrect.w)/2;
151183
dstrect.y = (window_height - dstrect.h)/2;
184+
namedWindow("Video Player");//Declaring the video to show the video//
152185

153186
calibrate();
187+
154188

155189
}
156190

157-
int gui_window::eventloop() {
191+
int gui_window_implementation::eventloop() {
158192
SDL_Event event;
159193
int ret = 0;
194+
160195
solver->lattice->Color(outputBitmap); // Updating graphics
161196
SDL_LockSurface(sdl_surface);
162197
CudaMemcpy(sdl_surface->pixels, outputBitmap, sizeof(uchar4)*solver->region.sizeL(), cudaMemcpyDeviceToHost);
@@ -168,9 +203,53 @@ int gui_window::eventloop() {
168203
SDL_RenderClear(sdl_renderer);
169204
SDL_RenderCopy(sdl_renderer, sdl_texture, &srcrect, &dstrect);
170205
SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, 255);
171-
SDL_RenderDrawLine(sdl_renderer, 0, 0, 200, 200);
206+
//SDL_RenderDrawLine(sdl_renderer, 0, 0, 200, 200);
172207
SDL_RenderPresent( sdl_renderer );
173208

209+
Mat myImage, newImage, labelImage, newImage2, binImage, camImage;
210+
211+
cap >> camImage;
212+
if (camImage.empty()) exit(2);
213+
214+
warpPerspective(camImage, myImage, H, target_size,WARP_INVERSE_MAP);
215+
216+
cvtColor(myImage, newImage, cv::COLOR_RGB2GRAY);
217+
threshold(newImage, binImage, 20, 1, THRESH_BINARY_INV);
218+
219+
Mat stats, centroids;
220+
int nLabels = connectedComponentsWithStats(binImage, labelImage, stats, centroids);
221+
222+
vector<int> index(nLabels-1, 0);
223+
for (int i = 0; i < nLabels-1; i++) index[i] = i + 1;
224+
225+
const int CV_AREA = ConnectedComponentsTypes::CC_STAT_AREA;
226+
size_t idx_b = 0xFFFFFF;
227+
int idx_b_max = 0;
228+
for (size_t i=1;i<nLabels;i++) {
229+
int area = stats.at<int>(i,CV_AREA);
230+
if (area > idx_b_max) {
231+
idx_b = i;
232+
idx_b_max = area;
233+
}
234+
}
235+
236+
Vec3b col1(0, 0, 255);
237+
Vec3b col2(0, 255, 0);
238+
Mat dst(myImage.size(), CV_8UC3);
239+
for(int r = 0; r < dst.rows; ++r){
240+
for(int c = 0; c < dst.cols; ++c){
241+
int label = labelImage.at<int>(r, c);
242+
Vec3b &pixel = dst.at<Vec3b>(r, c);
243+
if (label == idx_b) {
244+
pixel = col1;
245+
} else {
246+
pixel = col2;
247+
}
248+
}
249+
}
250+
imshow("Video Player", dst);
251+
waitKey(1);
252+
174253
while( SDL_PollEvent(&event) )
175254
{
176255
if (event.type == SDL_QUIT) {
@@ -182,10 +261,12 @@ int gui_window::eventloop() {
182261
ret = 1;
183262
}
184263
}
264+
185265
return 0;
186266
}
187267

188-
gui_window::~gui_window() {
268+
gui_window_implementation::~gui_window_implementation() {
269+
cap.release();
189270
CudaFree( outputBitmap );
190271
output("Killing SDL window\n");
191272
}

src/gui.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,10 @@
1414
#include <cuda.h>
1515
#include <cuda_gl_interop.h>
1616

17-
#include <SDL2/SDL.h>
18-
#include <SDL2/SDL_ttf.h>
19-
#include <SDL2/SDL_image.h>
17+
class gui_window_implementation;
2018

21-
22-
struct gui_window {
23-
Solver * solver;
24-
int window_width;
25-
int window_height;
26-
SDL_Window* sdl_window;
27-
SDL_Renderer* sdl_renderer;
28-
SDL_Texture* sdl_display;
29-
SDL_Texture *sdl_texture;
30-
SDL_Surface* sdl_surface;
31-
uchar4* outputBitmap;
32-
SDL_Rect srcrect, dstrect;
33-
int calibrate();
19+
class gui_window {
20+
gui_window_implementation* impl;
3421
public:
3522
gui_window(int window_width_, int window_height_, Solver * solver_);
3623
~gui_window();

0 commit comments

Comments
 (0)