opencv_contrib module compilation and installation

foreword

Since the installation of the opencv_contrib module depends on the opencv basic module, please read the previous OpenCV basic module installation tutorial before reading this tutorial

download

Download the opencv_contrib module (it needs to be the same as the opencv basic module version number 4.6.0, which can be downloaded in gitcode in China )

Configure, compile, install

Open cmake-gui, select the source code path of [ OpenCV basic module ] (not the contrib module), then select the new compilation output path build/contrib, then click configure, and wait for the end.
insert image description here

After completion, [in the configuration of the basic module] continue to set the following configurations:

  1. Set the contrib module path xxxx/opencv_contrib-4.6.0/modules(note that modules should be included)
    image-20230513163704147

  2. Check nonfree (allows the use of patented algorithms)
    image-20230513163853739

Click configure again, and then wait. At this time, a lot of things will be downloaded again, and if nothing unexpected happens, it will report red again, so it needs to be downloaded manually next. [ For the download method, see the end of the article - configure red solution ]

After downloading, renaming and placing it in the corresponding folder, perform the following steps in sequence:

  1. Click configure, then generate
  2. Close cmake-gui, open cmd in build/contribthe directory
  3. Enter the command in cmd mingw32-make -j7to start compiling (usually takes more than 30 minutes)

After the compilation is complete, you can use the command mingw32-make installto install, and then add environment variables (note that the .dllincluded bin directory is added to the environment variables)

configure report red solution

Method 1. Manual download

Enter build/contribthe directory , open the [CMakeDownloadLog.txt] file, find all the lines containing "cmake_download", and copy them to a file separately for later comparison and download.

like:

#cmake_download "D:/xxxx/opencv460/opencv/sources/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i"
# 第2个链接是文件下载位置,第1个链接则是下载文件并重命名后保存的位置

For each download link, change raw.githubusercontent.comall raw.staticdn.netbefore downloading.

After the download is complete, rename and save it to the specified location as described in the first link.

Method 2. Write a Python script to automatically download

[Attachment]: I wrote a python script to automatically download missing files. Those who have installed python and the requests library can use this script to download ( note that there is a large .datfile , please download it yourself)

import requests
import os


def getItemUrls(fpath):
    items = []
    with open(fpath, 'r') as f:
        lines = f.readlines()
        for line in lines:
            if('cmake_download' in line):
                line = line.strip().replace('"', '')
                linfo = line.split()
                fileSavePath = linfo[1]
                url = linfo[2].replace(
                    'raw.githubusercontent.com', 'raw.staticdn.net')
                item = dict(file=fileSavePath, url=url)
                items.append(item)
    return items


def save(savepath, content):
    with open(savepath, 'wb') as f:
        f.write(content)


print('请输入 CMakeDownloadLog.txt 的文件路径',
      r'比如D:\opencv\sources\build\release\CMakeDownloadLog.txt', sep='\n')
fp = input(': ')

items = getItemUrls(fp)

for i in range(len(items)):
    item = items[i]
    if(item["url"][-3:] == "dat"):
        print(f"i={
      
      i} --> 文件过大,请自行前往{
      
      item['url']}\n并保存为 {
      
      item['file']}")
        continue
    os.makedirs('/'.join(item['file'].split('/')[:-1]), exist_ok=True)
    try:
        r = requests.get(item['url'], timeout=20)
    except:
        print(f"i={
      
      i} --> 下载失败,请自行前往{
      
      item['url']}\n并保存为 {
      
      item['file']}")
        continue
    save(item['file'], r.content)
    print(f'i={
      
      i} --> 已下载')
    
print("【下载结束】")

Guess you like

Origin blog.csdn.net/m0_46079750/article/details/130660446