首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV在ubuntu下的编译

OpenCV在ubuntu下的编译

作者头像
Pulsar-V
发布2018-04-18 16:57:48
9940
发布2018-04-18 16:57:48
举报
文章被收录于专栏:Pulsar-VPulsar-VPulsar-V

opencv的编译

下面我们写一个shell命名为build.sh放在opencv的根目录下面,代码如下:

mkdir $1
cd $1
cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON -DBUILD_opencv_java=ON BUILD_opencv_test_java=OFF ..
make -j8

其他编译参数请参考博客

在Windows下的编译可以直接使用CMake GUI进行设置配置

如图

选择自己需要的类型的配置参数(如编译器类型,编译参数)

接下来我们运行一下

sh ./build.sh build

即可在bin目录下找到生成的opencv build

java版本的安装

1.Ant的安装(非Java可略过此处)

生成opencv的Java包一直是一个会困扰Java党萌新的问题

按照官方给出的安装教程并不会生成OpenCV的jar包

首先我们要安装ant

输入指令sudo apt-get install ant

在windows下ant 在安装ant需要去下载 apache 的ant包并设置好系统环境变量ANT_HOME为类似D:\apache-ant-1.10.1安装目录

注:要安装oracle jdk不然有时候会出玄学问题

在上面c++安装编译的时候参数-DBUILD_opencv_java=ON就已经帮我们生成了jar的包在bin目录下

注:opencv生成的包分为静态包和动态包,推荐生成静态包,静态调用会省去许多不必要或者是不知道的动态链接库的加载。 接下来我们把包导入到idea中。 下面给出一个基于Config的opencv启动器

project/src/org/uestc/config

package org.uestc.config;

import java.io.*;
import java.util.*;

public class mainConfig{
    BufferedInputStream user_in=null;
    BufferedInputStream sys_in=null;
    FileOutputStream user_file=null;
    FileOutputStream sys_file=null;
    private String sysPath="./config/sys.properties";
    private String userPath="./config/config.properties";
    public mainConfig(){
        try {
            try {
                user_in = new BufferedInputStream(new FileInputStream(this.userPath));
                sys_in = new BufferedInputStream(new FileInputStream(sysPath));
            } catch (IOException e) {
                try {
                    user_file = new FileOutputStream(userPath, true);
                    sys_file = new FileOutputStream(sysPath, true);
                    user_in = new BufferedInputStream(new FileInputStream(userPath));
                    sys_in = new BufferedInputStream(new FileInputStream(sysPath));
                    user_file.close();
                    sys_file.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                System.out.println("No Config file , System will auto Created");
            }
        }catch (Exception e){
            System.out.println("I/O Error");
        }
    }
    private void setDefaultConfig(Map<String,String> info) throws IOException {
        Properties pps = new Properties();
        Iterator<String> it=info.keySet().iterator();
        while (it.hasNext()){
            String key=it.next();
            pps.setProperty(key,info.get(key));
        }
        pps.store(user_file, "The New properties user_file");
    }
    public  Map<String,String> getUserConfig(){
        Properties pps = new Properties();
        Map<String,String> info=new HashMap<String,String>();
        try {
            pps.load(user_in);
            Iterator<String> it=pps.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                info.put(key,pps.getProperty(key));
            }
            return info;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return info;
    }
}

配置文件

project/config/config.properties

max_camera=100

入口文件

/**
 * Created by Summer-V on 17-4-12.
 */
import org.opencv.videoio.VideoCapture;
import org.opencv.core.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {
    public void video_start(Map<Integer,String> camera){
        Iterator<Integer> cap=camera.keySet().iterator();
        while (cap.hasNext()){
            int index=cap.next();
            new Thread(new Runnable() {
                @Override
                public void run() {
            videoViewer video=new videoViewer();
            video.openWindow(index,camera.get(index),100);
                }
            }).start();
        }
    public static void main(String[] args){
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);//加载opencv库
        mainConfig Config=new mainConfig();
        Map<String,String> info=Config.getUserConfig();
        Iterator<String> it=info.keySet().iterator();
        while (it.hasNext()){
            String key =it.next();
        }
        VideoCapture cae=new VideoCapture();
        for(int i=0;i<Integer.parseInt(info.get("max_camera"));i+=1){
            Map<Integer,String> cap=new HashMap<Integer,String>();
            if (cae.open(i)){
                cap.put(i,"Camera"+i);
                video_start(cap);
            }else {
                break;
            }
        }
    }
}

注:如果不存在引用关系的话,每一次都要加载opencv库

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • opencv的编译
  • java版本的安装
    • 1.Ant的安装(非Java可略过此处)
      • 注:要安装oracle jdk不然有时候会出玄学问题
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档