如何在OpenCV中裁剪最大的内部边界框?

22

我有一些在黑色背景上的图像,而这些图像没有方形边缘(请参见下面图像的右下角)。我想将它们裁剪成最大的矩形图像(红色边框)。我知道我可能会失去原始图像的一部分。是否可以在OpenCV中使用Python完成此操作?我知道有可以裁剪轮廓边界框的函数,但那仍然会在某些地方留下黑色背景。

enter image description here


除了“图像没有方形边缘”之外,您还能说些什么? 它是否总是像示例中那样 - 需要从底部和右侧进行裁剪?(在这种情况下,它非常简单) - Eran W
图像(非黑色区域)可以有任何形状,我想将其转换为矩形,同时尽可能少地损失图像。 - nickponline
2
@nickponline 你找到解决方案了吗? - Micka
4个回答

17

好的,我有一个想法并测试了它(这是c++代码,但你可能能够将其转换为python):

  1. 假设:背景是黑色的,内部没有黑色边界部分
  2. 您可以使用findContours找到外轮廓
  3. 使用该轮廓中的最小/最大x/y点位置,直到由这些点构建的矩形不包含任何在轮廓之外的点

我无法保证此方法始终找到“最佳”内部框,但我使用一种启发式方法来选择矩形是否在顶部/底部/左侧/右侧缩小。

当然,代码也可以进行优化;)

使用此作为测试图像,我得到了以下结果(非红色区域是找到的内部矩形):

enter image description here

enter image description here

请注意,右上角有一个像素不应包含在矩形中,也许是从错误地提取/绘制轮廓中来的?!?

以下是代码:

cv::Mat input = cv::imread("LenaWithBG.png");

cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);

cv::imshow("gray", gray);

// extract all the black background (and some interior parts maybe)
cv::Mat mask = gray>0;
cv::imshow("mask", mask);

// now extract the outer contour
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;

cv::findContours(mask,contours,hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cv::Point(0,0));

std::cout << "found contours: " << contours.size() << std::endl;


cv::Mat contourImage = cv::Mat::zeros( input.size(), CV_8UC3 );;

//find contour with max elements
// remark: in theory there should be only one single outer contour surrounded by black regions!!

unsigned int maxSize = 0;
unsigned int id = 0;
for(unsigned int i=0; i<contours.size(); ++i)
{
    if(contours.at(i).size() > maxSize)
    {
        maxSize = contours.at(i).size();
        id = i;
    }
}

std::cout << "chosen id: " << id << std::endl;
std::cout << "max size: " << maxSize << std::endl;

/// Draw filled contour to obtain a mask with interior parts
cv::Mat contourMask = cv::Mat::zeros( input.size(), CV_8UC1 );
cv::drawContours( contourMask, contours, id, cv::Scalar(255), -1, 8, hierarchy, 0, cv::Point() );
cv::imshow("contour mask", contourMask);

// sort contour in x/y directions to easily find min/max and next
std::vector<cv::Point> cSortedX = contours.at(id);
std::sort(cSortedX.begin(), cSortedX.end(), sortX);

std::vector<cv::Point> cSortedY = contours.at(id);
std::sort(cSortedY.begin(), cSortedY.end(), sortY);


unsigned int minXId = 0;
unsigned int maxXId = cSortedX.size()-1;

unsigned int minYId = 0;
unsigned int maxYId = cSortedY.size()-1;

cv::Rect interiorBB;

while( (minXId<maxXId)&&(minYId<maxYId) )
{
    cv::Point min(cSortedX[minXId].x, cSortedY[minYId].y);
    cv::Point max(cSortedX[maxXId].x, cSortedY[maxYId].y);

    interiorBB = cv::Rect(min.x,min.y, max.x-min.x, max.y-min.y);

// out-codes: if one of them is set, the rectangle size has to be reduced at that border
    int ocTop = 0;
    int ocBottom = 0;
    int ocLeft = 0;
    int ocRight = 0;

    bool finished = checkInteriorExterior(contourMask, interiorBB, ocTop, ocBottom,ocLeft, ocRight);
    if(finished)
    {
        break;
    }

// reduce rectangle at border if necessary
    if(ocLeft)++minXId;
    if(ocRight) --maxXId;

    if(ocTop) ++minYId;
    if(ocBottom)--maxYId;


}

std::cout <<  "done! : " << interiorBB << std::endl;

