import cv2
import numpy as np
import urllib
SAMPLES = 300
# 判断number和test是否接近
def in_range(number, test, thresh=0.2):
return abs(number - test) < thresh
img_path = './CarData/TestImages/test-3.pgm'
# img_path = './car.jpg'
def get_extract_detect():
return cv2.xfeatures2d.SIFT_create(), cv2.xfeatures2d.SIFT_create()
def get_flann_matcher():
flann_params = dict(algorithm = 1, trees = 5)
return cv2.FlannBasedMatcher(flann_params, {})
# extractor: sift算子
# detector: sift算子
# 使用extractor.compute函数
def extract_sift(fn, extractor, detector):
im = cv2.imread(fn,0)
# detector.detect(im): 图像的sift特征
# extractor.compute(im, detector.detect(im))[1]: 提取图像sift特征并返回
return extractor.compute(im, detector.detect(im))[1]
datapath = './CarData/TrainImages/'
def path(cls,i):
return "%s/%s%d.pgm" % (datapath,cls,i+1)
def bow_features(img, extractor_bow, detector):
# 基于extractor_bow 提取img的sift特征
return extractor_bow.compute(img, detector.detect(img))
def car_detector():
pos, neg = "pos-", "neg-"
detect, extract = get_extract_detect()
matcher = get_flann_matcher()
# 初始化BOW
bow_kmeans_trainer = cv2.BOWKMeansTrainer(12)
# 输入extract、matcher 使用cv2.BOWImgDescriptorExtractor 得到extract_bow
# 得到许多视觉词汇
extract_bow = cv2.BOWImgDescriptorExtractor(extract, matcher)
for i in range(SAMPLES):
print(i)
# 建立视觉词汇
# print(extract_sift(path(pos, i), extract, detect))
# print(extract_sift(path(neg, i), extract, detect))
if ((extract_sift(path(pos, i), extract, detect)) is None) or ((extract_sift(path(neg, i), extract, detect)) is None):
continue
else:
bow_kmeans_trainer.add(extract_sift(path(pos, i), extract, detect))
bow_kmeans_trainer.add(extract_sift(path(neg, i), extract, detect))
vocabulary = bow_kmeans_trainer.cluster()
# 基于BOW的词汇字典 这是基于汽车的BOW模型(extract_bow)
# 方便后续测试图片的sift特征提取extractor_bow.compute(img, detector.detect(img))
extract_bow.setVocabulary(vocabulary)
traindata, trainlabels = [], []
for i in range(SAMPLES):
# 建立词汇字典
print(i)
# 需要判断提取的sift特征是否为None
if (bow_features(cv2.imread(path(pos, i), 0), extract_bow, detect) is None) or (bow_features(cv2.imread(path(neg, i), 0), extract_bow, detect) is None):
continue
else:
traindata.extend(bow_features(cv2.imread(path(pos, i), 0), extract_bow, detect))
trainlabels.append(1)
traindata.extend(bow_features(cv2.imread(path(neg, i), 0), extract_bow, detect))
trainlabels.append(-1)
# 进行SVM训练 得到svm模型
svm = cv2.ml.SVM_create()
#svm的设置
svm.setType(cv2.ml.SVM_C_SVC)
svm.setGamma(1)
svm.setC(35)
# 用于多分类核
svm.setKernel(cv2.ml.SVM_RBF)
# 建立好了汽车分类模型svm
svm.train(np.array(traindata), cv2.ml.ROW_SAMPLE, np.array(trainlabels))
return svm, extract_bow
# 图像金字塔函数实现
def resize(img, scaleFactor):
return cv2.resize(img, (int(img.shape[1] * (1 / scaleFactor)), int(img.shape[0] * (1 / scaleFactor))), interpolation=cv2.INTER_AREA)
def pyramid(image, scale=1.5, minSize=(200, 80)):
yield image
while True:
# 每次以scale的倍数进行缩小
image = resize(image, scale)
# image.shape[0]: height image.shape[1]: width
# minSize=(200, 80) --> minSize[0] width
if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
break
yield image # 每进行一次resize 就会yield image 从而实现了图像金字塔多尺度
# sliding_window函数
def sliding_window(image, stepSize, windowSize):
# image.shape[0]: 高
for y in range(0, image.shape[0], stepSize):
# image.shape[1]: 宽
for x in range(0, image.shape[1], stepSize):
# y:y + windowSize[1]: 高
# x:x + windowSize[0]: 宽 提取图像对应是: 行/列
# 如果移动到了图像的边界 此时得到的图像大小不一定是windowSize了
yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])
def area(box):
return (abs(box[2] - box[0])) * (abs(box[3] - box[1]))
# a: (x,y,w,h) -> (a[0],a[1],a[2],a[3])
def overlaps(a, b, thresh):
# print("checking overlap ")
# print(a, b)
# 起始点x坐标
x1 = np.maximum(a[0], b[0])
# 对角点的x坐标
x2 = np.minimum(a[2], b[2])
# 起始点y坐标
y1 = np.maximum(a[1], b[1])
# 对角点的y坐标
y2 = np.minimum(a[3], b[3])
intersect = float(area([x1, y1, x2, y2]))
# if(intersect / np.minimum(area(a), area(b)) >= thresh):
# print("Had overlap")
# else:
# print("No overlap")
# 两个举行的重合部分
return intersect / np.minimum(area(a), area(b)) >= thresh
svm, extractor = car_detector()
detect = cv2.xfeatures2d.SIFT_create()
rectangles = []
counter = 1
scaleFactor = 1.25
scale = 1
font = cv2.FONT_HERSHEY_PLAIN
cnt = 0
w, h = 100, 40
# 读取测试图片
img = cv2.imread(img_path)
print(img.shape)
i/*
开发不易,整理也不易,如需要详细的说明文档和程序,以及完整的数据集,训练好的模型,或者进一步开发,
可加作者新联系方式咨询,WX:Q3101759565,QQ:3101759565
*/
print("yes")
continue
# 每个sliding_window得到的区域图像
try:
# 基于汽车类型的BOW模型extractor
# detect: sift提取特征
# extractor_bow.compute(img, detector.detect(img))
# 提取roi的sift特征
bf = bow_features(roi, extractor, detect)
# 提取后基于BOW的sift特征可用于汽车类型的分类模型svm进行预测
_, result = svm.predict(bf)
# flags=cv2.ml.STAT_MODEL_RAW_OUTPUT | cv2.ml.STAT_MODEL_UPDATE_MODEL
a, res = svm.predict(bf, flags=cv2.ml.STAT_MODEL_RAW_OUTPUT | cv2.ml.STAT_MODEL_UPDATE_MODEL)
# result[0][0]: 得到分类 1或者是-1
# res[0][0] 是 Score
# res
# print( "Class: %d, Score: %f, a: %s" % (result[0][0], res[0][0], res))
score = res[0][0]
# 检测到了汽车
if result[0][0] == 1:
if score < -1.0:
# 进行图像放大 得到原图像img的矩形参数 [rx, ry, rx2, ry2]
rx, ry, rx2, ry2 = int(x * scale), int(y * scale), int((x + w) * scale), int((y + h) * scale)
# rectangles 是一个二维的list
rectangles.append([rx, ry, rx2, ry2, abs(score)])
print("NO")
cnt = cnt + 1
# print("Find %d rect" % cnt)
except:
pass
counter += 1
# 以上的功能实现可以这么理解:
# 使用多尺度(缩小 提取的应用技术),每一次是1个resized,对每一个resized,使用sliding_window,每一块是roi,对roi进行BOW+SVM的判断,
# rectangles存储的是使用sliding_window在图像中判断为汽车的标定。
windows = np.array(rectangles)
# 非极大值抑制
overlapThresh = 0.25
# boxes: [[150. 75. 275. 125. 1.28081572]]
boxes = windows
# print(boxes)
# 找出col4这一列
scores = boxes[:,4]
# print(scores)
# 从小到大的升序排序 score_idx是一维数组
score_idx = np.argsort(scores)
# print(scores)
# box = scores[score_idx[0]] # score_idx[0]: 最小的score的index
# print(box)
# #从小到大排序的位置索引: 15 13 28 23 5 22 31 7 0 32 21 29 30 14 6 8 1 16 3 17 12 9 27 24 2 18 25 26 4 20 19 10 11
# print(score_idx)
# # score_idx[0]: 表示第一个位置元素 最小的score
# print(score_idx[0])
# for s in score_idx:
# print(s)
print(score_idx)
to_delete = []
while len(score_idx) > 0:
# 找到最小的score的位置索引index
box = score_idx[0]
# 从最小的开始
for s in score_idx:
if s == box or s == score_idx[-1]:
continue
try:
#
if (overl
没有合适的资源?快使用搜索试试~ 我知道了~
Opencv实现程序-部分9
共5个文件
txt:5个
需积分: 5 0 下载量 111 浏览量
2024-03-17
11:54:41
上传
评论
收藏 10KB ZIP 举报
温馨提示
最近整理了Opencv的学习内容实现代码,每个代码都在本机上运行通过的,通过这些程序,可以实现某些经典的功能,也可以对Opencv有个熟悉学习的过程. (1)图片的读取和写入功能实现代码; (2)对像素值的赋值实现代码; (3)Img.itemset的功能实现代码; (4)对图像通道的操作功能实现代码; (5)对图像ROI区域进行操作功能实现代码; (6)Img.imread属性返回操作功能实现代码; (7)视频图像的读取操作功能实现代码; (8)视频图像的读取和写入功能实现代码; (9)对鼠标和键盘的检测和画图功能实现代码; (10)对摄像头(电脑摄像头/USB摄像头)的操作功能实现代码; (11)对摄像头采集视频进行图片捕获以及反转处理保存功能实现代码; (12)对读取的图片进行滤波变换处理功能实现代码; (13)对读取图片进行中值滤波、Laplacian变换、分解各通道并进行融合功能实现代码; (14)对摄像头捕获实时图像进行灰度变换以及Laplacian变换实时显示功能实现代码; (15)实现一个类对设定卷积核进行图像滤波处理功能实现代码; ..........
资源推荐
资源详情
资源评论
收起资源包目录
9.zip (5个子文件)
9
实现了基于BOW-SVM-Guassian金字塔尺度变换-移动窗口的图像识别实现代码.txt 9KB
实现基于预训练模型实时摄像头视频的人脸面部定位检测和识别实现代码.txt 2KB
实现了基于动态帧的视频移动物体实时检测识别实现代码.txt 1KB
实现基于卡尔曼滤波-CAMshift-Meanshift等多种方法实时物体跟踪识别实现代码.txt 6KB
实现基于卡尔曼滤波的鼠标绘制线跟踪识别.txt 2KB
共 5 条
- 1
资源评论
迪哥_AI_人工智能
- 粉丝: 539
- 资源: 128
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功