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) if(config.simpleStich)
out = simpleStich(remapedImages); out = simpleStich(remapedImages);
else else
out = stich(remapedImages); out = stich(remapedImages, true);
if(config.verbose) if(config.verbose)
{ {

View File

@ -328,30 +328,27 @@ bool deleteEmptyCols(cv::Mat& mat)
return false; return false;
} }
void removeEmptyFrontBackCols(cv::Mat& mat) void removeSparseCols(cv::Mat& mat, bool front)
{ {
assert(mat.type() == CV_32FC1); assert(mat.type() == CV_32FC1);
int front = 0; int count = 0;
int back = 0;
for(int y = 0; y < mat.rows; ++y) for(int y = 0; y < mat.rows; ++y)
{ {
if(mat.at<float>(y,0) >= 0) ++front; if(front && mat.at<float>(y,0) >= 0) ++count;
if(mat.at<float>(y,mat.cols-1) >= 0) ++back; else if(mat.at<float>(y,mat.cols-1) >= 0) ++count;
} }
Log(Log::DEBUG)<<__func__<<" front: "<<front<<" back "<<back;
cv::Rect roi; cv::Rect roi;
bool frontRej = (front < mat.rows/2); bool rej = (count < mat.rows/2);
bool backRej = (back < mat.rows/2); roi.x=front ? rej : 0;
roi.x=frontRej;
roi.y=0; roi.y=0;
roi.width = mat.cols - backRej - frontRej; roi.width = mat.cols - rej;
roi.height = mat.rows; roi.height = mat.rows;
mat = mat(roi); mat = mat(roi);
if(frontRej || backRej) removeEmptyFrontBackCols(mat); if(rej)
removeSparseCols(mat, front);
} }
static float linInterpolate(float A, float B, float x) static float linInterpolate(float A, float B, float x)
{ {
return A * (1.0f - x) + B * 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 u0 = static_cast<int>(scaledX);
int v0 = static_cast<int>(scaledY); 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 v1 = v0;
int u2 = u0; int u2 = u0;
int v2 = 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 u3 = std::min(inImage.cols-1, static_cast<int>(scaledX)+1);
int v3 = std::min(inImage.rows-1, static_cast<int>(scaledY+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 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 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++) for (int x = 0; x < outImage.cols; x++)
{ {
float scaledX = x * scaleX; 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); 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) size.width < 2 || size.height < 2 || inImage.cols < 2 || inImage.rows < 2)
return false; return false;
std::cout<<"size: "<<size.height<<' '<<size.width<<'\n';
outImage = cv::Mat::zeros(size.height, size.width, inImage.type()); outImage = cv::Mat::zeros(size.height, size.width, inImage.type());
if(inImage.type() == CV_8U) 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); 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); 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); sortIntoRemapMaps(points, out.xMat, out.yMat, out.topLeftCoordinate);
Log(Log::DEBUG)<<__func__<<": xMat raw\n"<<out.xMat; Log(Log::DEBUG)<<__func__<<": xMat raw\n"<<out.xMat;
removeEmptyFrontBackCols(out.xMat);
removeEmptyFrontBackCols(out.yMat); int cols = out.xMat.cols;
deleteEmptyCols(out.xMat); removeSparseCols(out.xMat, true);
deleteEmptyCols(out.yMat); 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; Log(Log::DEBUG)<<__func__<<": xMat rejcted\n"<<out.xMat;
fillMissing(out.xMat); fillMissing(out.xMat);
Log(Log::DEBUG)<<__func__<<": xMat filled\n"<<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.xMat, xMapResized, outputSize, cv::INTER_LINEAR);
cv::resize(map.yMat, yMapResized, outputSize, cv::INTER_LINEAR); cv::resize(map.yMat, yMapResized, outputSize, cv::INTER_LINEAR);
cv::Rect roi; cv::Rect noBorderRoi(map.outputCellSize/2, map.outputCellSize/2,
cv::Mat xMapRed; outputSize.width-map.outputCellSize, outputSize.height-map.outputCellSize);
cv::Mat yMapRed; cv::Mat xMapRed = xMapResized(noBorderRoi);
if(findDeadSpace(xMapResized, roi)) cv::Mat yMapRed = yMapResized(noBorderRoi);
cv::Rect deadRoi;
if(findDeadSpace(xMapResized, deadRoi))
{ {
xMapRed = xMapResized(roi); xMapRed = yMapRed(deadRoi);
yMapRed = yMapResized(roi); yMapRed = yMapRed(deadRoi);
}
else
{
xMapRed = xMapResized;
yMapRed = yMapResized;
} }
cv::remap(image, out.image, xMapRed, yMapRed, cv::INTER_LINEAR); cv::remap(image, out.image, xMapRed, yMapRed, cv::INTER_LINEAR);
out.origin = cv::Point2i(map.topLeftCoordinate.x*map.outputCellSize, map.topLeftCoordinate.y*map.outputCellSize); out.origin = cv::Point2i(map.topLeftCoordinate.x*map.outputCellSize, map.topLeftCoordinate.y*map.outputCellSize);