java边缘轮廓提取_matlab 提取图像轮廓(图像边缘提取)
时间: 2023-08-11 14:24:54 浏览: 167
对于Java,可以使用JavaCV库进行边缘检测和轮廓提取。JavaCV是一个基于OpenCV和FFmpeg的Java接口库,可以用于图像和视频处理。以下是一个简单的JavaCV边缘检测和轮廓提取的示例代码:
```java
import org.bytedeco.javacpp.opencv_core.*;
import org.bytedeco.javacpp.opencv_imgcodecs;
import org.bytedeco.javacpp.opencv_imgproc;
public class EdgeDetectionExample {
public static void main(String[] args) {
// Load image
Mat image = opencv_imgcodecs.imread("input.jpg");
// Convert to grayscale
Mat grayImage = new Mat();
opencv_imgproc.cvtColor(image, grayImage, opencv_imgproc.CV_BGR2GRAY);
// Apply Canny edge detection
Mat edges = new Mat();
opencv_imgproc.Canny(grayImage, edges, 100, 200);
// Find contours
MatVector contours = new MatVector();
Mat hierarchy = new Mat();
opencv_imgproc.findContours(edges, contours, hierarchy, opencv_imgproc.CV_RETR_EXTERNAL, opencv_imgproc.CV_CHAIN_APPROX_SIMPLE);
// Draw contours on original image
Mat result = new Mat();
opencv_core.Scalar color = new opencv_core.Scalar(0, 255, 0, 0);
opencv_imgproc.drawContours(image, contours, -1, color, 2, opencv_imgproc.LINE_8, hierarchy, 0, new opencv_core.Point());
// Save result
opencv_imgcodecs.imwrite("output.jpg", image);
}
}
```
对于Matlab,可以使用以下代码进行图像边缘提取:
```matlab
% Load image
image = imread('input.jpg');
% Convert to grayscale
grayImage = rgb2gray(image);
% Apply edge detection using Canny method
edges = edge(grayImage, 'canny');
% Find contours
[B,L] = bwboundaries(edges, 'noholes');
% Draw contours on original image
imshow(image);
hold on;
for i=1:length(B)
boundary = B{i};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);
end
```
这里使用了Canny边缘检测方法和bwboundaries函数来提取轮廓。在显示轮廓时,使用了plot函数绘制轮廓边界。
阅读全文