Error while using the stitching_detailed.py code on some images

Dear OpenCV Team,

I am facing an error while using the stitching_detailed.py code on some images. I am running the python code with Jupyter-notebook, but the error is corresponding to the cpp file as shown below.
How can I solve this problem, what should I do ? I would be so pleased if you help me with this problem, I googled a lot, but I did not find any solution for it. I also tried asking ChatGPT as shown below, but it did not work as well.


error Traceback (most recent call last)
File ~\My Python Stuff\Stitching Project - ICCAS\Using Stitching Algorithm 1\Image Stitching in OpenCV\stitching_detailed_old.py:521
517 print(“Done”)
520 if name == ‘main’:
→ 521 main()
522 cv.destroyAllWindows()

File ~\My Python Stuff\Stitching Project - ICCAS\Using Stitching Algorithm 1\Image Stitching in OpenCV\stitching_detailed_old.py:341, in main()
338 images.append(img)
340 matcher = get_matcher(args)
→ 341 p = matcher.apply2(features)
342 matcher.collectGarbage()
344 if save_graph:

error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\flann\src\miniflann.cpp:521: error: (-215:Assertion failed) (size_t)knn <= index_->size() in function ‘cv::flann::runKnnSearch_’.

C++ File where the error is happening (The affected part of the code):

According to Chat-GPT:

The error you’re encountering is related to the FLANN (Fast Library for Approximate Nearest Neighbors) module in OpenCV. Specifically, the error message indicates that there’s an assertion failure in the miniflann.cpp file at line 521. This assertion failure is likely due to an invalid size for the k-nearest neighbors (knn) parameter.

To address this issue, you can modify the get_matcher function to handle this situation more gracefully. Update the get_matcher function as follows:

pythonCopy code

def get_matcher(args):
    try_cuda = args.try_cuda
    matcher_type = args.matcher
    if args.match_conf is None:
        if args.features == 'orb':
            match_conf = 0.3
        else:
            match_conf = 0.65
    else:
        match_conf = args.match_conf
    range_width = args.rangewidth
    if matcher_type == "affine":
        matcher = cv.detail_AffineBestOf2NearestMatcher(False, try_cuda, match_conf)
    elif range_width == -1:
        matcher = cv.detail_BestOf2NearestMatcher(try_cuda, match_conf)
    else:
        try:
            matcher = cv.detail_BestOf2NearestRangeMatcher(range_width, try_cuda, match_conf)
        except cv.error as e:
            print(f"Error in creating BestOf2NearestRangeMatcher: {e}")
            matcher = cv.detail_BestOf2NearestMatcher(try_cuda, match_conf)  # Fallback to BestOf2NearestMatcher
    return matcher

This modification includes a try-except block to catch any potential errors when creating a BestOf2NearestRangeMatcher. If an error occurs, it falls back to creating a BestOf2NearestMatcher. This change is intended to provide more robust handling of the FLANN error you encountered.

Please replace the existing get_matcher function in your script with this updated version and see if it resolves the issue.


I tried it, but it did not solve the problem.
Hope you can help me fix this error.
Thanks a lot in advance and I wish you all Merry Christmas and a happy new year.

Best regards,
MedIWA

may i ask you to print out the features (or at least their shape) before

341 p = matcher.apply2(features)

and show us ?

my explanation of the exception would be like:
if the size of the flann index is less than 2 (the K in knn here), it probably means, you did not pass any valid features (to build the index) at all

Hallo Sir,

thank you for your quick response.
This is the result which I get after running the code with the print statement before line 341.

