opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.32k stars 5.74k forks source link

在centos7上,用cmake编译opencv和opencv-contrib #3226

Open xu000yp opened 2 years ago

xu000yp commented 2 years ago

opencv、opencv-contrib都是4.5.3版本。

在centos7上,用cmake编译opencv和opencv-contrib,我想把opencv-contrib中的freetype模块功能编译出来,但是编译出来的jar中,总是没有freetype,求指点,谢谢!

操作步骤:

  1. sudo yum group install "Development Tools"
  2. gcc --version
  3. yum install -y wget
  4. wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz
  5. tar -zxvf cmake-3.6.2.tar.gz
  6. cd cmake-3.6.2
  7. ./bootstrap && make -j4 && sudo make install
  8. cmake --version
  9. sudo yum -y install epel-release sudo yum -y install gtk2-devel sudo yum install -y libpng-devel sudo yum install -y jasper-devel sudo yum install -y openexr-devel sudo yum install -y libwebp-devel sudo yum -y install libjpeg-turbo-devel sudo yum -y install libtiff-devel sudo yum -y install tbb-devel eigen3-devel sudo yum -y install boost boost-thread boost-devel sudo yum -y install libv4l-devel yum install python-devel numpy yum install -y unzip zip yum install ant yum install epel-release rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm yum install ffmpeg ffmpeg-devel -y

  10. 然后把提前准备好的 opencv、opencv-contrib(都是4.5.3版本)上传到centos7上,解压opencv,cd opencv,mkdir build
  11. 然后将opencv_contrib移动到opencv目录下,解压
  12. 将opencv/modules/features2d复制,然后粘贴到build目录
  13. 在build目录下,执行 cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
  14. make
  15. make install

然后再 /root/opencv/build/bin里面找到 jar包下载下来,看里面没有 freeType,请指点,最好有详细操作步骤及编译命令,谢谢~

Kumataro commented 2 years ago

For centos, following packages are needed.

$ sudo yum -y install freetype-devel
$ sudo yum -y install harfbuzz-devel

After that, pkg-config can be detected these.

$ pkg-config harfbuzz --modversion
1.7.5
$ pkg-config freetype2 --modversion
20.0.14
xu000yp commented 2 years ago

您好,我安装了一下freetype-devel和harfbuzz-devel,发现已经存在最新的了版本和你发的是一样的。 是我在cmake编译的时候命令不对吗? 还是有别的地方需要我去修改? 麻烦指点一下,谢谢

Kumataro commented 2 years ago

I see CMakeFile of imgcodecs. Like it , freetype2 wrapper is able to to build for java too I think.

Step 1. installing harfbuzz and freetype2 libraries to enable freetype wrapper.

If It's OK, in Step 3, CMake log shows it.

-- freetype2:   YES (ver 20.0.14)
-- harfbuzz:    YES (ver 1.7.5)

And

--   OpenCV modules:
--     To be built:                 aruco barcode ... freetype ...

Step 2. Change CMakeLists to add java wrapper .

https://github.com/opencv/opencv_contrib/blob/4.x/modules/freetype/CMakeLists.txt#L24

- ocv_define_module(freetype opencv_core opencv_imgproc WRAP python)
+ ocv_define_module(freetype opencv_core opencv_imgproc WRAP python java)

Step 3. (re-)run cmake to make Makefile. and run make.

If it works not good, it is better to re-create build directory.

Step 4. check jar file in bin directory.

[kmtr@localhost build]$ jar tf bin/opencv-453.jar | grep freetype
org/opencv/freetype/
org/opencv/freetype/FreeType2.java
org/opencv/freetype/Freetype.java
org/opencv/freetype/FreeType2.class
org/opencv/freetype/Freetype.class

Comment...

I'm a little unsure about java environment. It is difficult to support to run application in JAVA. I'm sorry. (So that, I can not enable java wrapper for freetype2 wrapper as default.)

I think it similar to imgcodecs, because it is used external libraries(libjpeg, libpng, libwebp, ...). If there are any trouble, please reference around it.

xu000yp commented 2 years ago

谢谢

xu000yp commented 2 years ago

您好,不好意思 `还要麻烦您,opencv在java中启动的时候,会通过 static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } ` 来加载opencv,但是我没有找到freeType要如何加载,所以我通过freetype里面的创建方法来创建对象。 但是在运行的时候报错,编译到opencv中Freetype类如下,

`// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) //

package org.opencv.freetype;

public class Freetype { public Freetype() { }

public static FreeType2 createFreeType2() {
    return FreeType2.__fromPtr__(createFreeType2_0());
}

private static native long createFreeType2_0();

} ,我在外面调用opencv.jar中的Freetype创建对象, FreeType2 freeType2 = Freetype.createFreeType2(); ,会报错如下, java.lang.UnsatisfiedLinkError: org.opencv.freetype.Freetype.createFreeType2_0()J at org.opencv.freetype.Freetype.createFreeType2_0(Native Method) at org.opencv.freetype.Freetype.createFreeType2(Freetype.java:23) ,我在网上查了一下说是没有找到freeType.so文件,然后 我把Freetype类修改了一下并重新编译成class文件放到opencv.jar中,如下: package org.opencv.freetype;

public class Freetype { public Freetype() { }

static {
    System.load("/usr/lib/libfreetype.so");
}

public static FreeType2 createFreeType2() {
    return FreeType2.__fromPtr__(createFreeType2_0());
}

private static native long createFreeType2_0();

}`, 但是我感觉创建createFreeType2方式是不是需要变更一下,我不知道要如何才能创建出调用freeType2的对象来调用其内部的方法,求解惑,谢谢~

Kumataro commented 2 years ago

This is sample code.

I tried it with ubuntu 22.04-dev and OpenCV/Contriv 4.x. Could you try with it ?

SimpleSample.class : SimpleSample.java
        javac -classpath lib/opencv-455.jar SimpleSample.java

run : SimpleSample.class
        java -Djava.library.path='/usr/local/share/java/opencv4/' -classpath .:lib/opencv-455.jar SimpleSample
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;

import org.opencv.freetype.Freetype;
import org.opencv.freetype.FreeType2;

class SimpleSample {
  static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

  public static void main(String[] args) {
    String fontData = "/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf";
    String text = "Funny text inside the box";
    int fontHeight = 40;
    int thickness = 1;
    int linestyle = 16;

    Mat img = Mat.zeros(600, 800, CvType.CV_8UC3);

    int[] baseline = {0};

    FreeType2 ft2 = Freetype.createFreeType2();
    ft2.loadFontData (fontData, 0);

    Size textSize = ft2.getTextSize( text, fontHeight, thickness, baseline);

    if ( thickness < 0 ){
      baseline[0] += thickness;
    }

     // center the text
     Point textOrg = new Point ((img.cols() - textSize.width) / 2,
                                (img.rows() + textSize.height) / 2);

     // draw the box
     Imgproc.rectangle(img,
               new Point(textOrg.x + 0,              textOrg.y + baseline[0]),
               new Point(textOrg.x + textSize.width, textOrg.y - textSize.height),
               new Scalar(0,255,0),
               1,
               8);

    Imgproc.line(img,
               new Point(textOrg.x + 0,              textOrg.y + thickness),
               new Point(textOrg.x + textSize.width, textOrg.y + thickness),
               new Scalar(0,0,255),
               1,
               8);

    ft2.putText(img,
                text,
                textOrg,
                fontHeight,
                new Scalar(255,255,255),
                thickness,
                linestyle,
                true );

    Imgcodecs.imwrite("output.png", img);

    System.out.println(textSize);

  }
}
xu000yp commented 2 years ago

好的 我试一下 谢谢