cv::Mat mask2 = cv::Mat::zeros(input.rows, input.cols, CV_8UC1);
cv::rectangle(mask2,interiorBB, cv::Scalar(255),-1);

cv::Mat maskedImage;
input.copyTo(maskedImage);
for(unsigned int y=0; y<maskedImage.rows; ++y)
    for(unsigned int x=0; x<maskedImage.cols; ++x)
    {
        maskedImage.at<cv::Vec3b>(y,x)[2] = 255;
    }
input.copyTo(maskedImage,mask2);

cv::imshow("masked image", maskedImage);
cv::imwrite("interiorBoundingBoxResult.png", maskedImage);

使用减少函数:

bool checkInteriorExterior(const cv::Mat&mask, const cv::Rect&interiorBB, int&top, int&bottom, int&left, int&right)
{
// return true if the rectangle is fine as it is!
bool returnVal = true;

cv::Mat sub = mask(interiorBB);

unsigned int x=0;
unsigned int y=0;

// count how many exterior pixels are at the
unsigned int cTop=0; // top row
unsigned int cBottom=0; // bottom row
unsigned int cLeft=0; // left column
unsigned int cRight=0; // right column
// and choose that side for reduction where mose exterior pixels occured (that's the heuristic)

for(y=0, x=0 ; x<sub.cols; ++x)
{
    // if there is an exterior part in the interior we have to move the top side of the rect a bit to the bottom
    if(sub.at<unsigned char>(y,x) == 0)
    {
        returnVal = false;
        ++cTop;
    }
}

for(y=sub.rows-1, x=0; x<sub.cols; ++x)
{
    // if there is an exterior part in the interior we have to move the bottom side of the rect a bit to the top
    if(sub.at<unsigned char>(y,x) == 0)
    {
        returnVal = false;
        ++cBottom;
    }
}

for(y=0, x=0 ; y<sub.rows; ++y)
{
    // if there is an exterior part in the interior
    if(sub.at<unsigned char>(y,x) == 0)
    {
        returnVal = false;
        ++cLeft;
    }
}

for(x=sub.cols-1, y=0; y<sub.rows; ++y)
{
    // if there is an exterior part in the interior
    if(sub.at<unsigned char>(y,x) == 0)
    {
        returnVal = false;
        ++cRight;
    }
}

// that part is ugly and maybe not correct, didn't check whether all possible combinations are handled. Check that one please. The idea is to set `top = 1` iff it's better to reduce the rect at the top than anywhere else.
if(cTop > cBottom)
{
    if(cTop > cLeft)
        if(cTop > cRight)
            top = 1;
}
else
    if(cBottom > cLeft)
        if(cBottom > cRight)
            bottom = 1;

if(cLeft >= cRight)
{
    if(cLeft >= cBottom)
        if(cLeft >= cTop)
            left = 1;
}
else
    if(cRight >= cTop)
        if(cRight >= cBottom)
            right = 1;



return returnVal;
}

bool sortX(cv::Point a, cv::Point b)
{
    bool ret = false;
    if(a.x == a.x)
        if(b.x==b.x)
            ret = a.x < b.x;

    return ret;
}

bool sortY(cv::Point a, cv::Point b)
{
    bool ret = false;
    if(a.y == a.y)
        if(b.y == b.y)
            ret = a.y < b.y;


    return ret;
}

1
该算法不正确。例如在以下多边形中失败,并返回零高度包围框:{x=186 y=205 } {x=464 y=43 } {x=378 y=427 } {x=3 y=325 } - MiB_Coder
@MiB_Coder 对于这样的轮廓,启发式算法根本无法工作。 - Micka
也许你也可以看一下这个:https://dev59.com/J3E95IYBdhLWcg3wDpmg%c3%97n-binary-matrix - Micka

3

受@micka答案启发,写了这个Python解决方案。

这不是一个聪明的解决方案,可以进行优化,但在我的情况下它能够(慢慢地)工作。

我修改了你的图像,添加了一个正方形,就像你的示例中一样:请参见there

最终,这段代码将裁剪出这个picture中的白色矩形。

希望对你有所帮助!

import cv2

# Import your picture
input_picture = cv2.imread("LenaWithBG.png")

# Color it in gray
gray = cv2.cvtColor(input_picture, cv2.COLOR_BGR2GRAY)

# Create our mask by selecting the non-zero values of the picture
ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)

# Select the contour
mask , cont, _ = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# if your mask is incurved or if you want better results, 
# you may want to use cv2.CHAIN_APPROX_NONE instead of cv2.CHAIN_APPROX_SIMPLE, 
# but the rectangle search will be longer

