java 图片轮廓检测_opencv 一 轮廓识别

本文介绍了如何使用Java结合OpenCV库进行图像处理,通过二值化获取房间轮廓,提取房间的拐角坐标,并利用地图API将这些坐标转换为地图上的位置,最终在Google Map上展示房间的形状和位置。内容包括二值化、轮廓提取、角点检测和图像识别等步骤。
摘要由CSDN通过智能技术生成

基于opencv java api实现图像识别

需求是将图片上的房间多边形识别出来后绘制到地图上,使用户可以在地图上看房间的位置,朝向以及临街等信息并可以选择入住指定房间(给工资就干,需求合不合理我说了不算)

大致思路是识别房间拐角坐标,在根据地图api获取图片左上和右下角的坐标值(经纬度转坐标,amap googlemap地图api支持)

再根据坐标值计算每个角在地图中的坐标值,最后将地图坐标值绘制多边形

代码很多就不一一复制, 有需要代码的评论/私聊我

opencv:

1:二值获取房间矩形

Core.inRange(src, new Scalar(0, 0, 215), new Scalar(154, 157, 243), mask);

30dfeab6c0c3

1599467171(1).jpg

2:轮廓提取&截取房间号

Imgproc.findContours(mask, contours, new Mat(), Imgproc.RETR_EXTERNAL,

//二值房间号房间图片

Core.inRange(mdc, new Scalar(89, 44, 28), new Scalar(113, 74, 63), make);

//高斯模糊( 由于房间号数字之间可能存在空格高斯模糊后获取一个整体轮廓矩形)

Imgproc.GaussianBlur(make, mk2, new Size(9, 9), 9, 9);

Imgproc.findContours(mk2, roomRectPointList, new Mat(), Img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaOpenCV3来识别图像中的表格,可以按照以下步骤进行操作: 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”的文件,其中包含了从原图中提取出的表格区域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值