Skip to content

Commit a42f990

Browse files
authored
fix cropped export from preview (#125)
1 parent 552c9a1 commit a42f990

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

tests/stitcher_pipeline_test.cc

+42
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "xpano/algorithm/options.h"
2727
#include "xpano/algorithm/stitcher.h"
2828
#include "xpano/constants.h"
29+
#include "xpano/utils/rect.h"
30+
#include "xpano/utils/vec_opencv.h"
2931

3032
using Catch::Matchers::Equals;
3133
using Catch::Matchers::WithinAbs;
@@ -441,6 +443,46 @@ TEST_CASE("Export [extra results]") {
441443
std::filesystem::remove(tmp_path);
442444
}
443445

446+
TEST_CASE("Export with crop") {
447+
const std::filesystem::path tmp_path =
448+
xpano::tests::TmpPath().replace_extension("png");
449+
450+
xpano::pipeline::StitcherPipeline<kReturnFuture> stitcher;
451+
auto loading_task = stitcher.RunLoading(kInputsWithExifMetadata, {}, {});
452+
auto data = loading_task.future.get();
453+
REQUIRE(data.panos.size() == 1);
454+
455+
auto crop = xpano::utils::Rect(xpano::utils::Ratio2f{0.25f, 0.25f},
456+
xpano::utils::Ratio2f{0.5f, 0.75f});
457+
auto stitch_result = stitcher
458+
.RunStitching(data, {.pano_id = 0,
459+
.export_path = tmp_path,
460+
.export_crop = crop})
461+
.future.get();
462+
463+
const float eps = 0.02;
464+
465+
CHECK(stitch_result.pano.has_value());
466+
CHECK(stitch_result.export_path.has_value());
467+
468+
REQUIRE(std::filesystem::exists(tmp_path));
469+
auto image = cv::imread(tmp_path.string());
470+
REQUIRE(!image.empty());
471+
CHECK_THAT(image.rows, WithinRel(488, eps));
472+
CHECK_THAT(image.cols, WithinRel(334, eps));
473+
474+
auto cv_rect = xpano::utils::GetCvRect(*stitch_result.pano, crop);
475+
auto pano_cropped = (*stitch_result.pano)(cv_rect);
476+
477+
REQUIRE(pano_cropped.rows == image.rows);
478+
REQUIRE(pano_cropped.cols == image.cols);
479+
480+
auto avg_diff = cv::norm(pano_cropped, image);
481+
CHECK(avg_diff <= 1e-6);
482+
483+
std::filesystem::remove(tmp_path);
484+
}
485+
444486
TEST_CASE("ExportWithMetadata") {
445487
const std::filesystem::path tmp_path =
446488
xpano::tests::TmpPath().replace_extension("jpg");

xpano/gui/pano_gui.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,12 @@ void PanoGui::PerformExportAction(int pano_id) {
586586
return;
587587
}
588588

589+
const auto& pano = stitcher_data_->panos.at(pano_id);
589590
if (plot_pane_.Type() == ImageType::kPanoFullRes) {
590591
std::optional<std::filesystem::path> metadata_path;
591592
if (options_.metadata.copy_from_first_image) {
592593
metadata_path = first_image->GetPath();
593594
}
594-
const auto& pano = stitcher_data_->panos.at(pano_id);
595595
stitcher_pipeline_.RunExport(plot_pane_.Image(),
596596
{.pano_id = pano_id,
597597
.export_path = *export_path,
@@ -603,6 +603,7 @@ void PanoGui::PerformExportAction(int pano_id) {
603603
{.pano_id = pano_id,
604604
.full_res = true,
605605
.export_path = *export_path,
606+
.export_crop = pano.crop,
606607
.metadata = options_.metadata,
607608
.compression = options_.compression,
608609
.stitch_algorithm = options_.stitch});

xpano/pipeline/stitcher_pipeline.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ StitchingResult RunStitchingPipeline(
257257
export_path = RunExportPipeline(result,
258258
{.export_path = *options.export_path,
259259
.metadata_path = metadata_path,
260-
.compression = options.compression},
260+
.compression = options.compression,
261+
.crop = options.export_crop},
261262
progress)
262263
.export_path;
263264
}

xpano/pipeline/stitcher_pipeline.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct StitchingOptions {
3131
int pano_id = 0;
3232
bool full_res = false;
3333
std::optional<std::filesystem::path> export_path;
34+
std::optional<utils::RectRRf> export_crop;
3435
MetadataOptions metadata;
3536
CompressionOptions compression;
3637
StitchAlgorithmOptions stitch_algorithm;

xpano/utils/vec_opencv.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <opencv2/core.hpp>
77

8+
#include "xpano/utils/rect.h"
89
#include "xpano/utils/vec.h"
910

1011
namespace xpano::utils {

0 commit comments

Comments
 (0)