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
python图像分割后处理-拟合边界四条直线并求交点
最新推荐文章于 2023-09-17 18:08:40 发布
本文介绍了如何使用Python进行图像分割后的后处理操作,通过拟合边界得到四条直线,并详细阐述了如何计算这四条直线的交点,提供了实现这一过程的详细步骤和示例效果图。
摘要由CSDN通过智能技术生成