cv2.drawContours(gray, cont, -1, (255,0,0), 1)
cv2.imshow("Your picture with contour", gray)
cv2.waitKey(0)

# Get all the points of the contour
contour = cont[0].reshape(len(cont[0]),2)

# we assume a rectangle with at least two points on the contour gives a 'good enough' result
# get all possible rectangles based on this hypothesis
rect = []

for i in range(len(contour)):
    x1, y1 = contour[i]
    for j in range(len(contour)):
        x2, y2 = contour[j]
        area = abs(y2-y1)*abs(x2-x1)
        rect.append(((x1,y1), (x2,y2), area))

# the first rect of all_rect has the biggest area, so it's the best solution if he fits in the picture
all_rect = sorted(rect, key = lambda x : x[2], reverse = True)

# we take the largest rectangle we've got, based on the value of the rectangle area
# only if the border of the rectangle is not in the black part

# if the list is not empty
if all_rect:
    
    best_rect_found = False
    index_rect = 0
    nb_rect = len(all_rect)
    
    # we check if the rectangle is  a good solution
    while not best_rect_found and index_rect < nb_rect:
        
        rect = all_rect[index_rect]
        (x1, y1) = rect[0]
        (x2, y2) = rect[1]
        
        valid_rect = True
        
        # we search a black area in the perimeter of the rectangle (vertical borders)
        x = min(x1, x2)
        while x <max(x1,x2)+1 and valid_rect:
            if mask[y1,x] == 0 or mask[y2,x] == 0:
                # if we find a black pixel, that means a part of the rectangle is black
                # so we don't keep this rectangle
                valid_rect = False
            x+=1
        
        y = min(y1, y2)
        while y <max(y1,y2)+1 and valid_rect:
            if mask[y,x1] == 0 or mask[y,x2] == 0:
                valid_rect = False
            y+=1
            
        if valid_rect:
            best_rect_found = True
        
        index_rect+=1
        
    if best_rect_found:
        
        cv2.rectangle(gray, (x1,y1), (x2,y2), (255,0,0), 1)
        cv2.imshow("Is that rectangle ok?",gray)
        cv2.waitKey(0)

        # Finally, we crop the picture and store it
        result = input_picture[min(y1, y2):max(y1, y2), min(x1,x2):max(x1,x2)]

        cv2.imwrite("Lena_cropped.png",result)
    else:
        print("No rectangle fitting into the area")
    
else:
    print("No rectangle found")

如果您的掩模是内凹的,或者只是为了获得更好的结果,您可能需要使用cv2.CHAIN_APPROX_NONE代替cv2.CHAIN_APPROX_SIMPLE,但矩形搜索将需要更多时间(因为最好情况下它是一个二次解决方案)。

1
在ImageMagick 6.9.10-30(或7.0.8.30)或更高版本中,您可以使用新定义的-trim函数。
输入: {{link1:enter image description here}}
convert image.png -fuzz 5% -define trim:percent-background=0% -trim +repage result.png


enter image description here

或者对于下面呈现的图像:

输入:

enter image description here

convert image2.png -bordercolor black -border 1 -define trim:percent-background=0% -trim +repage result2.png


enter image description here


0
你可以使用largestinteriorrectangle包来计算最大的内部边界框。它们以[x, y, width, height]的形式呈现。
要裁剪内部边界框,请使用以下代码。
import cv2 as cv
import numpy as np
import largestinteriorrectangle as lir

img = cv.imread("LenaWithBG.png")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
_, mask = cv.threshold(gray, 0, 255, cv.THRESH_BINARY)
contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

contour = np.array([contours[0][:, 0, :]])
inner_bb = lir.lir(contour)

cropped_img = img[inner_bb[1]:inner_bb[1] + inner_bb[3],
                  inner_bb[0]:inner_bb[0] + inner_bb[2]]

让我们来绘制一下:
cv.imshow('Lena', img)

enter image description here

plot = img.copy()
cv.polylines(plot, [contour], True, (0, 0, 255))
cv.rectangle(plot, lir.pt1(inner_bb), lir.pt2(inner_bb), (255, 0, 0))

cv.imshow('Inner Bounding Box', plot)
cv.waitKey(0)
cv.destroyAllWindows()

enter image description here

cv.imshow('Cropped Image', cropped_img)
cv.waitKey(0)
cv.destroyAllWindows()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接