ठीक है, तो .... मैंने शायद इस पर बहुत अधिक काम किया है, लेकिन यह काफी आसान लग रहा था।
मेरे कार्यान्वयन के लिए मैंने फैसला किया कि खाली पार्किंग रिक्त स्थान ढूंढना बेहतर होगा, और मान लें कि अन्य सभी रिक्त स्थान पर कब्जा कर लिया गया है। यह निर्धारित करने के लिए कि कोई स्थान खाली है या नहीं, मैंने बस इसे सड़क के एक पार्किंग स्थान के आकार के हिस्से से तुलना की। इसका मतलब है कि यह वही एल्गोरिदम काम करना चाहिए चाहे वह चमकदार या अंधेरा हो, क्योंकि टेम्पलेट सीधे छवि से निकाला जाता है।
इस बिंदु पर मैं सिर्फ टेम्पलेट मिलान करता हूं (मैंने विभिन्न विधियों की कोशिश की, लेकिन cv2.TM_CCORR_NORMED
सर्वोत्तम काम किया)। यह एक सभ्य परिणाम देता है, और अब हमें इसे संसाधित करने की आवश्यकता है।
मैं पार्किंग स्थल पंक्तियों के आसपास ROI को (ब्याज के क्षेत्रों) पैदा करते हैं। मैं फिर प्रति स्तंभ आंकड़े लेकर उन्हें एक वेक्टर में पतन कर देता हूं। मैं मतलब देखता हूँ।
यह एक बहुत अच्छा संकेतक है, आप पहले से ही स्पष्ट रूप से स्पष्ट रूप से देख सकते हैं कि रिक्त स्थान कहां हैं। लेकिन काले रंग की कारें अभी भी कुछ मुद्दों को पेश करती हैं, इसलिए अब हम एक और आंकड़े देखने का फैसला करते हैं, भिन्नता के बारे में कैसे? पूरे क्षेत्र में पार्किंग स्थल काफी स्थिर है। दूसरी ओर एक कार में कुछ बदलाव हैं, खिड़कियां, छत के दर्पण कुछ भिन्नता के लिए बनाते हैं। तो मैं "उलटा" भिन्नता साजिश। तो 0 के भिन्नता वाले बदलाव के बजाय, इसमें 1 का अंतर है। ऐसा लगता है कि यह
यह बहुत ही आशाजनक दिखता है! लेकिन आप जानते हैं कि क्या बेहतर है? दोनों का मिश्रण! तो चलिए उन्हें एक साथ गुणा करते हैं, मैंने इस परिणाम को "संभावना" कहा क्योंकि यह 0 और
अब आप रिक्त स्थान और एक अंधेरे कार के बीच का अंतर देख सकते हैं। तो चलो कुछ सरल दहलीज करते हैं। यह बहुत अच्छा है, लेकिन यह आपको वाहनों/खाली रिक्त स्थानों की संख्या नहीं देता है। इस बिंदु पर हम कॉलम द्वारा "संभाव्यता" कॉलम से गुजरते हैं, और हम थ्रेसहोल्ड पर लगातार पिक्सल की एक निश्चित संख्या की तलाश करते हैं। कितने पिक्सल? एक कार के रूप में कई पिक्सल चौड़ा है। यह "हिस्ट्रेसिस" प्रकार मॉडल किसी भी चोटियों या नकली डेटा बिंदुओं को दबा देना चाहिए।
और अब यह सब एक साथ आता है, हम यह मान स्थानों की संख्या निरंतर (उचित धारणा मुझे लगता है कि) है और हम सिर्फ number of cars = number of spaces - number of empty spaces
कहना और छवि
निशान और कुछ परिणामों को मुद्रित
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/अजगर परियोजना
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()
एचएम, अपने पार्किंग की जगह लाइनों उठा, तो आप जब तक कुछ विभाजन कर सकता है के रूप में पार्किंग स्थान का आकार "दिया" हो सकता है। यह कहना है, पार्किंग लाइन लाइनों को निकालें, सत्यापित करें कि यह अनुपात/आकार के साथ एक स्थान है, फिर स्पॉट के भीतर क्षेत्र की जांच करके कार होने पर स्पॉट के भीतर जांच करें - शायद समानता या कुछ के कुछ विश्लेषण का उपयोग कर। – ABC
मैं यह नहीं मान सकता कि हमेशा पार्किंग स्पेस लाइनें होंगी। – nickponline
उस मामले में, एचएसवी कलर स्पेस से वी और कुछ मॉर्फोलॉजिकल खेल रहे हैं। शुरू करने के लिए मैं कुछ त्वरित प्राथमिक कोड पोस्ट करूंगा। – ABC