python图像分割后处理-拟合边界四条直线并求交点

本文介绍了如何使用Python进行图像分割后的后处理操作,通过拟合边界得到四条直线,并详细阐述了如何计算这四条直线的交点,提供了实现这一过程的详细步骤和示例效果图。
摘要由CSDN通过智能技术生成
from imutils.perspective import four_point_transform
import cv2
import numpy as np
from scipy import stats
from numpy import unique
from numpy import where
from sklearn.cluster import KMeans
from matplotlib import pyplot
import math

def linefit(x, y):
    k = 0
    b = 0
    r = 0
    N = float(len(x))
    sx, sy, sxx, syy, sxy = 0, 0, 0, 0, 0
    for i in range(0, int(N)):
        sx += x[i]
        sy += y[i]
        sxx += x[i] * x[i]
        syy += y[i] * y[i]
        sxy += x[i] * y[i]
    if(N - sxx != 0 and syy - sy * sy / N!=0):
        k = (sy * sx / N - sxy) / (sx * sx / N - sxx)
        b = (sy - k * sx) / N
        r = abs(sy * sx / N - sxy) / math.sqrt((sxx - sx * sx / N) * (syy - sy * sy / N))
    return k, b, r

def jvli(mask, image):
    mask = cv2.imread(mask)
    image = cv2.imread(image)
    # print(mas)
    # 将BGR转成灰度图
    gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    # 图片二值化
    ret, binary = cv2.threshold(gray, 156, 255, cv2.THRESH_BINARY)
    # 原参数为binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    # 找到最大区域并填充
    area = []
    for j in range(len(contours)):
        area.append(cv2.contourArea(contours[j]))

    max_idx = np.argmax(area)
    max_area = cv2.contourArea(contours[max_idx])
    # print(max_area)
    for k in range(len(contours)):
        if k != max_idx:
            cv2.fillPoly(mask, [contours[k]], (198, 215, 20))
    cv2.drawContours(mask, contours, -1, (0, 0, 0), 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值