简体   繁体   English

模块“cv2.cv2”没有属性“xfeatures2d”(Opencv 3.4.2.17)

[英]module 'cv2.cv2' has no attribute 'xfeatures2d'(Opencv 3.4.2.17)

hello can someone resolve my problem I am getting this error你好有人可以解决我的问题我收到这个错误

I am working on an image stitching project and have installed OpenCV and OpenCV-contrib version - 3.4.2.17 still getting this error我正在处理一个图像拼接项目并安装了 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'

while running this line of code:在运行这行代码时:

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)

the class is this: 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

Note: I am using opencv version 3.4.2.17 and opencv-contrib-3.4.2.17注意: I am using opencv version 3.4.2.17 and opencv-contrib-3.4.2.17

using the following on Kaggle notebook:在 Kaggle 笔记本上使用以下内容:

https://www.kaggle.com/deepzsenu/image-stitching/ https://www.kaggle.com/deepzsenu/image-stitching/

the above is the link to my notebook:以上是我笔记本的链接:

Thank you谢谢

hi every I have solved the above error don't downgrade your OpenCV package directly first use大家好,我已经解决了上述错误,不要直接降级你的 OpenCV package 第一次使用

!pip uninstall opencv-python -y

then I install only the opencv contrib using然后我只安装 opencv contrib 使用

!pip install -U opencv-contrib-python=3.4.2.17

just use cv2.SIFT_create() try this out只需使用 cv2.SIFT_create() 试试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块“cv2.cv2”没有属性“xfeatures2d”,模块“cv2.cv2”没有属性“SIFT” - module 'cv2.cv2' has no attribute 'xfeatures2d' and module 'cv2.cv2' has no attribute 'SIFT' 模块 &#39;cv2.cv2&#39; 没有属性 &#39;sift&#39; - module 'cv2.cv2' has no attribute 'sift' 模块“cv2.cv2”没有属性“PutText” - module 'cv2.cv2' has no attribute 'PutText' 接收错误:AttributeError:尝试调用openCV方法时,模块&#39;cv2.cv2&#39;没有属性&#39;CompareHist&#39; - Receiving error: AttributeError: module 'cv2.cv2' has no attribute 'CompareHist' when trying to call a openCV method 如何修复错误“模块&#39;cv2.cv2&#39;没有属性setMouseCallBack?” - How to fix the error "module 'cv2.cv2' has no attribute setMouseCallBack?" AttributeError:模块&#39;cv2.cv2&#39;没有属性&#39;rectange&#39; - AttributeError: module 'cv2.cv2' has no attribute 'rectange' 模块“cv2.cv2”没有属性“dnn_superres” - module 'cv2.cv2' has no attribute 'dnn_superres' OpenCV AttributeError 模块“cv2.cv2”没有属性“TrackerBoosting_create” - OpenCV AttributeError module 'cv2.cv2' has no attribute 'TrackerBoosting_create' AttributeError: 模块 &#39;cv2.cv2&#39; 没有属性 &#39;release&#39; - AttributeError: module 'cv2.cv2' has no attribute 'release' AttributeError: 模块 &#39;cv2.cv2&#39; 没有属性 &#39;CAP_PROP_ORIENTATION_META&#39; - AttributeError: module 'cv2.cv2' has no attribute 'CAP_PROP_ORIENTATION_META'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM