2016-04-06 11 views
0

私は、分離した音符表記を使用して、ピクチャ内のすべての要素の輪郭を描こうとしています。音楽表記のOpenCv等高線矩形

musical notations

これは私がアンドロイド/ Javaで実行しているコードです:私は取得しています

public static Bitmap findNotationContours(Bitmap inputImage) { 
Mat inputImageMat = new Mat(); 
Utils.bitmapToMat(inputImage, inputImageMat); 

Imgproc.cvtColor(inputImageMat, inputImageMat, Imgproc.COLOR_BGR2GRAY); 
Imgproc.GaussianBlur(inputImageMat, inputImageMat, new Size(5, 5), 0); 
Imgproc.adaptiveThreshold(inputImageMat, inputImageMat, 255, 1, 1, 11, 2); 

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

int contourColor = android.R.color.holo_red_dark; 
Scalar contourScalar = new Scalar(Color.red(contourColor), Color.green(contourColor), Color.blue(contourColor)); 

for (int i = 0; i < contours.size(); i++) { 
    Rect rect = Imgproc.boundingRect(contours.get(i)); 
    Imgproc.rectangle(inputImageMat, 
      new Point(rect.x, rect.y), 
      new Point(rect.x + rect.width, rect.y + rect.height), 
      contourScalar, 3); 
} 


Utils.matToBitmap(inputImageMat, inputImage); 
return inputImage; 

}

結果は次のとおりです。

result

あなたが十分にズームインすると、表記の輪郭はそこにありますが、元の絵をそれぞれの周りに矩形の輪郭だけで残しておく必要がありますので、パターンとして保存することができます。 私が間違っていることを教えてもらえますか?

+0

は、すでに1チャンネル(グレースケール)画像に変え、まだそれは3であったかのように、あなたはそれに描画しますチャンネル画像(RGB)。 –

+0

私は最初のinputImageの別のMatを作成しました。そして、その上の1チャンネルの画像から見つかった四角形を描きました。そして問題を修正しました。だから、ありがとうございました。 –

答えて

0

重要な情報は@ダンMašek問題が修正された、私は私たちが輪郭を描画するために使用する最初の3チャネルの画像とマットを保持するだけで説明を必要としない改訂コードを追加します四角形が表示されます。もし `inputImageMat`に描画開始時間により

public static Bitmap findNotationContours(Bitmap inputImage) { 
Mat inputImageMat = new Mat(); 
Mat resultImageMat = new Mat(); 
Utils.bitmapToMat(inputImage, inputImageMat); 
Utils.bitmapToMat(inputImage, resultImageMat); 

Imgproc.cvtColor(inputImageMat, inputImageMat, Imgproc.COLOR_BGR2GRAY); 
Imgproc.GaussianBlur(inputImageMat, inputImageMat, new Size(5, 5), 0); 
Imgproc.adaptiveThreshold(inputImageMat, inputImageMat, 255, 1, 1, 11, 2); 

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

Scalar contourScalar = new Scalar(255,0,0); 

for (int i = 0; i < contours.size(); i++) { 
    Rect rect = Imgproc.boundingRect(contours.get(i)); 
    Imgproc.rectangle(resultImageMat, 
      new Point(rect.x, rect.y), 
      new Point(rect.x + rect.width, rect.y + rect.height), 
      contourScalar, 3); 
    Log.i("contour", "contour" + i + " x:" + rect.x + " y:" + rect.y); 
} 


Utils.matToBitmap(resultImageMat, inputImage); 
return inputImage; 

}

final result