-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
73 lines (60 loc) · 1.91 KB
/
s1.cpp
File metadata and controls
73 lines (60 loc) · 1.91 KB
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "BinaryImage.h"
#include "Validation.h"
#include "pgm/Image.h"
#include "objects/Object.h"
#include "objects/ObjectLabeler.h"
#include "objects/ImageObjectDatabase.h"
#include "Sphere.h"
using namespace std;
int main(int argc, const char * argv[]) {
if (argc != 4) {
cerr << "ERROR: Invalid number of arguments." << endl;
exit(-1);
}
const char* input_img_fname = argv[1];
const char* output_fname = argv[3];
// Check if threshold is valid int and set to variable
if (!isValidType<int, const char*>(argv[2])){
cerr << "ERROR: Invalid threshold number." << endl;
exit(-1);
}
int threshold = atoi(argv[2]);
// Read image from input file
Image* input_img = new Image;
if (readImage(input_img, input_img_fname) < 0) {
cerr << "ERROR: Something went wrong reading the input image" << endl;
exit(-1);
}
// Convert to binary
Image* binary_img = new Image;
convertToBinary(input_img, binary_img, threshold);
// Label objects in image and store in image databas
ObjectLabeler labeler;
Image* labeled_img = new Image;
labeler.labelObjects(binary_img, labeled_img);
ImageObjectDatabase iodb(labeled_img);
// create new sphere with labeled image and center
pair <float, float> center = iodb.getObject(1)->calculateCenter();
Sphere s(labeled_img, center);
// Open output file
ofstream writef;
writef.open(output_fname);
if (writef.fail()) {
cerr << "ERROR: Something went wrong reading the output file" << endl;
exit(-1);
}
// Write sphere attributes to file
if (writef.is_open()) {
writef << center.first << " ";
writef << center.second<< " ";
writef << s.getRadius() << "\n";
}
writef.close();
delete input_img;
delete binary_img;
delete labeled_img;
return 0;
}