代码绘制匹配连线
Mat dstImage;
drawMatches(featuredImg, keypoints, featuredImg1, keypoints1, qualifiedMatches, dstImage);
imshow("dstImage", dstImage);
出现报错
OpenCV(3.4.7) Error: Assertion failed (i2 >= 0 && i2 < static_cast(keypoints2.size())) in cv::drawMatches, file E:\MAYA\source\opencv-3.4.7\modules\features2d\src\draw.cpp, line 226
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(3.4.7) Error: Assertion failed (i2 >= 0 && i2 < static_cast(keypoints2.size())) in cv::drawMatches, file E:\MAYA\source\opencv-3.4.7\modules\features2d\src\draw.cpp, line 226
最开始没太理解这是什么意思,百度查找到Overstack中《OpenCV drawMatches error》有相同问题(https://stackoverflow.com/questions/28725273/opencv-drawmatches-error)
其中最佳答案为
In general, good_matches is an array, which binds points from both keypoints1 and keypoints2array. So point keypoints1[good_matches[m].queryIdx] corresponds to point keypoints2[good_matches[m].trainIdx]. As you can see, assertions in opencv code have sense.
It seems, the problem is in matches array.
说的倒是没错,keypoints1[good_matches[m].queryIdx]与keypoints2[good_matches[m].trainIdx]是相对应的,但我没能看出具体有什么。
于是我开始输出qualifiedMatches、keypoints相关信息
keypoints.size: 81956, keypoints1.size: 18171
qualifiedMatches.at(i).queryIdx:
58, 99, 164, 172, 174, 193, 194, 219, 234, 250, 257, 262, 276, 286, 303, 314, 366, 369, 392, 447, 458, 507, 514, 518, 524, 533, 536, 564, 583, 601, 667, 669, 715, 728, 731, 741, 787, 836, 837, 851, 882, 898, 946, 947, 966, 1001, 1100, 1130, 1135, 1233, 1269, 1270, 1307, 1324, 1360, 1361, 1376, 1383, 1390, 1438, 1439, 1451, 1452, 1457, 1475, 1490, 1495, 1496, 1520, 1524, 1528, 1589, 1601, 1619, 1640, 1651, 1670, 1681, 1692, 1705, 1716, 1721, 1722, 1731, 1748, 1749, 1750, 1761, 1776, 1781, 1789, 1792, 1800, 1802, 1813, 1837, 1846, 1877, 1913, 1934, 1943, 1960, 1982, 1986, 1991, 1992, 1993, 2006, 2011, 2063, 2074, 2089, 2103, 2118, 2119, 2120, 2128, 2142, 2143, 2153, 2158, 2178, 2185, 2186, 2194, 2197, 2199, 2204, 2205, 2210, 2214, 2225, 2244, 2245, 2251, 2252, 2253, 2255, 2264, 2268, 2269, 2270, 2290, 2298, 2307, 2312, 2315, 2322, 2324, 2333, 2342, 2347, 2360, 2366, 2374, 2375, 2383, 2396, 2397, 2404, 2412, 2418, 2422, 2432, 2433, 2440, 2449, 2455, 2457, 2463, 2464, 2468, 2469, 2471, 2480, 2481, 2483, 2484, 2492, 2494, 2503, 2504, 2511, 2519, 2529, 2538, 2539, 2544, 2548, 2550, 2552, 2566, 2568, 2576, 2578, 2591, 2592, 2597, 2602, 2607, 2608, 2609, 2617, 2629, 2636, 2638, 2639, 2647, 2649, 2657, 2666, 2667, 2678, 2683, 2684, 2697, 2700, 2704, 2709, 2721, 2722, 2730, 2744, 2748, 2754, 2756, 2758, 2764, 2766, 2770, 2771, 2776, 2804, 2807, 2808, 2820, 2822, 2828, 2829, 2842, 2846, 2855, 2862, 2864, 2865, 2866, 2868, 2869, 2870, 2873, 2874, 2882, 2895, 2900, 2903, 2912, 2913, 2914, 2917, 2918, 2921, 2922, 2923, 2932, 2941, 2942, 2945, 2946, 2954, 2955, 2958, 2969, 2971, 2972, 2973, 2974, 2975, 2976, 2977, 2978, 2979, 2987, 2996, 3001, 3003, 3008, 3010, 3025, 3028, 3029, 3033, 3040, 3050, 3063, 3066, 3071, 3072, 3074, 3078, 3084, 3086, 3088, 3090, 3091, 3092, 3094, 3095, 3096, 3098, 3100, 3112, 3120, 3121, 3123, 3124, 3126, 3132, 3139, 3145, 3146, 3147, 3148, 3149, 3157, 3175, 3179, 3186, 3187, 3188, 3192, 3193, 3209, 3210, 3221, 3228, 3229, 3232, 3233, 3234, 3236, 3244, 3257, 3272, 3278, 3285, 3286, 3290, 3300, 3306, 3313, 3326, 3328, 3332, 3335, 3336, 3342, 3345, 3358, 3360, 3362, 3363, 3371, 3375, 3382, 3388, 3389, 3391, 3403, 3407, 3408, 3414, 3458, 3469, 3476, 3484, 3485, 3486, 3489, 3504, 3508, 3510, 3512, 3515, 3517, 3518, 3520, 3531, 3533, 3534, 3536, 3542, 3543, 3546, 3547, 3548, 3549, 3550, 3554, 3558, 3563, 3564, 3573, 3576, 3579, 3584, 3590, 3591, 3592, 3594, 3597, 3598, 3600, 3610, 3615, 3618, 3629, 3630, 3631, 3633, 3637, 3638, 3642, 3643, 3644, 3647, 3648, 3651, 3653, 3656, 3657, 3664, 3666, 3667, 3668, 3674, 3678, 3687, 3688, 3702, 3704, 3708, 3715, 3717, 3725, 3728, 3729, 3730, 3731, 3732, 3736, 3739, 3750, 3756, 3761, 3762, 3765, 3770, 3773, 3774, 3789, 3790, 3791, 3794, 3795, 3798, 3800, 3802, 3806, 3807, 3809, 3810, 3815, 3821, 3823, 3824, 3833, 3834, 3835, 3837, 3841, 3846, 3847, 3851, 3853, 3869, 3870, 3871, 3880, 3881, 3884, 3885, 3889, 3895, 3897, 3898, 3902, 3908, 3909, 3910, 3912, 3914, 3920, 3921, 3924, 3926, 3927, 3931, 3933, 3934, 3937, 3944, 3945, 3946, 3947, 3952, 3959, 3965, 3972, 3974, 3975, 3980, 3981, 3983, 3990, 3992, 3995, 3997, 4000, 4001, 4003, 4004, 4008, 4011, 4017, 4020, 4022, 4028, 4030, 4033, 4043, 4047, 4048, 4050, 4057, 4073, 4074, 4078, 4080, 4089, 4091, 4094, 4095, 4096, 4097, 4099, 4100, 4101, 4110, 4116, 4117, 4118, 4119, 4124, 4125, 4127, 4133, 4135, 4141, 4146, 4151, 4154, 4155, 4159, 4169, 4170, 4180, 4182, 4183, 4190, 4195, 4198, 4204, 4205, 4206, 4208, 4219, 4223, 4225, 4236, 4266, 4275, 4299, 4300, 4308, 4309, 4312, 4313, 4318, 4327, 4332, 4345, 4349, 4352, 4369, 4370, 4372, 4407, 4408, 4412, 4418, 4429, 4441, 4453, 4454, 4463, 4470, 4472, 4487, 4489, 4492, 4496, 4502, 4520, 4524, 4538, 4541, 4547, 4548, 4572, 4573, 4575, 4577, 4594, 4603, 4604, 4622, 4629, 4660, 4668, 4678, 4679, 4680, 4683, 4687, 4696, 4711, 4722, 4735, 4736, 4745, 4759, 4763, 4764, 4774, 4785, 4787, 4795, 4801, 4810, 4811, 4813, 4815, 4816, 4826, 4845, 4859, 4866, 4880, 4881, 4882, 4885, 4889, 4890, 4894, 4910, 4916, 4920, 4921, 4934, 4937, 4939, 4940, 4941, 4945, 4946, 4952, 4962, 4967, 4968, 4969, 4973, 4974, 4984, 4987, 4991, 5009, 5015, 5026, 5031, 5035, 5053, 5061, 5069, 5071, 5073, 5081, 5082, 5087, 5088, 5095, 5099, 5107, 5109, 5110, 5133, 5153, 5170, 5180, 5185, 5187, 5191, 5209, 5213, 5216, 5225, 5233, 5236, 5250, 5256, 5273, 5277, 5278, 5286, 5292, 5295, 5299, 5308, 5310, 5312, 5316, 5327, 5328, 5343, 5351, 5366, 5369, 5370, 5376, 5378, 5388, 5390, 5392, 5396, 5408, 5411, 5427, 5429, 5441, 5442, 5443, 5454, 5463, 5467, 5500, 5507, 5509, 5514, 5523, 5527, 5536, 5539, 5559, 5569, 5580, 5591, 5592, 5593, 5595, 5596, 5599, 5602, 5612, 5621, 5625, 5628, 5632, 5633, 5654, 5674, 5676, 5687, 5692, 5698, 5704, 5707, 5708, 5709, 5711, 5712, 5728, 5743, 5758, 5759, 5763, 5764, 5780, 5781, 5785, 5786, 5791, 5801, 5804, 5813, 5822, 5826, 5827, 5829, 5830, 5838, 5839, 5840, 5857, 5876, 5880, 5885, 5887, 5913, 5917, 5918, 5921, 5924, 5930, 5937, 5939, 5944, 5948, 5958, 5960, 5967, 5977, 5985, 5986, 5989, 5999, 6002, 6003, 6006, 6024, 6030, 6032, 6033, 6036, 6037, 6041, 6044, 6053, 6060, 6061, 6067, 6068, 6074, 6076, 6077, 6088, 6094, 6105, 6108, 6113, 6114, 6121, 6122, 6123, 6136, 6143, 6144, 6146, 6147, 6148, 6156, 6159, 6161, 6171, 6172, 6173, 6175, 6179, 6181, 6183, 6187, 6191, 6194, 6202, 6211, 6217, 6218, 6221, 6229, 6231, 6238, 6240, 6248, 6252, 6254, 6263, 6267, 6275, 6328, 6329, 6335, 6356, 6357, 6358, 6402, 6423, 6435, 6436, 6441, 6442, 6443, 6468, 6483, 6484, 6496, 6514, 6517, 6523, 6538, 6542, 6545, 6547, 6562, 6585, 6593, 6597, 6610, 6643, 6647, 6650, 6660, 6661, 6669, 6670, 6671, 6697, 6713, 6717, 6722, 6723, 6728, 6739, 6741, 6745, 6746, 6752, 6771, 6800, 6821, 6823, 6844, 6847, 6855, 6873, 6888, 6895, 6901, 6904, 6907, 6920, 6922, 6978, 6979, 6998, 7006, 7022, 7023, 7036, 7055, 7059, 7060, 7107, 7125, 7135, 7178, 7179, 7188, 7192, 7200, 7220, 7234, 7237, 7238, 7266, 7275, 7276, 7277, 7315, 7353, 7360, 7361, 7371, 7374, 7387, 7398, 7400, 7404, 7412, 7418, 7419, 7469, 7472, 7500, 7514, 7543, 7546, 7550, 7556, 7571, 7574, 7577, 7591, 7602, 7610, 7634, 7639, 7641, 7651, 7656, 7666, 7674, 7703, 7728, 7734, 7741, 7753, 7754, 7765, 7772, 7796, 7824, 7828, 7833, 7844, 7847, 7854, 7869, 7871, 7877, 7890, 7902, 7910, 7935, 7964, 7986, 8007, 8053, 8061, 8064, 8078, 8081, 8100, 8101, 8129, 8136, 8169, 8176, 8181, 8189, 8205, 8208, 8215, 8256, 8268, 8275, 8305, 8320, 8324, 8339, 8345, 8346, 8400, 8413, 8420, 8433, 8439, 8450, 8467, 8473, 8487, 8511, 8518, 8527, 8545, 8550, 8572, 8581, 8602, 8603, 8607, 8657, 8659, 8698, 8709, 8718, 8777, 8778, 8782, 8791, 8816, 8866, 8867, 8885, 8916, 8961, 9014, 9033, 9034, 9035, 9074, 9084, 9122, 9181, 9243, 9248, 9268, 9337, 9378, 9385, 9425, 9427, 9462, 9524, 9525, 9582, 9647, 9667, 9668, 9676, 9709, 9711, 9718, 9719, 9731, 9751, 9752, 9756, 9780, 9781, 9794, 9814, 9819, 9872, 9876, 9904, 9913, 9928, 9940, 9950, 9967, 9972, 10001, 10019, 10046, 10052, 10065, 10078, 10079, 10088, 10128, 10181, 10188, 10214, 10235, 10237, 10241, 10245, 10278, 10303, 10307, 10342, 10373, 10403, 10404, 10421, 10433, 10435, 10446, 10449, 10457, 10475, 10515, 10523, 10538, 10582, 10657, 10669, 10703, 10705, 10718, 10728, 10737, 10738, 10741, 10757, 10771, 10854, 10868, 10876, 10914, 10932, 10941, 10963, 10996, 11001, 11185, 11186, 11196, 11213, 11218, 11241, 11275, 11291,
qualifiedMatches.at(i).trainIdx:
34246, 33968, 34053, 34089, 34096, 34878, 34174, 34959, 34425, 35193, 34583, 34598, 34870, 34703, 35386, 35437, 35131, 35737, 35827, 35616, 35713, 36160, 36179, 36126, 37206, 36134, 36108, 36233, 36356, 36405, 36917, 36899, 37011, 37232, 37058, 38127, 37510, 37518, 37596, 37645, 37805, 37863, 38481, 38104, 38149, 38768, 38820, 38975, 39003, 39453, 39890, 39891, 39981, 39881, 40021, 40022, 40084, 40166, 40095, 40665, 40666, 40390, 40391, 40413, 40508, 40939, 40667, 40668, 41079, 40828, 40829, 41386, 41203, 41397, 41342, 41374, 41743, 41779, 41599, 41676, 41723, 41752, 41744, 41806, 42124, 42125, 41898, 45802, 42207, 42226, 42063, 42071, 42351, 42379, 42175, 42284, 42359, 42497, 42669, 42773, 42805, 42891, 42991, 42993, 43005, 43139, 43027, 43061, 43123, 43359, 43438, 43670, 45673, 43683, 43775, 43776, 43639, 43922, 45984, 43839, 43902, 44059, 43907, 43939, 43976, 44146, 44005, 44020, 44014, 44061, 44139, 44207, 44380, 44381, 44208, 44209, 44329, 44249, 44258, 44316, 44414, 44415, 44563, 44578, 44657, 44589, 44542, 44679, 44718, 44777, 44807, 44693, 44739, 44816, 44942, 44943, 44903, 44888, 45087, 44988, 44953, 44987, 44985, 45128, 45129, 45042, 45073, 45652, 45284, 45243, 45194, 45263, 45264, 45244, 45340, 45480, 45317, 45349, 45336, 45356, 45510, 45396, 45424, 45590, 45589, 45675, 45645, 45710, 45563, 45630, 45577, 45640, 45656, 45895, 45676, 45749, 45763, 45796, 46011, 45832, 45812, 45813, 45906, 45957, 46005, 46015, 46019, 46073, 46129, 46134, 46167, 46251, 46197, 46260, 46261, 46309, 46320, 46335, 46362, 46323, 46417, 46464, 46457, 46490, 46527, 46544, 46573, 46767, 46592, 46636, 46629, 46637, 46731, 46714, 46726, 47014, 46745, 46857, 46727, 46836, 46848, 46860, 46969, 46994, 46912, 46876, 47274, 46939, 46940, 46949, 46938, 46978, 47053, 47077, 47190, 47141, 47147, 47139, 47155, 47152, 47166, 47179, 47180, 47201, 47219, 47212, 47237, 47238, 47267, 47282, 47341, 46636, 47404, 47438, 47396, 47397, 47455, 47408, 47411, 47418, 47421, 47583, 47536, 47516, 47514, 47561, 47530, 47598, 47626, 47611, 47636, 47645, 47743, 47870, 47804, 47838, 47867, 47824, 47880, 47900, 47928, 47933, 47910, 47911, 47959, 47964, 47976, 47957, 47977, 48062, 48005, 48069, 48063, 48060, 48056, 48065, 48135, 48128, 48144, 48145, 48183, 48184, 48157, 48165, 48213, 48233, 48234, 48232, 48221, 48294, 48331, 48308, 48298, 48358, 48370, 48362, 48390, 48371, 48372, 48389, 48435, 48458, 48475, 48486, 48505, 48506, 48519, 48597, 48604, 48663, 48737, 48728, 48770, 48774, 48775, 48719, 48862, 48890, 48880, 48895, 48896, 48980, 48967, 49240, 48969, 48970, 48980, 49034, 49060, 49065, 49096, 49293, 49271, 49339, 49358, 49359, 49360, 49383, 49449, 49485, 49461, 49506, 49452, 49489, 49540, 49536, 49593, 49604, 49618, 49683, 49664, 49665, 49675, 49676, 49693, 49694, 49669, 49636, 49718, 49776, 49754, 49850, 49869, 49881, 49892, 49926, 49903, 49904, 49962, 49961, 49982, 50004, 50017, 49905, 50072, 50130, 50117, 50104, 50095, 50133, 50134, 50133, 50134, 50192, 50254, 50217, 50248, 50262, 50255, 50238, 50283, 50302, 50670, 50344, 50355, 50353, 50422, 50449, 50481, 50492, 50494, 50559, 50552, 50608, 50641, 50610, 50637, 50638, 50661, 50652, 50698, 50630, 50744, 50774, 50775, 50769, 50758, 50824, 50825, 50906, 50865, 50912, 50964, 50957, 51006, 50962, 51381, 51036, 51053, 51055, 51066, 51075, 51114, 51092, 51093, 51154, 51155, 51061, 51063, 51074, 51159, 51172, 51249, 51165, 51321, 51349, 51327, 51382, 51392, 51312, 51313, 51328, 51413, 51450, 51449, 51475, 51479, 51406, 51448, 51510, 51435, 51517, 51518, 51577, 51570, 51597, 51572, 51610, 51615, 51601, 51675, 51614, 51626, 51627, 51682, 51801, 51827, 51874, 51822, 51802, 51830, 51874, 51836, 51900, 51864, 51922, 51826, 51828, 51829, 51951, 51953, 51965, 51974, 51983, 51923, 51956, 52045, 52009, 52112, 52131, 52201, 52212, 52216, 52145, 52282, 52230, 52242, 52314, 52271, 52349, 52364, 52363, 52365, 52274, 52382, 52361, 52362, 52549, 52584, 52418, 52467, 52442, 52482, 52520, 52522, 52523, 52533, 52590, 52624, 52586, 52662, 52648, 52667, 52711, 52712, 52727, 52761, 52762, 52814, 52800, 52859, 52894, 52895, 52896, 52910, 53135, 52995, 52950, 53041, 53347, 53299, 53441, 53452, 53505, 53504, 53523, 53524, 53598, 53550, 53644, 53693, 53931, 53727, 53816, 53817, 53879, 53955, 53956, 53953, 54022, 54052, 54105, 54149, 54413, 54168, 54250, 54259, 54245, 54244, 54212, 54324, 54302, 54379, 54357, 54741, 54751, 54782, 54783, 54532, 54542, 54926, 54554, 54605, 55081, 54696, 55169, 54850, 54991, 55443, 55518, 55519, 55509, 55536, 55562, 55655, 55712, 55817, 55927, 55928, 55918, 56083, 56024, 56025, 56188, 56219, 55850, 56221, 56277, 56297, 56368, 56378, 56348, 56364, 56460, 56542, 56622, 56230, 56772, 56731, 56724, 56829, 56851, 56849, 56834, 56881, 56996, 57004, 57035, 57086, 57063, 57154, 57167, 57168, 57183, 57129, 57230, 57343, 57379, 57350, 57351, 5
可以看到,输出戛然而止,结合代码
for (int i = 0; i < qualifiedMatches.size(); i++) {
out << "x: " << keypoints.at(qualifiedMatches.at(i).queryIdx).pt.x
<< ", y: " << keypoints.at(qualifiedMatches.at(i).queryIdx).pt.y << "; ";
out << "x: " << keypoints1.at(qualifiedMatches.at(i).trainIdx).pt.x
<< ", y: " << keypoints1.at(qualifiedMatches.at(i).trainIdx).pt.y << "; ";
if ((i + 1) % 2 == 0) {
out << "\n";
}
}
因为之前只输出keypoints信息的时候没有出错(输出戛然而止),
结合之前的报错Error: Assertion failed (i2 >= 0 && i2 < static_cast
再看qualifiedMatches.at(i).trainIdx的数据,第一个就是三万多(大概率是有问题的数据),都已经超过keypoints1.size: 18171了,
所以我认为是qualifiedMatches中关于keypoints1的数据有问题,而qualifiedMatches的数据来自std::vector
检查之后才发现,其实是我把关键点集与qualifiedMatches的ID类型对应关系搞错了,
应当是keypoints对应trainIdx,keypoints1对应queryIdx,
即第一张图对应trainIdx,第二张图对应queryIdx,
for (int i = 0; i < qualifiedMatches.size(); i++) {
out << "x: " << keypoints.at(qualifiedMatches.at(i).trainIdx).pt.x
<< ", y: " << keypoints.at(qualifiedMatches.at(i).trainIdx).pt.y << "; ";
out << "x: " << keypoints1.at(qualifiedMatches.at(i).queryIdx).pt.x
<< ", y: " << keypoints1.at(qualifiedMatches.at(i).queryIdx).pt.y << "; ";
if ((i + 1) % 2 == 0) {
out << "\n";
}
}
所以绘制匹配连线的对应关系也要更改,分别对换featuredImg和featuredImg1, keypoints和keypoints1的位置;
drawMatches(featuredImg1, keypoints1, featuredImg, keypoints, qualifiedMatches, dstImage);
成功显示匹配连线图像dstImage。
现在再回过头来看那段回答
In general, good_matches is an array, which binds points from both keypoints1 and keypoints2array. So point keypoints1[good_matches[m].queryIdx] corresponds to point keypoints2[good_matches[m].trainIdx]. As you can see, assertions in opencv code have sense.
It seems, the problem is in matches array.
keypoints1[good_matches[m].queryIdx]与keypoints2[good_matches[m].trainIdx]的对应关系已经暗示我们drawMatches时候,featuredImg和featuredImg1, keypoints和keypoints1各自的对应关系以及所在位置。(不过It seems, the problem is in matches array.这句话具有迷惑性,使得我误认为是matches 的错误,而实际上只是对应关系搞错了)