java 表格识别_OpenCV3 识别图中表格-JAVA 实现
时间: 2024-02-05 15:13:15 浏览: 37
要使用Java和OpenCV3来识别图像中的表格,可以按照以下步骤进行操作:
1. 导入OpenCV库并读取图像文件
```java
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class TableRecognition {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat image = Imgcodecs.imread("table.jpg");
}
}
```
2. 将图像转换为灰度图像
```java
Mat grayImage = new Mat();
Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);
```
3. 对灰度图像进行二值化处理
```java
Mat binaryImage = new Mat();
Imgproc.threshold(grayImage, binaryImage, 0, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU);
```
4. 对二值化图像进行形态学处理,消除噪点和连接线条
```java
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Mat morphImage = new Mat();
Imgproc.morphologyEx(binaryImage, morphImage, Imgproc.MORPH_OPEN, kernel);
```
5. 查找并筛选轮廓
```java
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(morphImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
List<MatOfPoint> tableContours = new ArrayList<>();
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
double area = Imgproc.contourArea(contour);
if (area > 1000 && area < 100000) {
double[] hierarchyData = hierarchy.get(0, i);
int parentIdx = (int) hierarchyData[3];
if (parentIdx == -1) {
tableContours.add(contour);
} else {
int childCount = countChildren(hierarchy, parentIdx);
if (childCount == 1) {
tableContours.add(contour);
}
}
}
}
```
6. 从原图中提取表格区域
```java
Rect tableRect = Imgproc.boundingRect(new MatOfPoint(tableContours.get(0).toArray()));
Mat tableImage = image.submat(tableRect);
```
完整代码:
```java
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.List;
public class TableRecognition {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat image = Imgcodecs.imread("table.jpg");
Mat grayImage = new Mat();
Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);
Mat binaryImage = new Mat();
Imgproc.threshold(grayImage, binaryImage, 0, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Mat morphImage = new Mat();
Imgproc.morphologyEx(binaryImage, morphImage, Imgproc.MORPH_OPEN, kernel);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(morphImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
List<MatOfPoint> tableContours = new ArrayList<>();
for (int i = 0; i < contours.size(); i++) {
MatOfPoint contour = contours.get(i);
double area = Imgproc.contourArea(contour);
if (area > 1000 && area < 100000) {
double[] hierarchyData = hierarchy.get(0, i);
int parentIdx = (int) hierarchyData[3];
if (parentIdx == -1) {
tableContours.add(contour);
} else {
int childCount = countChildren(hierarchy, parentIdx);
if (childCount == 1) {
tableContours.add(contour);
}
}
}
}
Rect tableRect = Imgproc.boundingRect(new MatOfPoint(tableContours.get(0).toArray()));
Mat tableImage = image.submat(tableRect);
Imgcodecs.imwrite("table_result.jpg", tableImage);
}
private static int countChildren(Mat hierarchy, int idx) {
int childCount = 0;
idx = (int) hierarchy.get(0, idx)[2];
while (idx != -1) {
childCount++;
idx = (int) hierarchy.get(0, idx)[0];
}
return childCount;
}
}
```
运行程序后,将会生成一个名为“table_result.jpg”的文件,其中包含了从原图中提取出的表格区域。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)