[Python] how to find the maximum area rectangle inside a contours

Discussão em 'Python' iniciado por Stack, Novembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a question. How I can find the maximum area rectangle inside a contours? Now I don't have an idea. My approach to the problem is below.
    Thanks for any help.

    [​IMG]

    import cv2
    import numpy as np
    import math


    image_path = r"\\acvnas03\ACVNAS03_DATA\60.pd\03. SMT\3. Method\9. File tam\python_k-xoa_T.anh\New folder\kiem_truoc_lo\ROI\H9300600\Bside\IC3.jpg"
    image = cv2.imread(image_path)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    #cv2.imshow("thresh",thresh)

    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    gg = cv2.drawContours(thresh, contours, -1, (0,255,0), 3)
    #cv2.imshow("thres6h",gg)

    edges = cv2.Canny(gg, 100, 200)
    #cv2.imshow("edges",edges)

    kernel = np.ones((3, 3), np.uint8)
    closed_edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    cv2.imshow("Origin6546 al image", closed_edges)
    #cv2.imwrite("Origin6546.jpg",closed_edges)

    contours2, _ = cv2.findContours(closed_edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    gg2 = cv2.drawContours(thresh, contours2, -1, (0,255,0), 2)
    #cv2.imshow("gg2",gg2)


    cv2.waitKey(0)
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página