Skip to Content
企业级Java开发中的应用人工智能 (Chinese Edition)
book

企业级Java开发中的应用人工智能 (Chinese Edition)

by Alex Soto Bueno, Markus Eisele, Natale Vinto
November 2025
Beginner to intermediate
430 pages
5h 12m
Chinese
O'Reilly Media, Inc.
Content preview from 企业级Java开发中的应用人工智能 (Chinese Edition)

第11章 图像 处理

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

前几章介绍了将Java与AI/ML项目集成的基础知识。 您还学习了如何使用DJL在Java中加载和推断模型,并通过LangChain4j和LangGraph4j使用这些模型。 本书剩余部分将基于这些知识,实现更接近实际项目场景的高级应用案例。

人工智能在项目中的常见应用之一是图像处理,用于分类或信息提取。输入图像可以是用户提供的单张照片,也可以是来自相机等设备的图像流。

以下是若干图像处理用例示例:

  • 检测物体或人物,例如在安防监控系统中

  • 按内容分类图像,例如商品分类

  • 从证件(如身份证、护照)中提取信息

  • 读取车辆牌照(例如通过测速摄像头)

所有用例的共同点在于:模型处理前需对图像进行预处理。 这可能包括:调整图像尺寸以满足模型输入要求、将图像裁剪为正方形以实现中心裁剪,或应用高斯滤波、Canny算法等高级算法,辅助模型完成图像检测、分类或处理任务。

本章不探讨图像处理算法本身,而是提供在Java中应用这些算法的基本时机与方法。 完成本章学习后,您将能够有效运用这些图像处理算法,处理数据工程师或视觉专家提供的各类应用场景。

但在深入图像处理前,让我们先厘清图像的本质。理解图像在内存中的存储方式,是掌握图像处理机制的关键。

图像由像素构成,每个像素代表图像中的一个点。像素总数取决于图像的尺寸(宽度和高度)。

每个像素包含该点位的信息,包括颜色、透明度以及基于图像格式的其他属性。例如:

  • 灰度图像中,像素值为0至255之间的整数,其中0代表黑色,255代表白色。

  • 在RGB(红、绿、蓝)图像中,像素由三个整数表示红、绿、蓝三原色分量。例如数值255、0、255组合呈现粉红色。

  • 在RGBA(红、绿、蓝、透明度)图像中,像素由四个整数表示,包含RGB值及透明度参数。

一张4×4的RGB格式图像(共16个像素)由三个三维整数矩阵构成(每种颜色各一个),取值范围为0至255。图11-1展示了 这种分解方式。

Diagram illustrating image decomposition into three color channels, each with specified width and height in pixels.
图11-1. 图像 分解

图像处理是在像素层面对矩阵进行修改——例如将接近零的值改为精确的零值。接下来我们将探讨如何在Java中实现图像处理。

OpenCV

开源计算机视觉库(OpenCV) 是一个基于 Apache 2.0 许可证开发的 C++ 库,用于编程实时计算机视觉算法和图像处理。 该库实现了超过 2,500 种算法。OpenCV 支持 GPU 加速,使其成为处理大型或实时图像的理想选择。

OpenCV支持的图像处理主要操作如下:

图像采集

通过从磁盘加载或从相机捕获获取图像。此操作可包含调整尺寸、色彩转换、裁剪及其他调整。

图像增强

修改图像亮度、对比度等参数以提升视觉质量。

图像修复

修正影响图像质量的缺陷,如噪点或运动模糊。

彩色图像处理

通过色彩平衡、色彩校正或自动白平衡调整颜色。

形态学处理

通过膨胀与腐蚀等算法分析图像内部形状以提取有用信息。

分割

将图像划分为多个区域以进行详细场景分析。

尽管OpenCV采用C++编写,但名为OpenPnP OpenCV的Java绑定项目通过Java本机接口(JNI)实现了在Java环境中原生加载和使用OpenCV。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

产品思维工程师 (Chinese Edition)

产品思维工程师 (Chinese Edition)

Drew Hoskins

Publisher Resources

ISBN: 0642572279905