opencv4.0.0图像处理:边缘检测(一)

opencv是一个C++语言库,对于java程序员来说,貌似不是很友好;不过java程序员可以利用jni调用的方式来处理

for example:

1
2
3
4
5
6
7
8
9
10
public static void main1(String[] args) throws Exception {
NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat hello = Mat.eye(150, 150, CvType.CV_8SC3);
System.out.println(hello.dump());
hello.setTo(new Scalar(180,80,250));
Mat sub=hello.submat(0,50,0,50);
sub.setTo(new Scalar(0,0,100));
//imwrite("E:\\idea-workspace\\hellocv\\dev\\hello.png",hello);
//System.out.println(hello.dump());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 边缘检测算子
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat tools=Imgcodecs.imread("images/tools.jpg");
Imgproc.cvtColor(tools, tools, Imgproc.COLOR_RGB2GRAY);
Imgproc.Canny(tools, tools,100.0, 300.0,3,true);

Mat invetedTools=tools.clone();
bitwise_not(invetedTools, invetedTools);
Imgcodecs.imwrite("output/tools-04.png", invetedTools);
}


图像掩模

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
static Scalar WHITE = new Scalar(255,255,255);
/**
* 图像掩模
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Mat kittens = imread("images/tools.jpg");

Imgproc.cvtColor(kittens, kittens, Imgproc.COLOR_RGB2GRAY);
Imgproc.Canny(kittens, kittens, 100.0, 300.0, 3, true);
bitwise_not(kittens, kittens);
// System.out.println(kittens.dump());

Mat target = new Mat(kittens.height(), kittens.width(), CvType.CV_8UC3, WHITE);
Mat bg = imread("images/light-blue-gradient.jpg");
Imgproc.resize(bg, bg, target.size());
bg.copyTo(target, kittens);

imwrite("output/kittens-03.png", target);
}

坚持原创技术分享,您的支持是我前进的动力!