[英]module 'cv2.cv2' has no attribute 'xfeatures2d' and module 'cv2.cv2' has no attribute 'SIFT'
[英]module 'cv2.cv2' has no attribute 'xfeatures2d'(Opencv 3.4.2.17)
你好有人可以解決我的問題我收到這個錯誤
我正在處理一個圖像拼接項目並安裝了 OpenCV 和 OpenCV-contrib 版本 - 3.4.2.17 仍然出現此錯誤
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-243a893afb39> in <module>
3 (result, matched_points) = panaroma.image_stitch([images[0], images[1]], match_status=True)
4 else:
----> 5 (result, matched_points) = panaroma.image_stitch([images[no_of_images-2], images[no_of_images-1]], match_status=True)
6 for i in range(no_of_images - 2):
7 (result, matched_points) = panaroma.image_stitch([images[no_of_images-i-3],result], match_status=True)
<ipython-input-14-2f5bd4f316a7> in image_stitch(self, images, lowe_ratio, max_Threshold, match_status)
5 #detect the features and keypoints from SIFT
6 (imageB, imageA) = images
----> 7 (KeypointsA, features_of_A) = self.Detect_Feature_And_KeyPoints(imageA)
8 (KeypointsB, features_of_B) = self.Detect_Feature_And_KeyPoints(imageB)
9
<ipython-input-14-2f5bd4f316a7> in Detect_Feature_And_KeyPoints(self, image)
37
38 # detect and extract features from the image
---> 39 descriptors = cv2.xfeatures2d.SIFT_create()
40 (Keypoints, features) = descriptors.detectAndCompute(image, None)
41
AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'
在運行這行代碼時:
panaroma = Panaroma()
if no_of_images==2:
(result, matched_points) = panaroma.image_stitch([images[0], images[1]], match_status=True)
else:
(result, matched_points) = panaroma.image_stitch([images[no_of_images-2], images[no_of_images-1]], match_status=True)
for i in range(no_of_images - 2):
(result, matched_points) = panaroma.image_stitch([images[no_of_images-i-3],result], match_status=True)
class 是這樣的:
class Panaroma:
def image_stitch(self, images, lowe_ratio=0.75, max_Threshold=4.0,match_status=False):
#detect the features and keypoints from SIFT
(imageB, imageA) = images
(KeypointsA, features_of_A) = self.Detect_Feature_And_KeyPoints(imageA)
(KeypointsB, features_of_B) = self.Detect_Feature_And_KeyPoints(imageB)
#got the valid matched points
Values = self.matchKeypoints(KeypointsA, KeypointsB,features_of_A, features_of_B, lowe_ratio, max_Threshold)
if Values is None:
return None
#to get perspective of image using computed homography
(matches, Homography, status) = Values
result_image = self.getwarp_perspective(imageA,imageB,Homography)
result_image[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
# check to see if the keypoint matches should be visualized
if match_status:
vis = self.draw_Matches(imageA, imageB, KeypointsA, KeypointsB, matches,status)
return (result_image, vis)
return result_image
def getwarp_perspective(self,imageA,imageB,Homography):
val = imageA.shape[1] + imageB.shape[1]
result_image = cv2.warpPerspective(imageA, Homography, (val , imageA.shape[0]))
return result_image
def Detect_Feature_And_KeyPoints(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect and extract features from the image
descriptors = cv2.xfeatures2d.SIFT_create()
(Keypoints, features) = descriptors.detectAndCompute(image, None)
Keypoints = np.float32([i.pt for i in Keypoints])
return (Keypoints, features)
def get_Allpossible_Match(self,featuresA,featuresB):
# compute the all matches using euclidean distance and opencv provide
#DescriptorMatcher_create() function for that
match_instance = cv2.DescriptorMatcher_create("BruteForce")
All_Matches = match_instance.knnMatch(featuresA, featuresB, 2)
return All_Matches
def All_validmatches(self,AllMatches,lowe_ratio):
#to get all valid matches according to lowe concept..
valid_matches = []
for val in AllMatches:
if len(val) == 2 and val[0].distance < val[1].distance * lowe_ratio:
valid_matches.append((val[0].trainIdx, val[0].queryIdx))
return valid_matches
def Compute_Homography(self,pointsA,pointsB,max_Threshold):
#to compute homography using points in both images
(H, status) = cv2.findHomography(pointsA, pointsB, cv2.RANSAC, max_Threshold)
return (H,status)
def matchKeypoints(self, KeypointsA, KeypointsB, featuresA, featuresB,lowe_ratio, max_Threshold):
AllMatches = self.get_Allpossible_Match(featuresA,featuresB);
valid_matches = self.All_validmatches(AllMatches,lowe_ratio)
if len(valid_matches) > 4:
# construct the two sets of points
pointsA = np.float32([KeypointsA[i] for (_,i) in valid_matches])
pointsB = np.float32([KeypointsB[i] for (i,_) in valid_matches])
(Homograpgy, status) = self.Compute_Homography(pointsA, pointsB, max_Threshold)
return (valid_matches, Homograpgy, status)
else:
return None
def get_image_dimension(self,image):
(h,w) = image.shape[:2]
return (h,w)
def get_points(self,imageA,imageB):
(hA, wA) = self.get_image_dimension(imageA)
(hB, wB) = self.get_image_dimension(imageB)
vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
vis[0:hA, 0:wA] = imageA
vis[0:hB, wA:] = imageB
return vis
def draw_Matches(self, imageA, imageB, KeypointsA, KeypointsB, matches, status):
(hA,wA) = self.get_image_dimension(imageA)
vis = self.get_points(imageA,imageB)
# loop over the matches
for ((trainIdx, queryIdx), s) in zip(matches, status):
if s == 1:
ptA = (int(KeypointsA[queryIdx][0]), int(KeypointsA[queryIdx][1]))
ptB = (int(KeypointsB[trainIdx][0]) + wA, int(KeypointsB[trainIdx][1]))
cv2.line(vis, ptA, ptB, (0, 255, 0), 1)
return vis
注意: I am using opencv version 3.4.2.17 and opencv-contrib-3.4.2.17
在 Kaggle 筆記本上使用以下內容:
https://www.kaggle.com/deepzsenu/image-stitching/
以上是我筆記本的鏈接:
謝謝
大家好,我已經解決了上述錯誤,不要直接降級你的 OpenCV package 第一次使用
!pip uninstall opencv-python -y
然后我只安裝 opencv contrib 使用
!pip install -U opencv-contrib-python=3.4.2.17
只需使用 cv2.SIFT_create() 試試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.