python - 在 Opencv Python 中创建多个轨迹栏
问题描述
尝试为 1 个窗口创建 3 个轨迹栏。
我正在拍照。将其覆盖为灰色。使用非本地意味着也就是模糊它。通过一个叫做比诺的门槛。对其进行边缘检测。使用边缘检测来寻找轮廓。按区域对这些轮廓进行排序。只取最大的轮廓。并最好在精明或阈值图像上显示它。
我需要一个用于 nlm 模糊、阈值和精明线的轨迹栏都在同一张图像上,但我不知道该怎么做,也无法弄清楚。请你我唯一的希望!
import numpy as np
import cv2 as cv
def thresh_callback(val):
nlm_thresh = val
bino_thresh = val
canny_thresh = val
img = cv.imread('test.jpg')
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
nlm = cv.fastNlMeansDenoising(imgray,nlm_thresh,nlm_thresh,11,21)
bino = cv.adaptiveThreshold(nlm,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,bino_thresh,8)
canny_output = cv.Canny(bino, canny_thresh, canny_thresh * 2)
_, contours, hierarchy = cv.findContours(canny_output,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
cnt = sorted(contours, key=cv.contourArea, reverse=True)
cnts = cnt[0]
area = cv.contourArea(cnts)
print (area)
drawing = cv.drawContours(img, cnt, 0, (0,255,0), 3)
cv.imshow('Contours2', canny_output)
cv.imshow(source_window, drawing)
source_window = 'Source'
cv.namedWindow(source_window)
max_thresh = 255
thresh = 100 # initial threshold
cv.createTrackbar('nlm_thresh:', source_window, thresh, max_thresh, thresh_callback)
cv.createTrackbar('bino_thresh:', source_window, thresh, max_thresh, thresh_callback)
cv.createTrackbar('canny_thresh:', source_window, thresh, max_thresh, thresh_callback)
thresh_callback(thresh)
cv.waitKey(0) & 0xFF
cv.destroyAllWindows()
解决方案
另一种更简单的方法是忽略传递给 thresh_callback 的参数,并使用getTrackbarPos ()分别获取每个轨迹条的轨迹条值
def thresh_callback(val):
nlm_thresh = cv.getTrackbarPos('nlm_thresh:', source_window)
bino_thresh = cv.getTrackbarPos('bino_thresh:', source_window)
canny_thresh = cv.getTrackbarPos('canny_thresh:', source_window)
推荐阅读
- c# - 收到的尾随元数据大小超过了 .net 核心客户端的限制。(GRPC 的问题)
- php - 在 Markdown 中上传图片时出现致命错误。我该如何解决这个错误?
- mysql - 在日期上弹出 mysql 触发器
- php - 无法从没有重复的自定义循环中检索所有标签
- javascript - 数组不是数据而是容器但是为什么它有数据类型?
- javascript - 具有视差效果的整页滚动
- java - Wildfly - 打开的文件太多
- html - 样式表的路径未在 WordPress 上更新?
- c++ - 为什么我在运行 ZMQ 发布示例时收到错误“***堆栈粉碎检测到***”?
- python - `steps_per_epoch=None` 仅对基于 `keras.utils.Sequence` 的生成器有效