SURF not available
['images/frames_output1\\frame0.jpg', 'images/frames_output1\\frame1.jpg', 'images/frames_output1\\frame10.jpg', 'images/frames_output1\\frame100.jpg', 'images/frames_output1\\frame101.jpg', 'images/frames_output1\\frame102.jpg', 'images/frames_output1\\frame103.jpg', 'images/frames_output1\\frame104.jpg', 'images/frames_output1\\frame105.jpg', 'images/frames_output1\\frame106.jpg', 'images/frames_output1\\frame107.jpg', 'images/frames_output1\\frame108.jpg', 'images/frames_output1\\frame109.jpg', 'images/frames_output1\\frame11.jpg', 'images/frames_output1\\frame110.jpg', 'images/frames_output1\\frame111.jpg', 'images/frames_output1\\frame112.jpg', 'images/frames_output1\\frame113.jpg', 'images/frames_output1\\frame114.jpg', 'images/frames_output1\\frame115.jpg', 'images/frames_output1\\frame116.jpg', 'images/frames_output1\\frame117.jpg', 'images/frames_output1\\frame118.jpg', 'images/frames_output1\\frame119.jpg', 'images/frames_output1\\frame12.jpg', 'images/frames_output1\\frame120.jpg', 'images/frames_output1\\frame121.jpg', 'images/frames_output1\\frame122.jpg', 'images/frames_output1\\frame123.jpg', 'images/frames_output1\\frame124.jpg', 'images/frames_output1\\frame125.jpg', 'images/frames_output1\\frame126.jpg', 'images/frames_output1\\frame127.jpg', 'images/frames_output1\\frame128.jpg', 'images/frames_output1\\frame129.jpg', 'images/frames_output1\\frame13.jpg', 'images/frames_output1\\frame130.jpg', 'images/frames_output1\\frame131.jpg', 'images/frames_output1\\frame132.jpg', 'images/frames_output1\\frame133.jpg', 'images/frames_output1\\frame134.jpg', 'images/frames_output1\\frame135.jpg', 'images/frames_output1\\frame136.jpg', 'images/frames_output1\\frame137.jpg', 'images/frames_output1\\frame138.jpg', 'images/frames_output1\\frame139.jpg', 'images/frames_output1\\frame14.jpg', 'images/frames_output1\\frame140.jpg', 'images/frames_output1\\frame141.jpg', 'images/frames_output1\\frame142.jpg', 'images/frames_output1\\frame143.jpg', 'images/frames_output1\\frame144.jpg', 'images/frames_output1\\frame145.jpg', 'images/frames_output1\\frame146.jpg', 'images/frames_output1\\frame147.jpg', 'images/frames_output1\\frame148.jpg', 'images/frames_output1\\frame149.jpg', 'images/frames_output1\\frame15.jpg', 'images/frames_output1\\frame150.jpg', 'images/frames_output1\\frame151.jpg', 'images/frames_output1\\frame152.jpg', 'images/frames_output1\\frame153.jpg', 'images/frames_output1\\frame154.jpg', 'images/frames_output1\\frame155.jpg', 'images/frames_output1\\frame156.jpg', 'images/frames_output1\\frame157.jpg', 'images/frames_output1\\frame158.jpg', 'images/frames_output1\\frame159.jpg', 'images/frames_output1\\frame16.jpg', 'images/frames_output1\\frame160.jpg', 'images/frames_output1\\frame161.jpg', 'images/frames_output1\\frame162.jpg', 'images/frames_output1\\frame163.jpg', 'images/frames_output1\\frame164.jpg', 'images/frames_output1\\frame165.jpg', 'images/frames_output1\\frame166.jpg', 'images/frames_output1\\frame167.jpg', 'images/frames_output1\\frame168.jpg', 'images/frames_output1\\frame169.jpg', 'images/frames_output1\\frame17.jpg', 'images/frames_output1\\frame170.jpg', 'images/frames_output1\\frame171.jpg', 'images/frames_output1\\frame172.jpg', 'images/frames_output1\\frame173.jpg', 'images/frames_output1\\frame174.jpg', 'images/frames_output1\\frame175.jpg', 'images/frames_output1\\frame176.jpg', 'images/frames_output1\\frame177.jpg', 'images/frames_output1\\frame178.jpg', 'images/frames_output1\\frame179.jpg', 'images/frames_output1\\frame18.jpg', 'images/frames_output1\\frame180.jpg', 'images/frames_output1\\frame181.jpg', 'images/frames_output1\\frame182.jpg', 'images/frames_output1\\frame183.jpg', 'images/frames_output1\\frame184.jpg', 'images/frames_output1\\frame185.jpg', 'images/frames_output1\\frame186.jpg', 'images/frames_output1\\frame187.jpg', 'images/frames_output1\\frame188.jpg', 'images/frames_output1\\frame189.jpg', 'images/frames_output1\\frame19.jpg', 'images/frames_output1\\frame190.jpg', 'images/frames_output1\\frame191.jpg', 'images/frames_output1\\frame192.jpg', 'images/frames_output1\\frame193.jpg', 'images/frames_output1\\frame194.jpg', 'images/frames_output1\\frame195.jpg', 'images/frames_output1\\frame196.jpg', 'images/frames_output1\\frame197.jpg', 'images/frames_output1\\frame198.jpg', 'images/frames_output1\\frame199.jpg', 'images/frames_output1\\frame2.jpg', 'images/frames_output1\\frame20.jpg', 'images/frames_output1\\frame200.jpg', 'images/frames_output1\\frame201.jpg', 'images/frames_output1\\frame202.jpg', 'images/frames_output1\\frame203.jpg', 'images/frames_output1\\frame204.jpg', 'images/frames_output1\\frame205.jpg', 'images/frames_output1\\frame206.jpg', 'images/frames_output1\\frame207.jpg', 'images/frames_output1\\frame208.jpg', 'images/frames_output1\\frame209.jpg', 'images/frames_output1\\frame21.jpg', 'images/frames_output1\\frame210.jpg', 'images/frames_output1\\frame211.jpg', 'images/frames_output1\\frame212.jpg', 'images/frames_output1\\frame213.jpg', 'images/frames_output1\\frame214.jpg', 'images/frames_output1\\frame215.jpg', 'images/frames_output1\\frame216.jpg', 'images/frames_output1\\frame217.jpg', 'images/frames_output1\\frame218.jpg', 'images/frames_output1\\frame219.jpg', 'images/frames_output1\\frame22.jpg', 'images/frames_output1\\frame220.jpg', 'images/frames_output1\\frame221.jpg', 'images/frames_output1\\frame222.jpg', 'images/frames_output1\\frame223.jpg', 'images/frames_output1\\frame224.jpg', 'images/frames_output1\\frame225.jpg', 'images/frames_output1\\frame226.jpg', 'images/frames_output1\\frame227.jpg', 'images/frames_output1\\frame228.jpg', 'images/frames_output1\\frame229.jpg', 'images/frames_output1\\frame23.jpg', 'images/frames_output1\\frame230.jpg', 'images/frames_output1\\frame231.jpg', 'images/frames_output1\\frame232.jpg', 'images/frames_output1\\frame233.jpg', 'images/frames_output1\\frame234.jpg', 'images/frames_output1\\frame235.jpg', 'images/frames_output1\\frame236.jpg', 'images/frames_output1\\frame237.jpg', 'images/frames_output1\\frame238.jpg', 'images/frames_output1\\frame239.jpg', 'images/frames_output1\\frame24.jpg', 'images/frames_output1\\frame240.jpg', 'images/frames_output1\\frame241.jpg', 'images/frames_output1\\frame242.jpg', 'images/frames_output1\\frame243.jpg', 'images/frames_output1\\frame244.jpg', 'images/frames_output1\\frame245.jpg', 'images/frames_output1\\frame246.jpg', 'images/frames_output1\\frame247.jpg', 'images/frames_output1\\frame248.jpg', 'images/frames_output1\\frame249.jpg', 'images/frames_output1\\frame25.jpg', 'images/frames_output1\\frame250.jpg', 'images/frames_output1\\frame251.jpg', 'images/frames_output1\\frame252.jpg', 'images/frames_output1\\frame253.jpg', 'images/frames_output1\\frame254.jpg', 'images/frames_output1\\frame255.jpg', 'images/frames_output1\\frame256.jpg', 'images/frames_output1\\frame257.jpg', 'images/frames_output1\\frame258.jpg', 'images/frames_output1\\frame259.jpg', 'images/frames_output1\\frame26.jpg', 'images/frames_output1\\frame260.jpg', 'images/frames_output1\\frame261.jpg', 'images/frames_output1\\frame262.jpg', 'images/frames_output1\\frame263.jpg', 'images/frames_output1\\frame264.jpg', 'images/frames_output1\\frame265.jpg', 'images/frames_output1\\frame266.jpg', 'images/frames_output1\\frame267.jpg', 'images/frames_output1\\frame268.jpg', 'images/frames_output1\\frame269.jpg', 'images/frames_output1\\frame27.jpg', 'images/frames_output1\\frame270.jpg', 'images/frames_output1\\frame271.jpg', 'images/frames_output1\\frame272.jpg', 'images/frames_output1\\frame273.jpg', 'images/frames_output1\\frame274.jpg', 'images/frames_output1\\frame275.jpg', 'images/frames_output1\\frame276.jpg', 'images/frames_output1\\frame277.jpg', 'images/frames_output1\\frame278.jpg', 'images/frames_output1\\frame279.jpg', 'images/frames_output1\\frame28.jpg', 'images/frames_output1\\frame280.jpg', 'images/frames_output1\\frame29.jpg', 'images/frames_output1\\frame3.jpg', 'images/frames_output1\\frame30.jpg', 'images/frames_output1\\frame31.jpg', 'images/frames_output1\\frame32.jpg', 'images/frames_output1\\frame33.jpg', 'images/frames_output1\\frame34.jpg', 'images/frames_output1\\frame35.jpg', 'images/frames_output1\\frame36.jpg', 'images/frames_output1\\frame37.jpg', 'images/frames_output1\\frame38.jpg', 'images/frames_output1\\frame39.jpg', 'images/frames_output1\\frame4.jpg', 'images/frames_output1\\frame40.jpg', 'images/frames_output1\\frame41.jpg', 'images/frames_output1\\frame42.jpg', 'images/frames_output1\\frame43.jpg', 'images/frames_output1\\frame44.jpg', 'images/frames_output1\\frame45.jpg', 'images/frames_output1\\frame46.jpg', 'images/frames_output1\\frame47.jpg', 'images/frames_output1\\frame48.jpg', 'images/frames_output1\\frame49.jpg', 'images/frames_output1\\frame5.jpg', 'images/frames_output1\\frame50.jpg', 'images/frames_output1\\frame51.jpg', 'images/frames_output1\\frame52.jpg', 'images/frames_output1\\frame53.jpg', 'images/frames_output1\\frame54.jpg', 'images/frames_output1\\frame55.jpg', 'images/frames_output1\\frame56.jpg', 'images/frames_output1\\frame57.jpg', 'images/frames_output1\\frame58.jpg', 'images/frames_output1\\frame59.jpg', 'images/frames_output1\\frame6.jpg', 'images/frames_output1\\frame60.jpg', 'images/frames_output1\\frame61.jpg', 'images/frames_output1\\frame62.jpg', 'images/frames_output1\\frame63.jpg', 'images/frames_output1\\frame64.jpg', 'images/frames_output1\\frame65.jpg', 'images/frames_output1\\frame66.jpg', 'images/frames_output1\\frame67.jpg', 'images/frames_output1\\frame68.jpg', 'images/frames_output1\\frame69.jpg', 'images/frames_output1\\frame7.jpg', 'images/frames_output1\\frame70.jpg', 'images/frames_output1\\frame71.jpg', 'images/frames_output1\\frame72.jpg', 'images/frames_output1\\frame73.jpg', 'images/frames_output1\\frame74.jpg', 'images/frames_output1\\frame75.jpg', 'images/frames_output1\\frame76.jpg', 'images/frames_output1\\frame77.jpg', 'images/frames_output1\\frame78.jpg', 'images/frames_output1\\frame79.jpg', 'images/frames_output1\\frame8.jpg', 'images/frames_output1\\frame80.jpg', 'images/frames_output1\\frame81.jpg', 'images/frames_output1\\frame82.jpg', 'images/frames_output1\\frame83.jpg', 'images/frames_output1\\frame84.jpg', 'images/frames_output1\\frame85.jpg', 'images/frames_output1\\frame86.jpg', 'images/frames_output1\\frame87.jpg', 'images/frames_output1\\frame88.jpg', 'images/frames_output1\\frame89.jpg', 'images/frames_output1\\frame9.jpg', 'images/frames_output1\\frame90.jpg', 'images/frames_output1\\frame91.jpg', 'images/frames_output1\\frame92.jpg', 'images/frames_output1\\frame93.jpg', 'images/frames_output1\\frame94.jpg', 'images/frames_output1\\frame95.jpg', 'images/frames_output1\\frame96.jpg', 'images/frames_output1\\frame97.jpg', 'images/frames_output1\\frame98.jpg', 'images/frames_output1\\frame99.jpg']
[< cv2.detail.ImageFeatures 000002B5185E3EA0>, < cv2.detail.ImageFeatures 000002B5185E3D80>, < cv2.detail.ImageFeatures 000002B56ED94ED0>, < cv2.detail.ImageFeatures 000002B5185BC0C0>, < cv2.detail.ImageFeatures 000002B5185BC150>, < cv2.detail.ImageFeatures 000002B5185BC1E0>, < cv2.detail.ImageFeatures 000002B5185BC270>, < cv2.detail.ImageFeatures 000002B5185BC300>, < cv2.detail.ImageFeatures 000002B5185BC390>, < cv2.detail.ImageFeatures 000002B5185BC420>, < cv2.detail.ImageFeatures 000002B5185BC4B0>, < cv2.detail.ImageFeatures 000002B5185BC540>, < cv2.detail.ImageFeatures 000002B5185BC5D0>, < cv2.detail.ImageFeatures 000002B5185BC660>, < cv2.detail.ImageFeatures 000002B5185BC6F0>, < cv2.detail.ImageFeatures 000002B5185BC780>, < cv2.detail.ImageFeatures 000002B5185BC810>, < cv2.detail.ImageFeatures 000002B5185BC8A0>, < cv2.detail.ImageFeatures 000002B5185BC930>, < cv2.detail.ImageFeatures 000002B5185BC9C0>, < cv2.detail.ImageFeatures 000002B5185BCA50>, < cv2.detail.ImageFeatures 000002B5185BCAE0>, < cv2.detail.ImageFeatures 000002B5185BCB70>, < cv2.detail.ImageFeatures 000002B5185BCC00>, < cv2.detail.ImageFeatures 000002B5185BCC90>, < cv2.detail.ImageFeatures 000002B5185BCD20>, < cv2.detail.ImageFeatures 000002B5185BCDB0>, < cv2.detail.ImageFeatures 000002B5185BCE40>, < cv2.detail.ImageFeatures 000002B5185BCED0>, < cv2.detail.ImageFeatures 000002B5185BCF60>, < cv2.detail.ImageFeatures 000002B5185BCFF0>, < cv2.detail.ImageFeatures 000002B5185BD080>, < cv2.detail.ImageFeatures 000002B5185BD110>, < cv2.detail.ImageFeatures 000002B5185BD1A0>, < cv2.detail.ImageFeatures 000002B5185BD230>, < cv2.detail.ImageFeatures 000002B5185BD2C0>, < cv2.detail.ImageFeatures 000002B5185BD350>, < cv2.detail.ImageFeatures 000002B5185BD3E0>, < cv2.detail.ImageFeatures 000002B5185BD470>, < cv2.detail.ImageFeatures 000002B5185BD500>, < cv2.detail.ImageFeatures 000002B5185BD590>, < cv2.detail.ImageFeatures 000002B5185BD620>, < cv2.detail.ImageFeatures 000002B5185BD6B0>, < cv2.detail.ImageFeatures 000002B5185BD740>, < cv2.detail.ImageFeatures 000002B5185BD7D0>, < cv2.detail.ImageFeatures 000002B5185BD860>, < cv2.detail.ImageFeatures 000002B5185BD8F0>, < cv2.detail.ImageFeatures 000002B5185BD980>, < cv2.detail.ImageFeatures 000002B5185BDA10>, < cv2.detail.ImageFeatures 000002B5185BDAA0>, < cv2.detail.ImageFeatures 000002B5185BDB30>, < cv2.detail.ImageFeatures 000002B5185BDBC0>, < cv2.detail.ImageFeatures 000002B5185BDC50>, < cv2.detail.ImageFeatures 000002B5185BDCE0>, < cv2.detail.ImageFeatures 000002B5185BDD70>, < cv2.detail.ImageFeatures 000002B5185BDE00>, < cv2.detail.ImageFeatures 000002B5185BDE90>, < cv2.detail.ImageFeatures 000002B5185BDF20>, < cv2.detail.ImageFeatures 000002B5185BDFB0>, < cv2.detail.ImageFeatures 000002B5185BE040>, < cv2.detail.ImageFeatures 000002B5185BE0D0>, < cv2.detail.ImageFeatures 000002B5185BE160>, < cv2.detail.ImageFeatures 000002B5185BE1F0>, < cv2.detail.ImageFeatures 000002B5185BE280>, < cv2.detail.ImageFeatures 000002B5185BE310>, < cv2.detail.ImageFeatures 000002B5185BE3A0>, < cv2.detail.ImageFeatures 000002B5185BE430>, < cv2.detail.ImageFeatures 000002B5185BE4C0>, < cv2.detail.ImageFeatures 000002B5185BE550>, < cv2.detail.ImageFeatures 000002B5185BE5E0>, < cv2.detail.ImageFeatures 000002B5185BE670>, < cv2.detail.ImageFeatures 000002B5185BE700>, < cv2.detail.ImageFeatures 000002B5185BE790>, < cv2.detail.ImageFeatures 000002B5185BE820>, < cv2.detail.ImageFeatures 000002B5185BE8B0>, < cv2.detail.ImageFeatures 000002B5185BE940>, < cv2.detail.ImageFeatures 000002B5185BE9D0>, < cv2.detail.ImageFeatures 000002B5185BEA60>, < cv2.detail.ImageFeatures 000002B5185BEAF0>, < cv2.detail.ImageFeatures 000002B5185BEB80>, < cv2.detail.ImageFeatures 000002B5185BEC10>, < cv2.detail.ImageFeatures 000002B5185BECA0>, < cv2.detail.ImageFeatures 000002B5185BED30>, < cv2.detail.ImageFeatures 000002B5185BEDC0>, < cv2.detail.ImageFeatures 000002B5185BEE50>, < cv2.detail.ImageFeatures 000002B5185BEEE0>, < cv2.detail.ImageFeatures 000002B5185BEF70>, < cv2.detail.ImageFeatures 000002B5185BF000>, < cv2.detail.ImageFeatures 000002B5185BF090>, < cv2.detail.ImageFeatures 000002B5185BF120>, < cv2.detail.ImageFeatures 000002B5185BF1B0>, < cv2.detail.ImageFeatures 000002B5185BF240>, < cv2.detail.ImageFeatures 000002B5185BF2D0>, < cv2.detail.ImageFeatures 000002B5185BF360>, < cv2.detail.ImageFeatures 000002B5185BF3F0>, < cv2.detail.ImageFeatures 000002B5185BF480>, < cv2.detail.ImageFeatures 000002B5185BF510>, < cv2.detail.ImageFeatures 000002B5185BF5A0>, < cv2.detail.ImageFeatures 000002B5185BF630>, < cv2.detail.ImageFeatures 000002B5185BF6C0>, < cv2.detail.ImageFeatures 000002B5185BF750>, < cv2.detail.ImageFeatures 000002B5185BF7E0>, < cv2.detail.ImageFeatures 000002B5185BF870>, < cv2.detail.ImageFeatures 000002B5185BF900>, < cv2.detail.ImageFeatures 000002B5185BF990>, < cv2.detail.ImageFeatures 000002B5185BFA20>, < cv2.detail.ImageFeatures 000002B5185BFAB0>, < cv2.detail.ImageFeatures 000002B5185BFB40>, < cv2.detail.ImageFeatures 000002B5185BFBD0>, < cv2.detail.ImageFeatures 000002B5185BFC60>, < cv2.detail.ImageFeatures 000002B5185BFCF0>, < cv2.detail.ImageFeatures 000002B5185BFD80>, < cv2.detail.ImageFeatures 000002B5185BFE10>, < cv2.detail.ImageFeatures 000002B5185BFEA0>, < cv2.detail.ImageFeatures 000002B5185BFF30>, < cv2.detail.ImageFeatures 000002B5185D4030>, < cv2.detail.ImageFeatures 000002B5185D40C0>, < cv2.detail.ImageFeatures 000002B5185D4150>, < cv2.detail.ImageFeatures 000002B5185D41E0>, < cv2.detail.ImageFeatures 000002B5185D4270>, < cv2.detail.ImageFeatures 000002B5185D4300>, < cv2.detail.ImageFeatures 000002B5185D4390>, < cv2.detail.ImageFeatures 000002B5185D4420>, < cv2.detail.ImageFeatures 000002B5185D44B0>, < cv2.detail.ImageFeatures 000002B5185D4540>, < cv2.detail.ImageFeatures 000002B5185D45D0>, < cv2.detail.ImageFeatures 000002B5185D4660>, < cv2.detail.ImageFeatures 000002B5185D46F0>, < cv2.detail.ImageFeatures 000002B5185D4780>, < cv2.detail.ImageFeatures 000002B5185D4810>, < cv2.detail.ImageFeatures 000002B5185D48A0>, < cv2.detail.ImageFeatures 000002B5185D4930>, < cv2.detail.ImageFeatures 000002B5185D49C0>, < cv2.detail.ImageFeatures 000002B5185D4A50>, < cv2.detail.ImageFeatures 000002B5185D4AE0>, < cv2.detail.ImageFeatures 000002B5185D4B70>, < cv2.detail.ImageFeatures 000002B5185D4C00>, < cv2.detail.ImageFeatures 000002B5185D4C90>, < cv2.detail.ImageFeatures 000002B5185D4D20>, < cv2.detail.ImageFeatures 000002B5185D4DB0>, < cv2.detail.ImageFeatures 000002B5185D4E40>, < cv2.detail.ImageFeatures 000002B5185D4ED0>, < cv2.detail.ImageFeatures 000002B5185D4F60>, < cv2.detail.ImageFeatures 000002B5185D4FF0>, < cv2.detail.ImageFeatures 000002B5185D5080>, < cv2.detail.ImageFeatures 000002B5185D5110>, < cv2.detail.ImageFeatures 000002B5185D51A0>, < cv2.detail.ImageFeatures 000002B5185D5230>, < cv2.detail.ImageFeatures 000002B5185D52C0>, < cv2.detail.ImageFeatures 000002B5185D5350>, < cv2.detail.ImageFeatures 000002B5185D53E0>, < cv2.detail.ImageFeatures 000002B5185D5470>, < cv2.detail.ImageFeatures 000002B5185D5500>, < cv2.detail.ImageFeatures 000002B5185D5590>, < cv2.detail.ImageFeatures 000002B5185D5620>, < cv2.detail.ImageFeatures 000002B5185D56B0>, < cv2.detail.ImageFeatures 000002B5185D5740>, < cv2.detail.ImageFeatures 000002B5185D57D0>, < cv2.detail.ImageFeatures 000002B5185D5860>, < cv2.detail.ImageFeatures 000002B5185D58F0>, < cv2.detail.ImageFeatures 000002B5185D5980>, < cv2.detail.ImageFeatures 000002B5185D5A10>, < cv2.detail.ImageFeatures 000002B5185D5AA0>, < cv2.detail.ImageFeatures 000002B5185D5B30>, < cv2.detail.ImageFeatures 000002B5185D5BC0>, < cv2.detail.ImageFeatures 000002B5185D5C50>, < cv2.detail.ImageFeatures 000002B5185D5CE0>, < cv2.detail.ImageFeatures 000002B5185D5D70>, < cv2.detail.ImageFeatures 000002B5185D5E00>, < cv2.detail.ImageFeatures 000002B5185D5E90>, < cv2.detail.ImageFeatures 000002B5185D5F20>, < cv2.detail.ImageFeatures 000002B5185D5FB0>, < cv2.detail.ImageFeatures 000002B5185D6040>, < cv2.detail.ImageFeatures 000002B5185D60D0>, < cv2.detail.ImageFeatures 000002B5185D6160>, < cv2.detail.ImageFeatures 000002B5185D61F0>, < cv2.detail.ImageFeatures 000002B5185D6280>, < cv2.detail.ImageFeatures 000002B5185D6310>, < cv2.detail.ImageFeatures 000002B5185D63A0>, < cv2.detail.ImageFeatures 000002B5185D6430>, < cv2.detail.ImageFeatures 000002B5185D64C0>, < cv2.detail.ImageFeatures 000002B5185D6550>, < cv2.detail.ImageFeatures 000002B5185D65E0>, < cv2.detail.ImageFeatures 000002B5185D6670>, < cv2.detail.ImageFeatures 000002B5185D6700>, < cv2.detail.ImageFeatures 000002B5185D6790>, < cv2.detail.ImageFeatures 000002B5185D6820>, < cv2.detail.ImageFeatures 000002B5185D68B0>, < cv2.detail.ImageFeatures 000002B5185D6940>, < cv2.detail.ImageFeatures 000002B5185D69D0>, < cv2.detail.ImageFeatures 000002B5185D6A60>, < cv2.detail.ImageFeatures 000002B5185D6AF0>, < cv2.detail.ImageFeatures 000002B5185D6B80>, < cv2.detail.ImageFeatures 000002B5185D6C10>, < cv2.detail.ImageFeatures 000002B5185D6CA0>, < cv2.detail.ImageFeatures 000002B5185D6D30>, < cv2.detail.ImageFeatures 000002B5185D6DC0>, < cv2.detail.ImageFeatures 000002B5185D6E50>, < cv2.detail.ImageFeatures 000002B5185D6EE0>, < cv2.detail.ImageFeatures 000002B5185D6F70>, < cv2.detail.ImageFeatures 000002B5185D7000>, < cv2.detail.ImageFeatures 000002B5185D7090>, < cv2.detail.ImageFeatures 000002B5185D7120>, < cv2.detail.ImageFeatures 000002B5185D71B0>, < cv2.detail.ImageFeatures 000002B5185D7240>, < cv2.detail.ImageFeatures 000002B5185D72D0>, < cv2.detail.ImageFeatures 000002B5185D7360>, < cv2.detail.ImageFeatures 000002B5185D73F0>, < cv2.detail.ImageFeatures 000002B5185D7480>, < cv2.detail.ImageFeatures 000002B5185D7510>, < cv2.detail.ImageFeatures 000002B5185D75A0>, < cv2.detail.ImageFeatures 000002B5185D7630>, < cv2.detail.ImageFeatures 000002B5185D76C0>, < cv2.detail.ImageFeatures 000002B5185D7750>, < cv2.detail.ImageFeatures 000002B5185D77E0>, < cv2.detail.ImageFeatures 000002B5185D7870>, < cv2.detail.ImageFeatures 000002B5185D7900>, < cv2.detail.ImageFeatures 000002B5185D7990>, < cv2.detail.ImageFeatures 000002B5185D7A20>, < cv2.detail.ImageFeatures 000002B5185D7AB0>, < cv2.detail.ImageFeatures 000002B5185D7B40>, < cv2.detail.ImageFeatures 000002B5185D7BD0>, < cv2.detail.ImageFeatures 000002B5185D7C60>, < cv2.detail.ImageFeatures 000002B5185D7CF0>, < cv2.detail.ImageFeatures 000002B5185D7D80>, < cv2.detail.ImageFeatures 000002B5185D7E10>, < cv2.detail.ImageFeatures 000002B5185D7EA0>, < cv2.detail.ImageFeatures 000002B5185D7F30>, < cv2.detail.ImageFeatures 000002B5185A8030>, < cv2.detail.ImageFeatures 000002B5185A80C0>, < cv2.detail.ImageFeatures 000002B5185A8150>, < cv2.detail.ImageFeatures 000002B5185A81E0>, < cv2.detail.ImageFeatures 000002B5185A8270>, < cv2.detail.ImageFeatures 000002B5185A8300>, < cv2.detail.ImageFeatures 000002B5185A8390>, < cv2.detail.ImageFeatures 000002B5185A8420>, < cv2.detail.ImageFeatures 000002B5185A84B0>, < cv2.detail.ImageFeatures 000002B5185A8540>, < cv2.detail.ImageFeatures 000002B5185A85D0>, < cv2.detail.ImageFeatures 000002B5185A8660>, < cv2.detail.ImageFeatures 000002B5185A86F0>, < cv2.detail.ImageFeatures 000002B5185A8780>, < cv2.detail.ImageFeatures 000002B5185A8810>, < cv2.detail.ImageFeatures 000002B5185A88A0>, < cv2.detail.ImageFeatures 000002B5185A8930>, < cv2.detail.ImageFeatures 000002B5185A89C0>, < cv2.detail.ImageFeatures 000002B5185A8A50>, < cv2.detail.ImageFeatures 000002B5185A8AE0>, < cv2.detail.ImageFeatures 000002B5185A8B70>, < cv2.detail.ImageFeatures 000002B5185A8C00>, < cv2.detail.ImageFeatures 000002B5185A8C90>, < cv2.detail.ImageFeatures 000002B5185A8D20>, < cv2.detail.ImageFeatures 000002B5185A8DB0>, < cv2.detail.ImageFeatures 000002B5185A8E40>, < cv2.detail.ImageFeatures 000002B5185A8ED0>, < cv2.detail.ImageFeatures 000002B5185A8F60>, < cv2.detail.ImageFeatures 000002B5185A8FF0>, < cv2.detail.ImageFeatures 000002B5185A9080>, < cv2.detail.ImageFeatures 000002B5185A9110>, < cv2.detail.ImageFeatures 000002B5185A91A0>, < cv2.detail.ImageFeatures 000002B5185A9230>, < cv2.detail.ImageFeatures 000002B5185A92C0>, < cv2.detail.ImageFeatures 000002B5185A9350>, < cv2.detail.ImageFeatures 000002B5185A93E0>, < cv2.detail.ImageFeatures 000002B5185A9470>, < cv2.detail.ImageFeatures 000002B5185A9500>, < cv2.detail.ImageFeatures 000002B5185A9590>, < cv2.detail.ImageFeatures 000002B5185A9620>, < cv2.detail.ImageFeatures 000002B5185A96B0>, < cv2.detail.ImageFeatures 000002B5185A9740>, < cv2.detail.ImageFeatures 000002B5185A97D0>, < cv2.detail.ImageFeatures 000002B5185A9860>, < cv2.detail.ImageFeatures 000002B5185A98F0>, < cv2.detail.ImageFeatures 000002B5185A9980>, < cv2.detail.ImageFeatures 000002B5185A9A10>, < cv2.detail.ImageFeatures 000002B5185A9AA0>, < cv2.detail.ImageFeatures 000002B5185A9B30>, < cv2.detail.ImageFeatures 000002B5185A9BC0>, < cv2.detail.ImageFeatures 000002B5185A9C50>, < cv2.detail.ImageFeatures 000002B5185A9CE0>, < cv2.detail.ImageFeatures 000002B5185A9D70>]
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
File ~\My Python Stuff\Stitching Project - ICCAS\Using Stitching Algorithm 1\Image Stitching in OpenCV\stitching_detailed_old.py:522
    518     print("Done")
    521 if __name__ == '__main__':
