红蓝锥桶招中识别

前言

SLAM写不出来,整个纯视觉凑合凑合得了

main

import cv2
import numpy as np
import time
import numba

class FindMiddleLine:
def __init__(self, raw_img):
self.raw_img = raw_img

def find_line(self):
img = self.raw_img

down_width = 640
down_height = 320

down_points = (down_width, down_height)

small_img = cv2.resize(img, down_points, interpolation=cv2.INTER_LINEAR)

start_pix = 80
end_pix = 250
max_pix = (end_pix-start_pix * 640 )
detected_img = small_img[start_pix:end_pix, 0:640]

hsv = cv2.cvtColor(detected_img, cv2.COLOR_BGR2HSV)

blue_shv = [[100, 128], [130, 255], [50, 240]]

red_hsv = [[0, 18], [149, 255], [85, 240]]
red_hsv_2 = []
blue_inrange = cv2.inRange(hsv, np.array([100, 130, 50]), np.array([128, 255, 240]))
red_inrange = cv2.inRange(hsv, np.array([0, 149, 85]), np.array([24, 255, 240]))

blue_inrange = blue_inrange.astype(np.int32)
red_inrange = red_inrange.astype(np.int32)
different = np.sum(red_inrange)
print(different)
different -= np.sum(blue_inrange)
different = different/(max_pix*255)
print(different)
return different
""" print(different)
blue_inrange = blue_inrange.astype(np.uint8)
red_inrange = red_inrange.astype(np.uint8)
cv2.imshow("qwq", blue_inrange)
cv2.imshow("pap", red_inrange)
if cv2.waitKey() != "q":

cv2.destroyAllWindows()

print(different)"""

# contribution by huawei
if __name__ == "__main__":
img = cv2.imread("img/17.jpg")
find = FindMiddleLine(img)
st = time.time()

st = time.time()
find.find_line()
en = time.time()
print(1/(en - st))

急速调参

相应的,需要一个调参的软件

import json # 用于保存配置到文件
import cv2
import numpy as np
import time

"""

1. R 按下代表保存红色参数
2. T 按下代表读取红色参数
3. B 按下代表保存蓝色参数
4. N 按下代表读取蓝色参数
5. Y 按下代表保存黄色参数
6. U 按下代表读取黄色参数

"""

# 'camera' or 'picture'
mode = 'picture'

if mode == 'camera':
cap = cv2.VideoCapture(0)

def read_config(filename):
with open(filename, 'r') as f:
config = json.load(f)
return config

def update(x):
global gs, erode, Hmin, Smin, Vmin, Hmax, Smax, Vmax, img, Hmin2, Hmax2, img0, size_min

if mode == 'camera':
ret, img0 = cap.read()
elif mode == 'picture':
img0 = cv2.imread('pap.jpg')
img = img0.copy()

gs = cv2.getTrackbarPos('gs', 'image')
erode = cv2.getTrackbarPos('erode', 'image')
Hmin = cv2.getTrackbarPos('Hmin1', 'image')
Smin = cv2.getTrackbarPos('Smin', 'image')
Vmin = cv2.getTrackbarPos('Vmin', 'image')
Hmax = cv2.getTrackbarPos('Hmax1', 'image')
Smax = cv2.getTrackbarPos('Smax', 'image')
Vmax = cv2.getTrackbarPos('Vmax', 'image')
Hmin2 = cv2.getTrackbarPos('Hmin2', 'image')
Hmax2 = cv2.getTrackbarPos('Hmax2', 'image')
size_min = cv2.getTrackbarPos('size_min', 'image')
# 滤波二值化
gs_frame = cv2.GaussianBlur(img, (gs, gs), 1)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=erode)
inRange_hsv = cv2.inRange(erode_hsv, np.array([Hmin, Smin, Vmin]), np.array([Hmax, Smax, Vmax]))
inRange_hsv2 = cv2.inRange(erode_hsv, np.array([Hmin2, Smin, Vmin]), np.array([Hmax2, Smax, Vmax]))
img = inRange_hsv + inRange_hsv2
# 外接计算
print(img.sum())
cnts = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
target_list = []
pos = []
if size_min < 1:
size_min = 1
for c in cnts:
if cv2.contourArea(c) < size_min:
continue
else:
target_list.append(c)
for cnt in target_list:
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img0, (x, y), (x + w, y + h), (0, 255, 0), 2)
pos.append([int(x + w / 2), y + h / 2])
#print(pos)

def save_config(filename):
config = {
'gs': cv2.getTrackbarPos('gs', 'image'),
'erode': cv2.getTrackbarPos('erode', 'image'),
'Hmin1': cv2.getTrackbarPos('Hmin1', 'image'),
'Hmax1': cv2.getTrackbarPos('Hmax1', 'image'),
'Hmin2': cv2.getTrackbarPos('Hmin2', 'image'),
'Hmax2': cv2.getTrackbarPos('Hmax2', 'image'),
'Smin': cv2.getTrackbarPos('Smin', 'image'),
'Smax': cv2.getTrackbarPos('Smax', 'image'),
'Vmin': cv2.getTrackbarPos('Vmin', 'image'),
'Vmax': cv2.getTrackbarPos('Vmax', 'image'),
'size_min': cv2.getTrackbarPos('size_min', 'image')
}

with open(filename, 'w') as f:
json.dump(config, f)

print(f'Parameters saved to {filename}')

def img_test():
sleep = 0.1
gs = 0
erode = 0
Hmin1 = 100
Hmax1 = 125
Hmin2 = 179
Hmax2 = 0
Smin = 130
Smax = 255
Vmin = 50
Vmax = 240
size_min = 1000

