add exact bilinearResize
This commit is contained in:
		
							parent
							
								
									b5249be51e
								
							
						
					
					
						commit
						d6bacdae62
					
				
					 2 changed files with 64 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -351,3 +351,62 @@ void removeEmptyFrontBackCols(cv::Mat& mat)
 | 
			
		|||
	if(frontRej || backRej) removeEmptyFrontBackCols(mat);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static float linInterpolate(float A, float B, float x)
 | 
			
		||||
{
 | 
			
		||||
	return A * (1.0f - x) + B * x;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <typename Type> static Type resizePointBilinear(const cv::Mat& inImage, float scaledX, float scaledY, 
 | 
			
		||||
														 float xFrac, float yFrac)
 | 
			
		||||
{
 | 
			
		||||
	int u0 = static_cast<int>(scaledX);
 | 
			
		||||
	int v0 = static_cast<int>(scaledY);
 | 
			
		||||
	int u1 = std::min(inImage.cols-1, static_cast<int>(scaledX+1));
 | 
			
		||||
	int v1 = v0;
 | 
			
		||||
	int u2 = u0;
 | 
			
		||||
	int v2 = std::min(inImage.rows-1, static_cast<int>(scaledY+1));
 | 
			
		||||
	int u3 = std::min(inImage.cols-1, static_cast<int>(scaledX+1));
 | 
			
		||||
	int v3 = std::min(inImage.rows-1, static_cast<int>(scaledY+1));
 | 
			
		||||
 | 
			
		||||
	float col0 = linInterpolate(inImage.at<Type>(v0, u0), inImage.at<Type>(v1, u1), xFrac);
 | 
			
		||||
	float col1 = linInterpolate(inImage.at<Type>(v2, u2), inImage.at<Type>(v3, u3), xFrac);
 | 
			
		||||
	float value = linInterpolate(col0, col1, yFrac);
 | 
			
		||||
 | 
			
		||||
	return cv::saturate_cast<Type>(value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <typename Type> static void resize(const cv::Mat& inImage, cv::Mat& outImage) 
 | 
			
		||||
{
 | 
			
		||||
	float scaleY = (inImage.rows - 1) / static_cast<float>(outImage.rows - 1);
 | 
			
		||||
	float scaleX = (inImage.cols - 1) / static_cast<float>(outImage.cols - 1);
 | 
			
		||||
 | 
			
		||||
	for (int y = 0; y < outImage.rows; ++y) 
 | 
			
		||||
	{
 | 
			
		||||
		float scaledY = y * scaleY;
 | 
			
		||||
		float yFrac = scaledY - static_cast<int>(scaledY);
 | 
			
		||||
 | 
			
		||||
		for (int x = 0; x < outImage.cols; x++)
 | 
			
		||||
		{
 | 
			
		||||
			float scaledX = x * scaleX;
 | 
			
		||||
			float xFrac = scaledX - static_cast<int>(scaledX);
 | 
			
		||||
 | 
			
		||||
			outImage.at<Type>(y, x) = resizePointBilinear<Type>(inImage, scaledX, scaledY, xFrac, yFrac);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool bilinearResize(const cv::Mat& inImage, cv::Mat& outImage, const cv::Size size) 
 | 
			
		||||
{
 | 
			
		||||
	if (!(inImage.type() == CV_8U || inImage.type() == CV_32F) ||
 | 
			
		||||
	size.width < 2 || size.height < 2 || inImage.cols < 2 || inImage.rows < 2)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	outImage = cv::Mat::zeros(size.height, size.width, inImage.type());
 | 
			
		||||
 | 
			
		||||
	if(inImage.type() == CV_8U)
 | 
			
		||||
		resize<uint8_t>(inImage, outImage);
 | 
			
		||||
	else if(inImage.type() == CV_32F)
 | 
			
		||||
		resize<float>(inImage, outImage);
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,8 +30,8 @@ std::vector<cv::Point2f>::iterator getBottomRight(std::vector<cv::Point2f>& poin
 | 
			
		|||
double distance(const cv::Point2f& a, const cv::Point2f& b);
 | 
			
		||||
 | 
			
		||||
bool findClosest(size_t& xIndex, size_t& yIndex, 
 | 
			
		||||
				 const cv::Point2f point, const std::vector< std::vector<cv::Point2f> >& array, 
 | 
			
		||||
				 float xTolerance, float yTolerance);
 | 
			
		||||
				const cv::Point2f point, const std::vector< std::vector<cv::Point2f> >& array, 
 | 
			
		||||
				float xTolerance, float yTolerance);
 | 
			
		||||
 | 
			
		||||
bool findClosest(size_t& index, const cv::Point2f point, const std::vector<cv::Point2f>& array, float xTolerance, float yTolerance);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -47,4 +47,7 @@ bool deleteEmptyCols(cv::Mat& mat);
 | 
			
		|||
 | 
			
		||||
void removeEmptyFrontBackCols(cv::Mat& mat);
 | 
			
		||||
 | 
			
		||||
bool bilinearResize(const cv::Mat& inImage, cv::Mat& outImage, cv::Size size);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue