【发布时间】:2018-09-29 03:34:16
【问题描述】:
我有以下图像,其中包含文本和文本下方的大量空白。我想裁剪空白区域,使其看起来像第二张图像。
裁剪图像
这就是我所做的
>>> img = cv2.imread("pg13_gau.jpg.png")
>>> gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
>>> edged = cv2.Canny(gray, 30,300)
>>> (img,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
>>> cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
【问题讨论】:
-
只裁剪
boundingRect的黑色像素 -
boundingRect采用轮廓。我应该使用findContour找到轮廓吗?如何获得左上角黑色像素和右下角黑色像素? -
boundingRect 采用点向量...无需查找轮廓。 C++ 代码将只是:
std::vector<cv::Point> pts; cv::findNonZero(~gray); cv::Rect roi = cv::boundingRect(pts); cv::Mat1b crop = img(roi);Python 不会有太大不同 -
从HERE查看第7节
cv2.boundingRect() -
我执行了以下操作,但生成的图像不是裁剪后的图像:
img = cv2.imread("ws.png"); gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY); coords = cv2.findNonZero(gray); rekt = cv2.boundingRect(coords) cv2.imwrite("rekt.png",rekt)
标签: python opencv image-processing opencv3.0