【问题标题】:How to find smallest rectangle in image using opencv python?如何使用opencv python在图像中找到最小的矩形?
【发布时间】:2020-08-29 23:43:37
【问题描述】:

您好,我正在尝试从汽车中提取车牌,我正在尝试过滤掉图像中除了包含文本的矩形之外的所有矩形,然后显示它。但是我不确定如何实现这一点。现在我所有的代码只是显示它找到的任何矩形,它在某些图像上显示挡风玻璃。谁能解释一下如何显示最小的矩形?

import matplotlib.pyplot as plt
import cv2

#import imutils

import numpy as np
from PIL import Image
from PIL import ImageEnhance
from skimage import color, data, restoration
from scipy.signal import convolve2d
import pytesseract
import PIL.ImageOps
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'


def main (img):
    print('flflfl')
    img = cv2.imread(img,cv2.IMREAD_COLOR)
    print(img)
    img = cv2.resize(img, (600,400) )
    img = cv2.resize(img, (600,400) )
    threshold = 180 # to be determined
    _, img_binarized = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
    pil_img = Image.fromarray(img_binarized)


    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 13, 15, 15)

    edged = cv2.Canny(gray, 30, 200)
    thresh = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 2)
    #contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    #contours = imutils.grab_contours(contours)
    contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
    screenCnt = None

    gaussian_blur_license_plate = cv2.GaussianBlur(
        img, (5, 5), 0)

    for c in contours:

        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)


        if len(approx) == 4:
            
            screenCnt = approx

            break

    if screenCnt is None:
        detected = 0
        print ("No contour detected")
    else:
         detected = 1

    if detected == 1:
        cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

    mask = np.zeros(gray.shape,np.uint8)
    new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
    new_image = cv2.bitwise_and(img,img,mask=mask)

    (x, y) = np.where(mask == 255)
    (topx, topy) = (np.min(x), np.min(y))
    (bottomx, bottomy) = (np.max(x), np.max(y))
    Cropped = gray[topx:bottomx+1, topy:bottomy+1]

【问题讨论】:

  • 我看到你已经尝试在轮廓区域的帮助下找到图像中最小的矩形。是否可以分享您尝试查找的图像。
  • 我正在尝试检查图像中最小的矩形是否包含文本,如果包含则显示它

标签: python-3.x opencv keras


【解决方案1】:

您可以通过查看轮廓区域找到最小的矩形:

for c in contours:
    area = cv2.contourArea(c)
    #code for finding the smalles area

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多