2017-07-31 89 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問題。您可以將您Imgproc.findContours()通話,而不是for (int contourIdx = ...後做到這一點,例如,通過這種方式(在下方插入代碼:

// 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); 

然後改變gray墊在Utils.matToBitmap()調用masked

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