--> 522     main()
    523     cv.destroyAllWindows()

File ~\My Python Stuff\Stitching Project - ICCAS\Using Stitching Algorithm 1\Image Stitching in OpenCV\stitching_detailed_old.py:342, in main()
    340 matcher = get_matcher(args)
    341 print(features)
--> 342 p = matcher.apply2(features)
    343 matcher.collectGarbage()
    345 if save_graph:

error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\flann\src\miniflann.cpp:521: error: (-215:Assertion failed) (size_t)knn <= index_->size() in function 'cv::flann::runKnnSearch_'

And this is the result when printing matcher before line 341:

< cv2.detail.BestOf2NearestMatcher 000002B5189DABF0>

Everything is according to this website:

https://docs.opencv.org/4.x/d8/d19/tutorial_stitcher.html

Here is the python code:
https://raw.githubusercontent.com/opencv/opencv/4.x/samples/python/stitching_detailed.py

I hope you can help me solving this knn error while doing feature matching.

It is important to mention that while using the Autostitch programm with the same images it works fine, meaning it should work as well with the Opencv code I think.

Wish you all Happy New Year.

How can I share my images with you in order to try it yourself, maybe it would be better for you to solve the problem then, because I cannot solve it on my own unfortunately ?
Here it says that I can only attach 5 image at once but not more ? the problem is that I am trying to stitch 281 images after cutting them from a video.

