航线轨迹提取

import cv2
import numpy as np
from glob import glob

# 读取文件夹中的图像
filenames = glob("./dataHRs/*.png")

# 将后一张图像与前一张做差
diff = None
centers = []
prev_center = None
for i in range(1, len(filenames)):
    img1 = cv2.imread(filenames[0],cv2.IMREAD_GRAYSCALE)
    img2 = cv2.imread(filenames[i],cv2.IMREAD_GRAYSCALE)
    diff = cv2.absdiff(img1, img2)
    # 返回差值中值不为0的区域的最小外接矩形的四个角和中心点坐标
    contours, _ = cv2.findContours(diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    max_area = 0 # variable to store area of largest non-zero difference region
    max_contour = None # variable to store contour of largest non-zero difference region
    
    # loop through all difference regions with non-zero median values
    for contour in contours:
        rect = cv2.boundingRect(contour)
        m = cv2.mean(diff[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]])
        if m[0] != 0:
            area = cv2.contourArea(contour) # calculate area of contour
            if area > max_area: # update max_contour and max_area if this contour is larger
                max_contour = contour
                max_area = area

    
    if max_contour is not None: # if there is a non-zero contour, find its center and store it
        rect = cv2.boundingRect(max_contour)
        center = (rect[0] + rect[2] // 2, rect[1] + rect[3] // 2)
        centers.append(center)
        # 保存中心点坐标到txt文件
        with open("centers.txt", "a") as file:
            file.write(str(center[0]) + " " + str(center[1]) + "\n")

# 在第一张图上连接中心点画一条直线,显示
img1 = cv2.imread(filenames[0])
for i in range(1, len(centers)):
     cv2.line(img1, centers[i-1], centers[i], (0, 0, 255), 5)
cv2.imwrite("./air.png", img1)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值