Я хочу извлечь самый дальний квадрат, но он не работает, как показано ниже. Как я могу получить только 2 ожидаемых файла изображения?
- Входное изображение (layer.png)
![enter image description here](https://i.stack.imgur.com/s9q2e.png)
- Ожидаемый вывод (2 файла изображения)
![enter image description here](https://i.stack.imgur.com/B9O4q.png)
- Фактический вывод ( 6 файлов изображений)
![enter image description here](https://i.stack.imgur.com/dK3wW.png)
# -*- coding:utf-8 -*-
import cv2 as cv
from pdf2image import convert_from_path
def main():
image_file = 'images/layer.png'
src = cv.imread(image_file, cv.IMREAD_COLOR)
height, width, channels = src.shape
image_size = height * width
img_gray = cv.cvtColor(src, cv.COLOR_RGB2GRAY)
retval, dst = cv.threshold(img_gray, 1000, 255, cv.THRESH_TOZERO_INV)
dst = cv.bitwise_not(dst)
retval, dst = cv.threshold(dst, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
contours, _ = cv.findContours(
dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
area = cv.contourArea(contour)
if area < 10:
continue
if image_size * 0.99 < area:
continue
x, y, w, h = cv.boundingRect(contour)
cut = src[y:y+h, x:x+w]
detector = cv.FastFeatureDetector_create()
detector.setNonmaxSuppression(False)
keypoints = detector.detect(cut)
cv.imwrite('images/debug_%d.png' % i, cut)
if __name__ == '__main__':
main()
$ git clone https://github.com/zono/ocr.git
$ cd ocr
$ git checkout 1de458a34ab14fede41bba9f6ef0d3a6356c8668
$ docker-compose up -d
$ docker exec -it ocr /bin/bash
$ python3 opencv.py