Извлечь наружу квадрат - PullRequest
       0

Извлечь наружу квадрат

0 голосов
/ 10 февраля 2020

Я хочу извлечь самый дальний квадрат, но он не работает, как показано ниже. Как я могу получить только 2 ожидаемых файла изображения?

  • Входное изображение (layer.png)

enter image description here

  • Ожидаемый вывод (2 файла изображения)

enter image description here enter image description here

  • Фактический вывод ( 6 файлов изображений)

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

  • opencv.py
# -*- 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

1 Ответ

1 голос
/ 10 февраля 2020

@ ХансХирс дал мне ответ. Спасибо!

Используйте cv.RETR_EXTERNAL вместо cv.RETR_TREE

  • До
contours, _ = cv.findContours(
        dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
  • После
contours, _ = cv.findContours(
        dst, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...