Skip to content

Commit 6597b53

Browse files
committed
feat: new cropping action parameters in chains
- RGB/BGR crop conversion - fixed width and height cropping as an alternative to ratio
1 parent 955b178 commit 6597b53

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/chain_actions.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace dd
4646
double bratio = 0.0;
4747
if (_params.has("padding_ratio"))
4848
bratio = _params.get("padding_ratio").get<double>(); // e.g. 0.055
49-
std::vector<APIData> cvad;
5049

5150
bool save_crops = false;
5251
if (_params.has("save_crops"))
@@ -56,6 +55,22 @@ namespace dd
5655
if (_params.has("save_path"))
5756
save_path = _params.get("save_path").get<std::string>() + "/";
5857

58+
bool to_rgb = false;
59+
if (_params.has("to_rgb"))
60+
to_rgb = _params.get("to_rgb").get<bool>();
61+
bool to_bgr = false;
62+
if (_params.has("to_bgr"))
63+
to_bgr = _params.get("to_bgr").get<bool>();
64+
65+
int fixed_width = 0;
66+
if (_params.has("fixed_width"))
67+
fixed_width = _params.get("fixed_width").get<int>();
68+
int fixed_height = 0;
69+
if (_params.has("fixed_height"))
70+
fixed_height = _params.get("fixed_height").get<int>();
71+
72+
std::vector<APIData> cvad;
73+
5974
// iterate image batch
6075
for (size_t i = 0; i < vad.size(); i++)
6176
{
@@ -95,6 +110,16 @@ namespace dd
95110
double cymax
96111
= std::min(static_cast<double>(img.rows), ymax + deltay);
97112

113+
if (fixed_width > 0 || fixed_height > 0)
114+
{
115+
double xcenter = cxmin + (cxmax - cxmin) / 2.0;
116+
double ycenter = cymin + (cymax - cymin) / 2.0;
117+
cxmin = int(xcenter - fixed_width / 2.0);
118+
cxmax = int(xcenter + fixed_width / 2.0);
119+
cymin = int(ycenter - fixed_height / 2.0);
120+
cymax = int(ycenter + fixed_height / 2.0);
121+
}
122+
98123
if (cxmin > img.cols || cymin > img.rows || cxmax < 0 || cymax < 0)
99124
{
100125
_chain_logger->warn("bounding box does not intersect image, "
@@ -105,6 +130,12 @@ namespace dd
105130
cv::Rect roi(cxmin, cymin, cxmax - cxmin, cymax - cymin);
106131
cv::Mat cropped_img = img(roi);
107132

133+
// set channels as needed
134+
if (to_rgb)
135+
cv::cvtColor(cropped_img, cropped_img, CV_BGR2RGB);
136+
if (to_bgr)
137+
cv::cvtColor(cropped_img, cropped_img, CV_RGB2BGR);
138+
108139
// adding bbox id
109140
std::string bbox_id = genid(uri, "bbox" + std::to_string(j));
110141
bbox_ids.push_back(bbox_id);

0 commit comments

Comments
 (0)