我在黑色背景上有圖像沒有方形邊緣(見下圖右下方)。我想將它們裁剪成最大的矩形圖像(紅色邊框)。我知道我可能會失去原來的形象。是否有可能在Python中使用OpenCV來完成此操作。我知道有一些功能可以裁剪出一個輪廓的邊框,但這仍然會讓我在黑色的背景中出現。如何裁剪OpenCV中最大的內部邊框?
12
A
回答
16
好了,我打了一個想法,測試它(它的C++但你可能就可以將其轉換成蟒蛇):
- 假設:背景是黑色,內部沒有黑色邊框部分
- 你可以找到
findContours
- 使用最小值/最大值從輪廓X/Y點位置的外部輪廓,直到由這些點建立的矩形不包含點t1帽子躺在輪廓之外
我不能保證此方法總能找到「最好」的內部框,但我使用啓發式方法來選擇矩形在頂部/底部/左側/右側是否縮小。
代碼當然可以被優化,太;
以此作爲testimage),我得到的結果(非紅色區域是所找到的內部矩形):
認爲在右上角有一個像素不應該包含在矩形中,也許這是從引出/繪製輪廓錯誤?!?
和這裏的代碼:
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
感謝分享!對我很好 –
相關問題
- 1. 使用OpenCV裁剪最大的矩形
- 2. 基於最大四邊形輪廓的裁剪圖像OpenCV
- 3. 如何在CSS3圓形邊框處裁剪內部HTML元素?
- 4. WPF剪裁邊框
- 5. Python + OpenCV =如何裁剪圓?
- 6. 用OpenCV裁剪黑色邊緣
- 7. OpenGL中的內置多邊形裁剪
- 8. 刪除頂部邊框的TabBar而不會裁剪到邊界
- 9. OpenCV裁剪矩形
- 10. 在opencv中裁剪圖像
- 11. 如何在imagemagick中裁剪邊距?
- 12. 使用邊框的裁剪圖像
- 13. 使用PIL裁剪圖像的邊框
- 14. 邊框周圍的未知裁剪
- 15. 裁剪一個非常大的邊框半徑的圖像
- 16. 如何在Java中自動裁剪圖像白色邊框?
- 17. CSS3邊框半徑裁剪髮出
- 18. 如何在裁剪之前裁剪大尺寸圖像而不裁剪?
- 19. OpenCV裁剪圖像失真
- 20. 用OpenCV裁剪圖像
- 21. 使用openCV裁剪圖像
- 22. OpenCV cvRemap裁剪圖像
- 23. 邊框懸停和裁剪元素內的圖像
- 24. 裁剪大jpeg
- 25. 如何使用GraphicsMagick裁剪/調整圖像的背景/邊框大小
- 26. OpenCV 2.4.3(iOS)中的裁剪墊圖像
- 27. 如何減少iphone中的裁剪圖像框大小?
- 28. 如何爲滑動的圓形裁剪圖像製作邊框
- 29. 複雜的剪裁邊界
- 30. 將圖像裁剪至最大居中部分,比例爲
更重要的是,你能告訴除了 '圖片不具有直角邊緣'? 它是否總是像這個例子 - 你需要從底部和右邊裁剪? (在這種情況下,它非常簡單) –
圖像(無黑色區域)可以具有任何形狀,我想將它轉換爲矩形,同時丟失最小量的圖像。 – nickponline
@nickponline你有沒有找到解決方案? – Micka