威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
[mw_shl_code=python,true]import cv2
import numpy as np
# 读取图片
baseImg = cv2.imread('./result/BJ2.png')
curImg = cv2.imread('./result/local_X200local_Y80,snr10,Track_height6850000m.png')
def detect(baseimg,curimg):
tmp3 = curImg.copy()
# 转灰度图
gray_base = cv2.cvtColor(baseImg, cv2.COLOR_BGR2GRAY)
gray_cur = cv2.cvtColor(curImg, cv2.COLOR_BGR2GRAY)
resImg = cv2.absdiff(gray_cur, gray_base)
binary,contours, hierarchy = cv2.findContours(resImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
area = []
# 找到最大的轮廓
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
draw_img3 = cv2.drawContours(curImg.copy(), contours, max_idx, (0, 0, 255), 1)
cnt = contours[max_idx]
# 外接图形
x, y, w, h = cv2.boundingRect(cnt)
# 直接在图片上进行绘制,所以一般要将原图复制一份,再进行绘制
res4 = cv2.rectangle(tmp3, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow('rectangle', res4)
cv2.imshow("resImg ", resImg)
cv2.imshow("counter ", draw_img3)
cv2.waitKey(0)
detect(baseImg,curImg)
[/mw_shl_code] |
|