我正在尝试使用 OpenCV 和 Python 自动计算图像中的汽车数量。
最初我认为我可以通过一些分割来做到这一点,但我并没有取得太大的成功。然后我认为霍夫变换可能有助于计算汽车周围的边界,但它只能真正挑选出 parking 位线。我唯一能想到的就是开始在汽车和非汽车的模板上训练一些比赛,但我希望有一些更简单的东西可以在这里做得很好。我还尝试了边缘检测,看起来不错,但不确定如何继续:
最佳答案
好的,所以......我可能在这方面做了太多工作,但它看起来很简单。
对于我的实现,我决定最好找到空的 parking 位,并假设所有其他 parking 位都已被占用。为了确定一个 parking 位是否为空,我只是将其与道路上 parking 位大小的部分进行了比较。这意味着无论是明亮还是黑暗等,该算法都应该有效,因为模板是直接从图像中提取的。
此时我只是做模板匹配(我尝试了各种方法,但 cv2.TM_CCORR_NORMED 效果最好)。这给出了一个不错的结果,现在我们需要处理它。
我在 parking 场行周围创建了 ROI(感兴趣区域)。然后我通过对每列进行统计来将它们折叠成一个向量。我看均值。
这是一个很好的指标,您已经可以清楚地看到空白处在哪里。但是深色汽车仍然存在一些问题,所以现在我们决定查看另一个统计数据,方差如何? parking 场在整个地区都非常稳定。另一方面,汽车有一些变化, window 、车顶行李架后视镜会产生一些变化。所以我绘制了“倒置”方差。所以它不是没有变化,方差为 0,而是方差为 1。它看起来像这样
这看起来很有希望!但你知道什么更好吗?两者结合!所以让我们将它们相乘,我称这个结果为“概率”,因为它应该介于 0 和 1 之间
现在您可以真正看出空白区域和深色汽车之间的区别了。因此,让我们做一些简单的阈值。这很好,但它不会为您提供车辆/空位的数量。此时,我们逐列查看“概率”,寻找超过阈值的一定数量的连续像素。多少像素?大约与汽车宽度一样多的像素。这种“滞后”类型的模型应该抑制任何峰值或虚假数据点。
现在一切都在一起了,我们假设空格的数量是恒定的(我认为合理的假设)我们只是说汽车数量 = 空格数 - 空格数 并标记图片
并打印一些结果
found 24 cars and 1 empty space(s) in row 1
found 23 cars and 0 empty space(s) in row 2
found 20 cars and 3 empty space(s) in row 3
found 22 cars and 0 empty space(s) in row 4
found 13 cars and 9 empty space(s) in row 5
当然还有代码。它可能不是最有效的,但我通常是一个 matlab 人,这是我的第一个 openCV/Python 项目
import cv2
import numpy as np
from matplotlib import pyplot as plt
# this just keeps things neat
class ParkingLotRow(object):
top_left=None
bot_right=None
roi=None
col_mean=None
inverted_variance=None
empty_col_probability=None
empty_spaces=0
total_spaces=None
def __init__(self,top_left,bot_right,num_spaces):
self.top_left = top_left
self.bot_right = bot_right
self.total_spaces = num_spaces
############################ BEGIN: TWEAKING PARAMETERS ###########################################
car_width = 8 #in pixels
thresh = 0.975 #used to determine if a spot is empty
############################### END: TWEAKING PARAMETERS ###########################################
parking_rows = []
# defines regions of interest, row 1 is on top, row 5 is on bottom, values determined empirically
parking_rows.append(ParkingLotRow(( 1, 20),(496, 41),25)) #row 1
parking_rows.append(ParkingLotRow(( 1, 87),(462,105),23)) #row 2
parking_rows.append(ParkingLotRow(( 1,140),(462,158),23)) #row 3
parking_rows.append(ParkingLotRow(( 1,222),(462,240),22)) #row 4
parking_rows.append(ParkingLotRow(( 1,286),(462,304),22)) #row 5
#read image
img = cv2.imread('parking_lot.jpg')
img2 = img.copy()
#creates a template, its jsut a car sized patch of pavement
template = img[138:165,484:495]
m, n, chan = img.shape
#blurs the template a bit
template = cv2.GaussianBlur(template,(3,3),2)
h, w, chan = template.shape
# Apply template Matching
res = cv2.matchTemplate(img,template,cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
#adds bounding box around template
cv2.rectangle(img,top_left, bottom_right, 255, 5)
#adds bounding box on ROIs
for curr_parking_lot_row in parking_rows:
tl = curr_parking_lot_row.top_left
br = curr_parking_lot_row.bot_right
cv2.rectangle(res,tl, br, 1, 5)
#displays some intermediate results
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.title('Original, template in blue'), plt.xticks([]), plt.yticks([])
plt.show()
curr_idx = int(0)
#overlay on original picture
f0 = plt.figure(4)
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)),plt.title('Original')
for curr_parking_lot_row in parking_rows:
#creates the region of interest
tl = curr_parking_lot_row.top_left
br = curr_parking_lot_row.bot_right
my_roi = res[tl[1]:br[1],tl[0]:br[0]]
#extracts statistics by column
curr_parking_lot_row.col_mean = np.mean(my_roi, 0)
curr_parking_lot_row.inverted_variance = 1 - np.var(my_roi,0)
curr_parking_lot_row.empty_col_probability = curr_parking_lot_row.col_mean * curr_parking_lot_row.inverted_variance
#creates some plots
f1 = plt.figure(1)
plt.subplot('51%d' % (curr_idx + 1)),plt.plot(curr_parking_lot_row.col_mean),plt.title('Row %d correlation' %(curr_idx + 1))
f2 = plt.figure(2)
plt.subplot('51%d' % (curr_idx + 1)),plt.plot(curr_parking_lot_row.inverted_variance),plt.title('Row %d variance' %(curr_idx + 1))
f3 = plt.figure(3)
plt.subplot('51%d' % (curr_idx + 1))
plt.plot(curr_parking_lot_row.empty_col_probability),plt.title('Row %d empty probability ' %(curr_idx + 1))
plt.plot((1,n),(thresh,thresh),c='r')
#counts empty spaces
num_consec_pixels_over_thresh = 0
curr_col = 0
for prob_val in curr_parking_lot_row.empty_col_probability:
curr_col += 1
if(prob_val > thresh):
num_consec_pixels_over_thresh += 1
else:
num_consec_pixels_over_thresh = 0
if (num_consec_pixels_over_thresh >= car_width):
curr_parking_lot_row.empty_spaces += 1
#adds mark to plt
plt.figure(3) # the probability graph
plt.scatter(curr_col,1,c='g')
plt.figure(4) #parking lot image
plt.scatter(curr_col,curr_parking_lot_row.top_left[1] + 7, c='g')
#to prevent doubel counting cars, just reset the counter
num_consec_pixels_over_thresh = 0
#sets axis range, apparantlly they mess up when adding the scatters
plt.figure(3)
plt.xlim([0,n])
#print out some stats
print('found {0} cars and {1} empty space(s) in row {2}'.format(
curr_parking_lot_row.total_spaces - curr_parking_lot_row.empty_spaces,
curr_parking_lot_row.empty_spaces,
curr_idx +1))
curr_idx += 1
#plot some figures
plt.show()
关于python - 你如何用 Python 在 OpenCV 中计算汽车数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35447293/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m
我想解析一个已经存在的.mid文件,改变它的乐器,例如从“acousticgrandpiano”到“violin”,然后将它保存回去或作为另一个.mid文件。根据我在文档中看到的内容,该乐器通过program_change或patch_change指令进行了更改,但我找不到任何在已经存在的MIDI文件中执行此操作的库.他们似乎都只支持从头开始创建的MIDI文件。 最佳答案 MIDIpackage会为您完成此操作,但具体方法取决于midi文件的原始内容。一个MIDI文件由一个或多个音轨组成,每个音轨是十六个channel中任何一个上的
本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
ValidPalindromeGivenastring,determineifitisapalindrome,consideringonlyalphanumericcharactersandignoringcases. [#125]Example:"Aman,aplan,acanal:Panama"isapalindrome."raceacar"isnotapalindrome.Haveyouconsiderthatthestringmightbeempty?Thisisagoodquestiontoaskduringaninterview.Forthepurposeofthisproblem