# 创建窗口
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.createTrackbar('gs', 'image', 0, 8, update)
cv2.createTrackbar('erode', 'image', 0, 8, update)
cv2.createTrackbar('Hmin1', 'image', 0, 179, update)
cv2.createTrackbar('Hmax1', 'image', 0, 179, update)
cv2.createTrackbar('Hmin2', 'image', 0, 179, update)
cv2.createTrackbar('Hmax2', 'image', 0, 179, update)
cv2.createTrackbar('Smin', 'image', 0, 255, update)
cv2.createTrackbar('Smax', 'image', 0, 255, update)
cv2.createTrackbar('Vmin', 'image', 0, 255, update)
cv2.createTrackbar('Vmax', 'image', 0, 255, update)
cv2.createTrackbar('size_min', 'image', 1, 100000, update)
# 默认值
cv2.setTrackbarPos('gs', 'image', gs)
cv2.setTrackbarPos('erode', 'image', erode)
cv2.setTrackbarPos('Hmin1', 'image', Hmin1)
cv2.setTrackbarPos('Hmax1', 'image', Hmax1)
cv2.setTrackbarPos('Hmin2', 'image', Hmin2)
cv2.setTrackbarPos('Hmax2', 'image', Hmax2)
cv2.setTrackbarPos('Smin', 'image', Smin)
cv2.setTrackbarPos('Smax', 'image', Smax)
cv2.setTrackbarPos('Vmin', 'image', Vmin)
cv2.setTrackbarPos('Vmax', 'image', Vmax)
cv2.setTrackbarPos('size_min', 'image', size_min)

while True:
try:
update(1)
except:
pass
cv2.imshow('image', img)
cv2.imshow('image1', img)
cv2.imshow('image0', img0)
key = cv2.waitKey(1)

# 检查按键事件
if key == ord('r'): # 如果按下 'r' 键
save_config('red.json')
elif key == ord('b'): # 如果按下 'b' 键
save_config('blue.json')
elif key == ord('y'): # 如果按下 'h' 键
save_config('yellow.json')
elif key == ord('t'):
red_dic = read_config('red.json')

cv2.setTrackbarPos('gs', 'image', red_dic["gs"])
cv2.setTrackbarPos('erode', 'image', red_dic["erode"])
cv2.setTrackbarPos('Hmin1', 'image', red_dic["Hmin1"])
cv2.setTrackbarPos('Hmax1', 'image', red_dic["Hmax1"])
cv2.setTrackbarPos('Hmin2', 'image', red_dic["Hmin2"])
cv2.setTrackbarPos('Hmax2', 'image', red_dic["Hmax2"])
cv2.setTrackbarPos('Smin', 'image', red_dic["Smin"])
cv2.setTrackbarPos('Smax', 'image', red_dic["Smax"])
cv2.setTrackbarPos('Vmin', 'image', red_dic["Vmin"])
cv2.setTrackbarPos('Vmax', 'image', red_dic["Vmax"])
cv2.setTrackbarPos('size_min', 'image', red_dic["size_min"])
elif key == ord('n'):
blue_dic = read_config('blue.json')

cv2.setTrackbarPos('gs', 'image', blue_dic["gs"])
cv2.setTrackbarPos('erode', 'image', blue_dic["erode"])
cv2.setTrackbarPos('Hmin1', 'image', blue_dic["Hmin1"])
cv2.setTrackbarPos('Hmax1', 'image', blue_dic["Hmax1"])
cv2.setTrackbarPos('Hmin2', 'image', blue_dic["Hmin2"])
cv2.setTrackbarPos('Hmax2', 'image', blue_dic["Hmax2"])
cv2.setTrackbarPos('Smin', 'image', blue_dic["Smin"])
cv2.setTrackbarPos('Smax', 'image', blue_dic["Smax"])
cv2.setTrackbarPos('Vmin', 'image', blue_dic["Vmin"])
cv2.setTrackbarPos('Vmax', 'image', blue_dic["Vmax"])
cv2.setTrackbarPos('size_min', 'image', blue_dic["size_min"])
elif key == ord('u'):
yellow_dic = read_config('blue.json')

cv2.setTrackbarPos('gs', 'image', yellow_dic["gs"])
cv2.setTrackbarPos('erode', 'image', yellow_dic["erode"])
cv2.setTrackbarPos('Hmin1', 'image', yellow_dic["Hmin1"])
cv2.setTrackbarPos('Hmax1', 'image', yellow_dic["Hmax1"])
cv2.setTrackbarPos('Hmin2', 'image', yellow_dic["Hmin2"])
cv2.setTrackbarPos('Hmax2', 'image', yellow_dic["Hmax2"])
cv2.setTrackbarPos('Smin', 'image', yellow_dic["Smin"])
cv2.setTrackbarPos('Smax', 'image', yellow_dic["Smax"])
cv2.setTrackbarPos('Vmin', 'image', yellow_dic["Vmin"])
cv2.setTrackbarPos('Vmax', 'image', yellow_dic["Vmax"])
cv2.setTrackbarPos('size_min', 'image', yellow_dic["size_min"])
elif key == 27: # 如果按下 ESC 键
break

time.sleep(0.1)

cv2.destroyAllWindows()

if __name__ == '__main__':
img_test()

效果嘎嘎好,跑了34秒应该能国一

评论

  1. Chansir
    Windows Chrome 122.0.0.0
    8 月前
    2024-3-13 14:18:47

    很有意思的博客,向作者学习!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