Skip to content

Commit 658644b

Browse files
committed
changed fitting window calculation to use doubles
1 parent 519ad8f commit 658644b

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

SEFramework/SEFramework/CoordinateSystem/CoordinateSystem.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ struct ImageCoordinate {
4545

4646
ImageCoordinate() : m_x(0), m_y(0) {}
4747
ImageCoordinate(double x, double y) : m_x(x), m_y(y) {}
48+
49+
ImageCoordinate operator*(double scalar) const {
50+
return ImageCoordinate(m_x * scalar, m_y * scalar);
51+
}
52+
53+
ImageCoordinate operator+(const ImageCoordinate& other) const {
54+
return ImageCoordinate(m_x + other.m_x, m_y + other.m_y);
55+
}
56+
57+
ImageCoordinate& operator+=(const ImageCoordinate& other) {
58+
m_x += other.m_x;
59+
m_y += other.m_y;
60+
return *this;
61+
}
62+
63+
ImageCoordinate operator-(const ImageCoordinate& other) const {
64+
return ImageCoordinate(m_x - other.m_x, m_y - other.m_y);
65+
}
66+
67+
ImageCoordinate& operator-=(const ImageCoordinate& other) {
68+
m_x -= other.m_x;
69+
m_y -= other.m_y;
70+
return *this;
71+
}
4872
};
4973

