Apart from the Matlab link commented in the previous response, you can apply ABLE (Able for Binary-appearance Loop-closure Evaluation), which is an algorithm that we proposed in some papers that we recently published (I attach them to you). This method is focused on place recognition, but you can adapt it for your CBIR problem. We are planning to publish an open source implementation in the next weeks, which will be downloadable from: