remove border more robustly

This commit is contained in:
2021-07-01 15:43:35 +02:00
parent f4f2dc3507
commit 6910754a9f
4 changed files with 33 additions and 34 deletions

View File

@ -241,7 +241,7 @@ int perfromOperation(int operation, char** fileNames, const Config& config)
if(config.simpleStich)
out = simpleStich(remapedImages);
else
out = stich(remapedImages);
out = stich(remapedImages, true);
if(config.verbose)
{

View File

@ -328,30 +328,27 @@ bool deleteEmptyCols(cv::Mat& mat)
return false;
}
void removeEmptyFrontBackCols(cv::Mat& mat)
void removeSparseCols(cv::Mat& mat, bool front)
{
assert(mat.type() == CV_32FC1);
int front = 0;
int back = 0;
int count = 0;
for(int y = 0; y < mat.rows; ++y)
{
if(mat.at<float>(y,0) >= 0) ++front;
if(mat.at<float>(y,mat.cols-1) >= 0) ++back;
if(front && mat.at<float>(y,0) >= 0) ++count;
else if(mat.at<float>(y,mat.cols-1) >= 0) ++count;
}
Log(Log::DEBUG)<<__func__<<" front: "<<front<<" back "<<back;
cv::Rect roi;
bool frontRej = (front < mat.rows/2);
bool backRej = (back < mat.rows/2);
roi.x=frontRej;
bool rej = (count < mat.rows/2);
roi.x=front ? rej : 0;
roi.y=0;
roi.width = mat.cols - backRej - frontRej;
roi.width = mat.cols - rej;
roi.height = mat.rows;
mat = mat(roi);
if(frontRej || backRej) removeEmptyFrontBackCols(mat);
if(rej)
removeSparseCols(mat, front);
}
static float linInterpolate(float A, float B, float x)
{
return A * (1.0f - x) + B * x;
@ -362,12 +359,12 @@ template <typename Type> static Type resizePointBilinear(const cv::Mat& inImage,
{
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 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));
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);
@ -389,7 +386,7 @@ template <typename Type> static void resize(const cv::Mat& inImage, cv::Mat& out
for (int x = 0; x < outImage.cols; x++)
{
float scaledX = x * scaleX;
float xFrac = scaledX - static_cast<int>(scaledX);
float xFrac = scaledX - (static_cast<int>(scaledX));
outImage.at<Type>(y, x) = resizePointBilinear<Type>(inImage, scaledX, scaledY, xFrac, yFrac);
}
@ -402,6 +399,8 @@ bool bilinearResize(const cv::Mat& inImage, cv::Mat& outImage, const cv::Size si
size.width < 2 || size.height < 2 || inImage.cols < 2 || inImage.rows < 2)
return false;
std::cout<<"size: "<<size.height<<' '<<size.width<<'\n';
outImage = cv::Mat::zeros(size.height, size.width, inImage.type());
if(inImage.type() == CV_8U)

View File

@ -45,7 +45,7 @@ bool findDeadSpace(const cv::Mat& mat, cv::Rect& roi);
bool deleteEmptyCols(cv::Mat& mat);
void removeEmptyFrontBackCols(cv::Mat& mat);
void removeSparseCols(cv::Mat& mat, bool front);
bool bilinearResize(const cv::Mat& inImage, cv::Mat& outImage, cv::Size size);

View File

@ -95,10 +95,13 @@ bool createRemapMap(const cv::Mat& image, RemapMap& out, const std::vector<Detec
sortIntoRemapMaps(points, out.xMat, out.yMat, out.topLeftCoordinate);
Log(Log::DEBUG)<<__func__<<": xMat raw\n"<<out.xMat;
removeEmptyFrontBackCols(out.xMat);
removeEmptyFrontBackCols(out.yMat);
deleteEmptyCols(out.xMat);
deleteEmptyCols(out.yMat);
int cols = out.xMat.cols;
removeSparseCols(out.xMat, true);
out.topLeftCoordinate.x += cols-out.xMat.cols;
removeSparseCols(out.xMat, false);
removeSparseCols(out.yMat, true);
removeSparseCols(out.yMat, false);
Log(Log::DEBUG)<<__func__<<": xMat rejcted\n"<<out.xMat;
fillMissing(out.xMat);
Log(Log::DEBUG)<<__func__<<": xMat filled\n"<<out.xMat;
@ -165,18 +168,15 @@ RemapedImage applyRemap(const cv::Mat& image, const RemapMap &map)
cv::resize(map.xMat, xMapResized, outputSize, cv::INTER_LINEAR);
cv::resize(map.yMat, yMapResized, outputSize, cv::INTER_LINEAR);
cv::Rect roi;
cv::Mat xMapRed;
cv::Mat yMapRed;
if(findDeadSpace(xMapResized, roi))
cv::Rect noBorderRoi(map.outputCellSize/2, map.outputCellSize/2,
outputSize.width-map.outputCellSize, outputSize.height-map.outputCellSize);
cv::Mat xMapRed = xMapResized(noBorderRoi);
cv::Mat yMapRed = yMapResized(noBorderRoi);
cv::Rect deadRoi;
if(findDeadSpace(xMapResized, deadRoi))
{
xMapRed = xMapResized(roi);
yMapRed = yMapResized(roi);
}
else
{
xMapRed = xMapResized;
yMapRed = yMapResized;
xMapRed = yMapRed(deadRoi);
yMapRed = yMapRed(deadRoi);
}
cv::remap(image, out.image, xMapRed, yMapRed, cv::INTER_LINEAR);
out.origin = cv::Point2i(map.topLeftCoordinate.x*map.outputCellSize, map.topLeftCoordinate.y*map.outputCellSize);