5074
class CoordinateSystem {

SEImplementation/SEImplementation/Plugin/MeasurementFrameRectangle/MeasurementFrameRectangle.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
#ifndef _SEIMPLEMENTATION_PLUGIN_MEASUREMENTFRAMERECTANGLE_MEASUREMENTFRAMERECTANGLE_H_
2525
#define _SEIMPLEMENTATION_PLUGIN_MEASUREMENTFRAMERECTANGLE_MEASUREMENTFRAMERECTANGLE_H_
2626

27+
#include <tuple>
28+
2729
#include "SEUtils/PixelRectangle.h"
30+
#include "SEFramework/CoordinateSystem/CoordinateSystem.h"
2831

2932
#include "SEFramework/Property/Property.h"
3033
#include "SEFramework/Image/Image.h"
@@ -36,11 +39,14 @@ class MeasurementFrameRectangle: public Property {
3639
virtual ~MeasurementFrameRectangle() = default;
3740

3841
explicit MeasurementFrameRectangle(bool bad_projection):
39-
m_min_coord{-1, -1}, m_max_coord{-1, -1}, m_bad_projection{bad_projection}{}
40-
41-
MeasurementFrameRectangle(PixelCoordinate min_coord, PixelCoordinate max_coord):
42-
m_min_coord{min_coord}, m_max_coord{max_coord}, m_bad_projection{false} {
43-
assert(min_coord.m_x <= max_coord.m_x && min_coord.m_y <= max_coord.m_y);
42+
m_min_coord{-1, -1}, m_max_coord{-1, -1}, m_min_coord_image{-1, -1},
43+
m_max_coord_image{-1, -1}, m_bad_projection{bad_projection} {}
44+
45+
MeasurementFrameRectangle(ImageCoordinate min_coord_image, ImageCoordinate max_coord_image):
46+
m_min_coord_image(min_coord_image), m_max_coord_image(max_coord_image), m_bad_projection{false} {
47+
assert(min_coord_image.m_x <= max_coord_image.m_x && min_coord_image.m_y <= max_coord_image.m_y);
48+
m_min_coord = PixelCoordinate(static_cast<int>(min_coord_image.m_x + 0.5), static_cast<int>(min_coord_image.m_y + 0.5));
49+
m_max_coord = PixelCoordinate(static_cast<int>(max_coord_image.m_x + 0.5), static_cast<int>(max_coord_image.m_y + 0.5));
4450
}
4551

4652
PixelCoordinate getTopLeft() const {
@@ -65,8 +71,8 @@ class MeasurementFrameRectangle: public Property {
6571
return m_max_coord.m_y - m_min_coord.m_y + 1;
6672
}
6773

68-
PixelRectangle getRect() const {
69-
return PixelRectangle(m_min_coord, m_max_coord);
74+
std::tuple<ImageCoordinate, ImageCoordinate> getImageRect() const {
75+
return std::make_tuple(m_min_coord_image, m_max_coord_image);
7076
}
7177

7278
bool badProjection() const {
@@ -75,6 +81,7 @@ class MeasurementFrameRectangle: public Property {
7581

7682
private:
7783
PixelCoordinate m_min_coord, m_max_coord;
84+
ImageCoordinate m_min_coord_image, m_max_coord_image;
7885
bool m_bad_projection;
7986
};
8087

SEImplementation/src/lib/Plugin/FlexibleModelFitting/FlexibleModelFittingIterativeTask.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ FlexibleModelFittingIterativeTask::~FlexibleModelFittingIterativeTask() {
7474
}
7575

7676
PixelRectangle FlexibleModelFittingIterativeTask::getUnclippedFittingRect(SourceInterface& source, int frame_index) const {
77-
auto fitting_rect = source.getProperty<MeasurementFrameRectangle>(frame_index).getRect();
77+
ImageCoordinate min_coord, max_coord;
78+
std::tie(min_coord, max_coord) = source.getProperty<MeasurementFrameRectangle>(frame_index).getImageRect();
7879

7980
if (m_window_type == WindowType::ROTATED_ELLIPSE) {
8081
auto ellipse = getFittingEllipse(source, frame_index);
@@ -84,14 +85,14 @@ PixelRectangle FlexibleModelFittingIterativeTask::getUnclippedFittingRect(Source
8485
return getEllipseRect(ellipse);
8586
}
8687

87-
if (fitting_rect.getWidth() <= 0 || fitting_rect.getHeight() <= 0) {
88+
if (max_coord.m_x - min_coord.m_x <= 0 || max_coord.m_y - min_coord.m_y <= 0) {
8889
return PixelRectangle();
8990
} else {
90-
auto min = fitting_rect.getTopLeft();
91-
auto max = fitting_rect.getBottomRight();
91+
auto min = min_coord;
92+
auto max = max_coord;
9293

9394
// FIXME temporary, for now just enlarge the area by a fixed amount of pixels
94-
PixelCoordinate border = (max - min) * .8 + PixelCoordinate(2, 2);
95+
ImageCoordinate border = (max - min) * .8 + ImageCoordinate(2.0, 2.0);
9596

9697
min -= border;
9798
max += border;
@@ -125,7 +126,10 @@ PixelRectangle FlexibleModelFittingIterativeTask::getUnclippedFittingRect(Source
125126
max.m_y = min.m_y + size;
126127
}
127128

128-
return PixelRectangle(min, max);
129+
auto min_pc = PixelCoordinate(static_cast<int>(min.m_x + 0.5), static_cast<int>(min.m_y + 0.5));
130+
auto max_pc = PixelCoordinate(static_cast<int>(max.m_x + 0.5), static_cast<int>(max.m_y + 0.5));
131+
132+
return PixelRectangle(min_pc, max_pc);
129133
}
130134
}
131135

SEImplementation/src/lib/Plugin/MeasurementFrameRectangle/MeasurementFrameRectangleTask.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,20 @@ void MeasurementFrameRectangleTask::computeProperties(SourceInterface& source) c
6767
auto max_x = std::max(coord1.m_x, std::max(coord2.m_x, std::max(coord3.m_x, coord4.m_x)));
6868
auto max_y = std::max(coord1.m_y, std::max(coord2.m_y, std::max(coord3.m_y, coord4.m_y)));
6969

70-
PixelCoordinate min_coord, max_coord;
71-
min_coord.m_x = int(min_x);
72-
min_coord.m_y = int(min_y);
73-
max_coord.m_x = int(max_x) + 1;
74-
max_coord.m_y = int(max_y) + 1;
75-
7670
// The full boundaries may lie outside of the frame
77-
if (bad_coordinates || max_coord.m_x < 0 || max_coord.m_y < 0 ||
78-
min_coord.m_x >= measurement_frame_info.getWidth() || min_coord.m_y >= measurement_frame_info.getHeight()) {
71+
if (bad_coordinates || max_x < 0.0 || max_y < 0.0 ||
72+
min_x >= measurement_frame_info.getWidth() - 0.5 || min_y >= measurement_frame_info.getHeight() - 0.5) {
7973
source.setIndexedProperty<MeasurementFrameRectangle>(m_instance, bad_coordinates);
8074
}
8175
// Clip the coordinates to fit the available image
8276
else {
83-
min_coord.m_x = std::max(0, min_coord.m_x);
84-
min_coord.m_y = std::max(0, min_coord.m_y);
85-
max_coord.m_x = std::min(measurement_frame_info.getWidth() - 1, max_coord.m_x);
86-
max_coord.m_y = std::min(measurement_frame_info.getHeight() - 1, max_coord.m_y);
77+
min_x = std::max(0.0, min_x);
78+
min_y = std::max(0.0, min_y);
79+
max_x = std::min(double(measurement_frame_info.getWidth()) - 0.5, max_x);
80+
max_y = std::min(double(measurement_frame_info.getHeight()) - 0.5, max_y);
8781

88-
source.setIndexedProperty<MeasurementFrameRectangle>(m_instance, min_coord, max_coord);
82+
source.setIndexedProperty<MeasurementFrameRectangle>(
83+
m_instance, ImageCoordinate(min_x, min_y), ImageCoordinate(max_x, max_y));
8984
}
9085
}
9186

SEImplementation/src/lib/Plugin/MeasurementFrameRectangle/MeasurementFrameRectangleTaskNoDetect.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "SEImplementation/Plugin/DetectionFrameCoordinates/DetectionFrameCoordinates.h"
2424
#include <SEImplementation/Plugin/WorldCentroid/WorldCentroid.h>
2525
#include <SEImplementation/Plugin/AssocMode/AssocMode.h>
26+
#include <SEImplementation/Plugin/ReferenceCoordinates/ReferenceCoordinates.h>
2627

2728
#include <SEImplementation/Plugin/MeasurementFrameRectangle/MeasurementFrameRectangle.h>
2829
#include <SEImplementation/Plugin/MeasurementFrameRectangle/MeasurementFrameRectangleTaskNoDetect.h>
@@ -31,23 +32,27 @@ namespace SourceXtractor {
3132

3233
void MeasurementFrameRectangleTaskNoDetect::computeProperties(SourceInterface& source) const {
3334
auto measurement_frame_coordinates = source.getProperty<MeasurementFrameCoordinates>(m_instance).getCoordinateSystem();
35+
auto reference_frame_coordinates = source.getProperty<ReferenceCoordinates>().getCoordinateSystem();
36+
3437
const auto& measurement_frame_info = source.getProperty<MeasurementFrameInfo>(m_instance);
3538
const auto& world_centroid = source.getProperty<WorldCentroid>();
3639
const auto& assoc_mode = source.getProperty<AssocMode>();
3740

38-
auto coord = world_centroid.getCentroid();
39-
4041
bool bad_coordinates = false;
4142
ImageCoordinate coord1, coord2, coord3, coord4;
4243
try {
43-
int w = assoc_mode.getRefFramePixelWidth();
44-
int h = assoc_mode.getRefFramePixelHeight();
44+
auto w = assoc_mode.getRefFramePixelWidth();
45+
auto h = assoc_mode.getRefFramePixelHeight();
4546

46-
auto c = measurement_frame_coordinates->worldToImage(coord);
47-
coord1 = ImageCoordinate(c.m_x - w, c.m_y - h);
48-
coord2 = ImageCoordinate(c.m_x + w, c.m_y - h);
49-
coord3 = ImageCoordinate(c.m_x - w, c.m_y + h);
50-
coord4 = ImageCoordinate(c.m_x + w, c.m_y + h);
47+
auto c = reference_frame_coordinates->worldToImage(world_centroid.getCentroid());
48+
coord1 = measurement_frame_coordinates->worldToImage(
49+
reference_frame_coordinates->imageToWorld(ImageCoordinate(c.m_x - w, c.m_y - h)));
50+
coord2 = measurement_frame_coordinates->worldToImage(
51+
reference_frame_coordinates->imageToWorld(ImageCoordinate(c.m_x + w, c.m_y - h)));
52+
coord3 = measurement_frame_coordinates->worldToImage(
53+
reference_frame_coordinates->imageToWorld(ImageCoordinate(c.m_x - w, c.m_y + h)));
54+
coord4 = measurement_frame_coordinates->worldToImage(
55+
reference_frame_coordinates->imageToWorld(ImageCoordinate(c.m_x + w, c.m_y + h)));
5156
}
5257
catch (const InvalidCoordinatesException&) {
5358
bad_coordinates = true;
@@ -59,25 +64,20 @@ void MeasurementFrameRectangleTaskNoDetect::computeProperties(SourceInterface& s
5964
auto max_x = std::max(coord1.m_x, std::max(coord2.m_x, std::max(coord3.m_x, coord4.m_x)));
6065
auto max_y = std::max(coord1.m_y, std::max(coord2.m_y, std::max(coord3.m_y, coord4.m_y)));
6166

62-
PixelCoordinate min_coord, max_coord;
63-
min_coord.m_x = int(min_x);
64-
min_coord.m_y = int(min_y);
65-
max_coord.m_x = int(max_x) + 1;
66-
max_coord.m_y = int(max_y) + 1;
67-
6867
// The full boundaries may lie outside of the frame
69-
if (bad_coordinates || max_coord.m_x < 0 || max_coord.m_y < 0 ||
70-
min_coord.m_x >= measurement_frame_info.getWidth() || min_coord.m_y >= measurement_frame_info.getHeight()) {
68+
if (bad_coordinates || max_x < 0.0 || max_y < 0.0 ||
69+
min_x >= measurement_frame_info.getWidth() - 0.5 || min_y >= measurement_frame_info.getHeight() - 0.5) {
7170
source.setIndexedProperty<MeasurementFrameRectangle>(m_instance, bad_coordinates);
7271
}
7372
// Clip the coordinates to fit the available image
7473
else {
75-
min_coord.m_x = std::max(0, min_coord.m_x);
76-
min_coord.m_y = std::max(0, min_coord.m_y);
77-
max_coord.m_x = std::min(measurement_frame_info.getWidth() - 1, max_coord.m_x);
78-
max_coord.m_y = std::min(measurement_frame_info.getHeight() - 1, max_coord.m_y);
74+
min_x = std::max(0.0, min_x);
75+
min_y = std::max(0.0, min_y);
76+
max_x = std::min(double(measurement_frame_info.getWidth()) - 0.5, max_x);
77+
max_y = std::min(double(measurement_frame_info.getHeight()) - 0.5, max_y);
7978

80-
source.setIndexedProperty<MeasurementFrameRectangle>(m_instance, min_coord, max_coord);
79+
source.setIndexedProperty<MeasurementFrameRectangle>(
80+
m_instance, ImageCoordinate(min_x, min_y), ImageCoordinate(max_x, max_y));
8181
}
8282
}
8383

0 commit comments

Comments
 (0)