2017-07-31 15 views
0

輪郭線を黒い物体だけで描くには、どうすればよいですか? 私のコードは、現在の画像上で輪郭を描くことが可能です:Android、OpenCV:色を検出して輪郭線を描く

Bitmap b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length); 
srcMat= new Mat(); 
Utils.bitmapToMat(b,srcMat); 

Mat gray = new Mat(); 
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY); 

Imgproc.Canny(gray, gray, 20, 20*3, 3, true); 
List<MatOfPoint> contours = new ArrayList<>(); 
Mat hierarchy = new Mat(); 

Imgproc.findContours(gray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) { 
    Imgproc.drawContours(srcMat, contours, contourIdx, new Scalar(0, 0, 255), -1); 
} 

Utils.matToBitmap(gray, b); 
imgR.setImageBitmap(b); 
+0

あなたが作成したいです輪郭を作成したり、ここであなたが話している他の種類の輪郭を作成したりして、画像のマスクを作成してください。 達成しようとしている出力を提供してください。 – arqam

答えて

1

あなたはthis質問への答えのように、マスクを作成して適用する必要があります。 maskedUtils.matToBitmap()呼び出しでgrayマットを変更すると

// create Mat for mask 
Mat mask = new Mat(new Size(srcMat.cols(), srcMat.rows()), CvType.CV_8UC1); 
mask.setTo(new Scalar(0.0)); 

// create Scalar for color of mask objects 
Scalar white = new Scalar(255, 255, 255); 

// draw contours border and fill them 
Imgproc.drawContours(mask, contours, -1, white, 10); 
for (MatOfPoint contour: contours) { 
    Imgproc.fillPoly(mask, Arrays.asList(contour), white); 
} 

// create mat foe masked image 
Mat masked = new Mat(); 

// apply mask to srcMat and set result to masked 
srcMat.copyTo(masked, mask); 

:あなたはこの方法(代わりにfor (int contourIdx = ...のごImgproc.findContours()呼び出した後、以下の挿入コードによって、例えば、これを行うことができ

Utils.matToBitmap(masked, b); 
imgR.setImageBitmap(b);