Resizing/scale rectangle

For anyone facing a similar problem; the issue was in my comprehension of the geometric aspects of things. If you scale down by 50% and then try to scale back up, you’re not trying to scale by 50% but rather by 200%. Moreover (xw,yh) ends up being the same as (xW,yH)-- this actually makes calculation simpler.

The following I’ve tested on a variety of scales and seems to work (for scaling up). YMMV.

inline double
percent_difference(const std::size_t larger, const std::size_t smaller)
{
	const std::size_t	difference(larger - smaller);
	const double		retval(double(difference) / double(larger));

	return retval;
}

inline double
scale_factor(const double percent)
{
	return (1.0 / percent);
}

[...]
const double	height_percent(1 - percent_difference(f->size().height, frame.size().height));
const double	width_percent(1 - percent_difference(f->size().width, frame.size().width));

	roi.y		*= scale_factor(height_percent);
	roi.x		*= scale_factor(width_percent); 
	roi.height	*= scale_factor(height_percent);  
	roi.width	*= scale_factor(width_percent); 

	/* There is an assertion inside of opencv which will crash the program 
	 * if we don't handle it here. 
	 */
	if (0 > roi.x || 0 > roi.width || roi.x + roi.width > f->cols ||
		0 > roi.y || 0 > roi.height || roi.y + roi.height > f->rows) {
		qDebug() << "ASSERTION FAILED";
	return new cv::Mat(f->clone());
}
			
frame = (*f)(roi).clone();

I’d also suggest if you have this sort of complication trying out one of the many various math forums where the people trying to help don’t have an inherent conflict of interest in that they’re using their status to try to make money, but again YMMV.