椭圆的绘制
OpenCV中椭圆的绘制依赖于ellipse() 这个函数。下面是该函数的参数说明。
-
img
输入源,Mat对象类型。 -
box
椭圆的参数说明,RotatedRect()类型的结构体。该结构体接受一个Point对象用以确定椭圆的质心,一个Size对象用以确定与椭圆外切的一个虚拟矩形的宽和高,以及旋转角度(已转换为角度制)。 -
color
颜色对象 -
thickness
厚度。
下面是RotatedRect()结构体内参数在二维平面下的示意图:
Java代码(JavaFX Controlle层)
public class Controller{
@FXML private Text fxText;
@FXML private ImageView imageView;
@FXML public void handleButtonEvent(ActionEvent actionEvent) throws IOException {
Node source = (Node) actionEvent.getSource();
Window theStage = source.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files(*.jpg)", "*.jpg"));
File file = fileChooser.showOpenDialog(theStage);
runInSubThread(file.getPath());
}
private void runInSubThread(String filePath){
new Thread(new Runnable() {
@Override
public void run() {
try {
WritableImage writableImage = drawEllipse(filePath);
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(writableImage);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private WritableImage drawEllipse(String filePath) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread(filePath);
Imgproc.ellipse(src, new RotatedRect(new Point(215,130), new Size(150,100),270), new Scalar(0,255,255),2);
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", src, matOfByte);
byte[] bytes = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bufImage = ImageIO.read(in);
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
}
运行图