Would you recommend me to still try the Opencv-Code for my image stitching project and wait for the solution of the problem with your code or that I should rather try other codes and try to optimize them regarding my images and project?






































These are some frames of the 281 frames which i got after cutting the video into frames, the 281 frames get stitched very well with the autostitch programm (AutoStitch). See the panaroma which i got with this program as an example.
But unfortunately it does not work while using the amazing opencv library as a python code. I hope you can fix this problem and figure something out. Thanks a lot in advance.

This might solve the problem in the miniflann.cpp code, where the Error is located:

Here is the Error again :
error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\flann\src\miniflann.cpp:521: error: (-215:Assertion failed) (size_t)knn <= index_->size() in function ‘cv::flann::runKnnSearch_’.

This might solve it:

#include
#include <opencv2/core.hpp>
#include <opencv2/flann.hpp>

int main() {
// Example data
cv::Mat data(100, 2, CV_32F);
cv::randn(data, cv::Scalar(0), cv::Scalar(1));

// Building the index
cv::flann::IndexParams indexParams;
indexParams.setAlgorithm(cv::flann::FLANN_INDEX_KMEANS);
indexParams.setInt("branching", 4);
indexParams.setInt("iterations", 20);

cv::flann::Index index(data, indexParams, cv::flann::FLANN_DIST_EUCLIDEAN);

// Query point
cv::Mat query(1, 2, CV_32F, cv::Scalar(0.5));

// Check if knn value is greater than index size
int knn = 5;
if (knn > index.size()) {
    std::cerr << "Error: knn value exceeds index size." << std::endl;
    return -1;
}

// Perform k-NN search
cv::Mat indices, dists;
index.knnSearch(query, indices, dists, knn, cv::flann::SearchParams());

// Print the results
std::cout << "Indices: " << indices << std::endl;
std::cout << "Distances: " << dists << std::endl;

return 0;

}

May you update it please so that I can try it with the stitching_detailed.py code