Skip to content

Commit

Permalink
fix cropped export from preview (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
krupkat committed Feb 11, 2024
1 parent 552c9a1 commit a42f990
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
42 changes: 42 additions & 0 deletions tests/stitcher_pipeline_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "xpano/algorithm/options.h"
#include "xpano/algorithm/stitcher.h"
#include "xpano/constants.h"
#include "xpano/utils/rect.h"
#include "xpano/utils/vec_opencv.h"

using Catch::Matchers::Equals;
using Catch::Matchers::WithinAbs;
Expand Down Expand Up @@ -441,6 +443,46 @@ TEST_CASE("Export [extra results]") {
std::filesystem::remove(tmp_path);
}

TEST_CASE("Export with crop") {
const std::filesystem::path tmp_path =
xpano::tests::TmpPath().replace_extension("png");

xpano::pipeline::StitcherPipeline<kReturnFuture> stitcher;
auto loading_task = stitcher.RunLoading(kInputsWithExifMetadata, {}, {});
auto data = loading_task.future.get();
REQUIRE(data.panos.size() == 1);

auto crop = xpano::utils::Rect(xpano::utils::Ratio2f{0.25f, 0.25f},
xpano::utils::Ratio2f{0.5f, 0.75f});
auto stitch_result = stitcher
.RunStitching(data, {.pano_id = 0,
.export_path = tmp_path,
.export_crop = crop})
.future.get();

const float eps = 0.02;

CHECK(stitch_result.pano.has_value());
CHECK(stitch_result.export_path.has_value());

REQUIRE(std::filesystem::exists(tmp_path));
auto image = cv::imread(tmp_path.string());
REQUIRE(!image.empty());
CHECK_THAT(image.rows, WithinRel(488, eps));
CHECK_THAT(image.cols, WithinRel(334, eps));

auto cv_rect = xpano::utils::GetCvRect(*stitch_result.pano, crop);
auto pano_cropped = (*stitch_result.pano)(cv_rect);

REQUIRE(pano_cropped.rows == image.rows);
REQUIRE(pano_cropped.cols == image.cols);

auto avg_diff = cv::norm(pano_cropped, image);
CHECK(avg_diff <= 1e-6);

std::filesystem::remove(tmp_path);
}

TEST_CASE("ExportWithMetadata") {
const std::filesystem::path tmp_path =
xpano::tests::TmpPath().replace_extension("jpg");
Expand Down
3 changes: 2 additions & 1 deletion xpano/gui/pano_gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,12 @@ void PanoGui::PerformExportAction(int pano_id) {
return;
}

const auto& pano = stitcher_data_->panos.at(pano_id);
if (plot_pane_.Type() == ImageType::kPanoFullRes) {
std::optional<std::filesystem::path> metadata_path;
if (options_.metadata.copy_from_first_image) {
metadata_path = first_image->GetPath();
}
const auto& pano = stitcher_data_->panos.at(pano_id);
stitcher_pipeline_.RunExport(plot_pane_.Image(),
{.pano_id = pano_id,
.export_path = *export_path,
Expand All @@ -603,6 +603,7 @@ void PanoGui::PerformExportAction(int pano_id) {
{.pano_id = pano_id,
.full_res = true,
.export_path = *export_path,
.export_crop = pano.crop,
.metadata = options_.metadata,
.compression = options_.compression,
.stitch_algorithm = options_.stitch});
Expand Down
3 changes: 2 additions & 1 deletion xpano/pipeline/stitcher_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ StitchingResult RunStitchingPipeline(
export_path = RunExportPipeline(result,
{.export_path = *options.export_path,
.metadata_path = metadata_path,
.compression = options.compression},
.compression = options.compression,
.crop = options.export_crop},
progress)
.export_path;
}
Expand Down
1 change: 1 addition & 0 deletions xpano/pipeline/stitcher_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct StitchingOptions {
int pano_id = 0;
bool full_res = false;
std::optional<std::filesystem::path> export_path;
std::optional<utils::RectRRf> export_crop;
MetadataOptions metadata;
CompressionOptions compression;
StitchAlgorithmOptions stitch_algorithm;
Expand Down
1 change: 1 addition & 0 deletions xpano/utils/vec_opencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <opencv2/core.hpp>

#include "xpano/utils/rect.h"
#include "xpano/utils/vec.h"

namespace xpano::utils {
Expand Down

0 comments on commit a42f990

Please sign in to comment.