22#include <opencv2/opencv.hpp>
36cv::Mat to_owning_mat(
const ImageBuffer& b) {
37 return cv::Mat(b.height, b.width, CV_8UC(b.channels),
const_cast<std::uint8_t*
>(b.data))
70 throw std::invalid_argument(
71 "NcorrSession::set_reference: invalid ImageBuffer (null data or "
72 "non-positive dimensions).");
74 impl_->ref_img =
Image2D(to_owning_mat(ref));
75 impl_->ref_width = ref.width;
76 impl_->ref_height = ref.height;
77 impl_->has_reference =
true;
79 impl_->has_roi =
false;
83 if (!roi_mask.
valid()) {
84 throw std::invalid_argument(
"NcorrSession::set_roi: invalid ROI mask ImageBuffer.");
86 if (impl_->has_reference &&
87 (roi_mask.
width != impl_->ref_width || roi_mask.
height != impl_->ref_height)) {
88 throw std::invalid_argument(
89 "NcorrSession::set_roi: ROI mask geometry does not match the "
93 impl_->roi =
ROI2D(gs > 0.5);
94 impl_->has_roi =
true;
98 if (!impl_->has_reference) {
99 throw std::logic_error(
100 "NcorrSession::process_frame: no reference frame set; call "
101 "set_reference() first.");
105 result.
valid =
false;
108 result.
message =
"process_frame: invalid deformed ImageBuffer.";
111 if (def.
width != impl_->ref_width || def.
height != impl_->ref_height) {
112 result.
message =
"process_frame: deformed frame geometry does not match reference.";
117 Image2D def_img(to_owning_mat(def));
119 ROI2D roi = impl_->has_roi
123 std::vector<Image2D> imgs{impl_->ref_img, def_img};
127 impl_->config.subregion_radius, impl_->config.num_threads,
131 if (out.
disps.empty()) {
132 result.
message =
"process_frame: DIC produced no displacement fields.";
142 const int h = u_arr.
height();
143 const int w = u_arr.
width();
147 const double nan = std::numeric_limits<double>::quiet_NaN();
148 const std::size_t n =
static_cast<std::size_t
>(w) * h;
149 result.
u.assign(n, nan);
150 result.
v.assign(n, nan);
153 for (
int i = 0; i < h; ++i) {
154 for (
int j = 0; j < w; ++j) {
155 if (i < mask.
height() && j < mask.
width() && mask(i, j)) {
156 const std::size_t idx =
static_cast<std::size_t
>(i) * w + j;
157 if (i < u_arr.
height() && j < u_arr.
width()) result.
u[idx] = u_arr(i, j);
158 if (i < v_arr.
height() && j < v_arr.
width()) result.
v[idx] = v_arr(i, j);
160 result.
corrcoef[idx] = cc_arr(i, j);
168 }
catch (
const std::exception& e) {
169 result.
valid =
false;
170 result.
message = std::string(
"process_frame: DIC failed: ") + e.what();
176 return impl_->has_reference;
difference_type width() const
difference_type height() const
const Array2D< double > & get_array() const
const Data2D & get_u() const
const ROI2D & get_roi() const
const Data2D & get_v() const
const Data2D & get_cc() const
Array2D< double > get_gs() const
Drives an in-memory Digital Image Correlation session.
void set_roi(const ImageBuffer &roi_mask)
Optionally supply a region-of-interest mask.
~NcorrSession()
Destructor (defined in session.cpp because of the PIMPL).
bool has_reference() const
DICResult process_frame(const ImageBuffer &def)
Push a deformed frame and run DIC against the reference.
NcorrSession(const SessionConfig &config=SessionConfig())
Construct a session with the given configuration.
const Array2D< bool > & get_mask() const
@ QUINTIC_BSPLINE_PRECOMPUTE
DIC_analysis_output DIC_analysis(const DIC_analysis_input &)
In-memory DIC session API for CppNCorr.
Result of running DIC on a single deformed frame.
int height
Height of the displacement fields, in reduced-grid samples.
std::vector< double > v
Vertical Lagrangian displacement (v), pixels, row-major. NaN outside ROI.
bool valid
True if the frame was processed successfully.
int width
Width of the displacement fields, in reduced-grid samples.
std::vector< double > u
Horizontal Lagrangian displacement (u), pixels, row-major. NaN outside ROI.
std::vector< double > corrcoef
Per-point correlation coefficient, row-major, size width*height. NaN outside ROI.
std::string message
Human-readable status / error message (empty on success).
std::vector< Disp2D > disps
Thin, non-owning view over a raw image in memory.
int width
Image width in pixels.
int height
Image height in pixels.
Impl(const SessionConfig &cfg)
Configuration for an in-memory DIC session.