0
点赞
收藏
分享

微信扫一扫

鸿蒙Harmony应用开发—ArkTS(@Extend装饰器:定义扩展组件样式)

伽马星系 03-23 07:00 阅读 4
pythonopencv
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time        :2024/3/21 15:32
# @Author      :weiz
# @ProjectName :jiangling_new
# @File        :getInnerRect.py
# @Description :
import cv2
import numpy as np
import copy


def maximum_internal_rectangle(mask):
    img = copy.deepcopy(mask)

    img_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

    ret, img_bin = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY)

    contours, _ = cv2.findContours(img_bin, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

    contour = contours[0].reshape(len(contours[0]), 2)

    rect = []

    for i in range(len(contour)):
        x1, y1 = contour[i]
        for j in range(len(contour)):
            x2, y2 = contour[j]
            area = abs(y2 - y1) * abs(x2 - x1)
            rect.append(((x1, y1), (x2, y2), area))

    all_rect = sorted(rect, key=lambda x: x[2], reverse=True)

    if all_rect:
        best_rect_found = False
        index_rect = 0
        nb_rect = len(all_rect)

        while not best_rect_found and index_rect < nb_rect:

            rect = all_rect[index_rect]
            (x1, y1) = rect[0]
            (x2, y2) = rect[1]

            valid_rect = True

            x = min(x1, x2)
            while x < max(x1, x2) + 1 and valid_rect:
                if any(img[y1, x]) == 0 or any(img[y2, x]) == 0:
                    valid_rect = False
                x += 1

            y = min(y1, y2)
            while y < max(y1, y2) + 1 and valid_rect:
                if any(img[y, x1]) == 0 or any(img[y, x2]) == 0:
                    valid_rect = False
                y += 1

            if valid_rect:
                best_rect_found = True

            index_rect += 1

        if best_rect_found:
            # 如果要在灰度图img_gray上画矩形,请用黑色画(0,0,0)
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 1)
            print(x1, y1, x2, y2)
            cv2.imshow("rec", img)
            cv2.waitKey(0)

        else:
            print("No rectangle fitting into the area")

    else:
        print("No rectangle found")


def maximum_internal_circle(mask):
    mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    contours, _ = cv2.findContours(mask_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # 计算到轮廓的距离
    raw_dist = np.empty(mask_gray.shape, dtype=np.float32)
    for i in range(mask_gray.shape[0]):
        for j in range(mask_gray.shape[1]):
            raw_dist[i, j] = cv2.pointPolygonTest(contours[0], (j, i), True)

    # 获取最大值即内接圆半径,中心点坐标
    minVal, maxVal, _, maxDistPt = cv2.minMaxLoc(raw_dist)
    print(cv2.minMaxLoc(raw_dist))
    minVal = abs(minVal)
    maxVal = abs(maxVal)

    # 画出最大内接圆
    result = cv2.cvtColor(mask_gray, cv2.COLOR_GRAY2BGR)
    radius = np.int(maxVal)
    center_of_circle = maxDistPt
    cv2.circle(result, maxDistPt, radius, (0, 255, 0), 2, 1, 0)
    cv2.rectangle(result, (np.int(maxDistPt[0] - 0.71 * radius), np.int(maxDistPt[1] - 0.71 * radius)),
                  (np.int(maxDistPt[0] + 0.71 * radius), np.int(maxDistPt[1] + 0.71 * radius)), (255, 0, 0), 1)
    cv2.imshow('Maximum inscribed circle', result)
    cv2.waitKey(0)


if __name__ == "__main__":
    mask = cv2.imread("C:/Users/weiz/Desktop/TOM_add/TOM10/11.png")
    maximum_internal_rectangle(mask)
    maximum_internal_circle(mask)

        可能只能检测出“凸”形状的mask,不能检测“凹”形状的mask,请慎用。

举报

相关推荐

0 条评论