2017-08-22 48 views
0

通過閱讀計算器上的幾個答案,我多學會爲止這樣的:OpenCV的,Python的:如何使用面膜參數ORB特徵檢測器

面具必須是一個numpy陣列(具有相同形狀爲圖像),數據類型爲CV_8UC1,值爲0255

這些數字是什麼意思?是否在檢測過程中忽略具有相應掩碼值爲零的任何像素,並且將使用掩碼值爲255的任何像素?它們之間的值呢?

另外,我如何初始化一個numpy陣列與數據類型CV_8UC1在python中?我可以只使用dtype=cv2.CV_8UC1

下面是我目前使用的代碼,基於我上面所做的假設。但問題是,當我爲任一圖像運行detectAndCompute時,我沒有得到任何關鍵點。我有一種感覺可能是因爲掩碼不是正確的數據類型。如果我說得對,那我該如何糾正?

# convert images to grayscale 
base_gray = cv2.cvtColor(self.base, cv2.COLOR_BGRA2GRAY) 
curr_gray = cv2.cvtColor(self.curr, cv2.COLOR_BGRA2GRAY) 

# initialize feature detector 
detector = cv2.ORB_create() 

# create a mask using the alpha channel of the original image--don't 
# use transparent or partially transparent parts 
base_cond = self.base[:,:,3] == 255 
base_mask = np.array(np.where(base_cond, 255, 0)) 

curr_cond = self.base[:,:,3] == 255 
curr_mask = np.array(np.where(curr_cond, 255, 0), dtype=np.uint8) 

# use the mask and grayscale images to detect good features 
base_keys, base_desc = detector.detectAndCompute(base_gray, mask=base_mask) 
curr_keys, curr_desc = detector.detectAndCompute(curr_gray, mask=curr_mask) 

print("base keys: ", base_keys) 
# [] 
print("curr keys: ", curr_keys) 
# [] 
+2

「我該如何初始化numpy數組」 - 您是否嘗試閱讀[numpy關於數據類型的文檔](https://docs.scipy.org/doc/numpy/user/basics.types.html) ? –

+0

問題是,CV_8UC1對應的列表上的數據類型是什麼?我傾向於相信這是因爲8和U,但我還沒有找到任何文件證實這一點。問題是我沒有從 –

+0

http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html - 第一段獲得任何關鍵點。你說得對,'uint8'。 |檢查口罩並確保它們有意義。 –

回答

0

所以在這裏是大多數,如果不是全部的答案:

什麼是這些數字的含義

0意味着忽略該像素和255表示使用它。我仍然不清楚其中的值,但我不認爲所有非零值都被認爲與掩碼中的「等價」爲255。請參閱here

此外,如何初始化數據類型CV_8UC1在Python中的numpy數組?

CV_8U類型是無符號的8位整數,使用numpy爲numpy.uint8。 C1後綴表示該陣列是1通道,而不是3通道彩色圖像和4通道rgba圖像。所以,要創建的無符號8位整數的1通道陣列:

import numpy as np 
np.zeros((480, 720), dtype=np.uint8) 

(三通道陣列將具有形狀(480, 720, 3),四通道(480, 720, 4)等)該掩模將導致檢測器和提取器但忽略整個圖像,因爲它全部爲零。

我該如何糾正[代碼]?

有兩個單獨的問題,每個問題分別導致每個關鍵點數組爲空。

首先,我忘了設定的類型爲base_mask

base_mask = np.array(np.where(base_cond, 255, 0)) # wrong 
base_mask = np.array(np.where(base_cond, 255, 0), dtype=uint8) # right 

其次,我使用了錯誤的圖像生成我curr_cond陣列:

curr_cond = self.base[:,:,3] == 255 # wrong 
curr_cond = self.curr[:,:,3] == 255 # right 

一些非常愚蠢的錯誤。

以下是完整的校正代碼:

# convert images to grayscale 
base_gray = cv2.cvtColor(self.base, cv2.COLOR_BGRA2GRAY) 
curr_gray = cv2.cvtColor(self.curr, cv2.COLOR_BGRA2GRAY) 

# initialize feature detector 
detector = cv2.ORB_create() 

# create a mask using the alpha channel of the original image--don't 
# use transparent or partially transparent parts 
base_cond = self.base[:,:,3] == 255 
base_mask = np.array(np.where(base_cond, 255, 0), dtype=np.uint8) 

curr_cond = self.curr[:,:,3] == 255 
curr_mask = np.array(np.where(curr_cond, 255, 0), dtype=np.uint8) 

# use the mask and grayscale images to detect good features 
base_keys, base_desc = detector.detectAndCompute(base_gray, mask=base_mask) 
curr_keys, curr_desc = detector.detectAndCompute(curr_gray, mask=curr_mask) 

TL; DR:掩模參數是1信道numpy陣列相同的形狀在其中正在努力尋找特徵的灰度級圖像(如果圖像形狀是(480, 720),掩模也是如此)。

數組中的值是np.uint8類型,255表示「使用這個像素」和0的意思是「不」

感謝Dan Mašek領導我這個答案的一部分。