%%writefile tmp/fig_02_sudoku2.cpp
#define MM_OUT "tmp/fig_02_sudoku2.png"
#include "morph.hpp"
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <filesystem>
int main() {
// 1. Image saved by the previous cell (sudoku.png, 500×500, gray)
mm::Image img = mm::read("sudoku.png");
// 2. Padding so the grid corners aren't cut off (mm::pad fills with 0)
const int PAD = 60;
mm::Image img_pad = mm::pad(img, PAD);
// 3. Grid corners in the expanded image — TL, TR, BL, BR
std::vector<std::array<double, 2>> pts1 = {{100, 160}, {390, 45}, {200, 580}, {570, 420}};
// 4. Destination corners: front view SIZE×SIZE
const int SIZE = 500;
std::vector<std::array<double, 2>> pts2 = {{0, 0}, {SIZE, 0}, {0, SIZE}, {SIZE, SIZE}};
// 5. Homography pts1 -> pts2 and rectification
mm::Image img_rect = mm::perspective_transform(img_pad, pts1, pts2, SIZE, SIZE);
// 6. Display
mm::show(
std::vector<mm::Image>{img_pad, img_rect},
MM_OUT,
std::vector<std::string>{"Original (with padding)", "Rectified front view"},
2
);
// [pdi:panel-io] auto-generated — do not edit by hand
std::filesystem::create_directories("tmp");
mm::write(img_pad, "tmp/fig_02_sudoku2_0.png");
mm::write(img_rect, "tmp/fig_02_sudoku2_1.png");
// [pdi:panel-io:end]
return 0;
}