簡體   English   中英

使用opencv在Android中繪制輪廓

[英]draw contours in Android using opencv

我在 Android 中有處理圖像並返回二進制圖像的代碼。

  Imgproc.cvtColor(source, middle, Imgproc.COLOR_RGB2GRAY);
    Mat element  = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(15, 15), new Point(0, 0));
    Imgproc.morphologyEx(middle, middle, MORPH_TOPHAT, element, new Point(0, 0));
    Imgproc.threshold(middle, middle, 20, 255, Imgproc.THRESH_BINARY);

在此處輸入圖片說明

[在此處輸入圖片說明 ][2

現在,我的要求是,我需要突出顯示原始圖像上的凹痕,而不是二進制圖像。 像這樣:

在此處輸入圖片說明

我想出的是使用

   Core.findnonzero() 

獲取凹痕的坐標,然后在原始圖像上使用 drawcontours。這只是一個想法。

我的問題是: 1. 最好的方法是什么? 2. 什么是matofpoint?

您可以在二進制圖像(代碼middle findContours()使用 OpenCV 的findContours()函數,然后遍歷列表中的所有頂級輪廓並將它們繪制在原始(源)圖像上,就像這樣:

List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(middle, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

// now iterate over all top level contours
for(int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) {
    MatOfPoint matOfPoint = contours.get(idx);
    Rect rect = Imgproc.boundingRect(matOfPoint);
    Imgproc.rectangle(originalImage, rect.tl(), rect.br(), new Scalar(0, 0, 255));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM