質問編集履歴
2
文の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
Opencvを利用し楽譜の画像を読み込み五線譜の輪郭をなんとなくとることができたのですが、これをそれぞれの囲みごとにトリミングしたいのですが方法が分かりません。
|
2
|
+
やろうと思ったのは Imgproc.findContoursで得た座標を使ってトリミングしようと思ったのですが具体的に何を使用すればよいかわかりません。どうかお願いします。
|
2
3
|
### ソース
|
3
4
|
```java
|
4
5
|
cvtColor(mat, mat, COLOR_RGB2GRAY);
|
1
画像の追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
OpenCVで検出した輪郭をトリミングしたい
|
body
CHANGED
@@ -1,31 +1,33 @@
|
|
1
|
-
|
1
|
+
Opencvを利用し楽譜の画像を読み込み五線譜の輪郭をなんとなくとることができたのですが、これをそれぞれの囲みごとにトリミングしたいのですが方法が分かりません。
|
2
2
|
### ソース
|
3
3
|
```java
|
4
|
-
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
|
5
|
-
|
6
|
-
@Override
|
7
|
-
public void onClick(View v) {
|
8
|
-
|
9
|
-
Mat mat = new Mat();
|
10
|
-
Mat den = new Mat();
|
11
|
-
Mat adp = new Mat();
|
12
|
-
Mat bin = new Mat();
|
13
|
-
Utils.bitmapToMat(bmp,mat);
|
14
|
-
|
15
|
-
|
4
|
+
cvtColor(mat, mat, COLOR_RGB2GRAY);
|
16
|
-
Photo.fastNlMeansDenoising(mat,den, 6, 7, 21);
|
5
|
+
Photo.fastNlMeansDenoising(mat, den, 6, 7, 21);
|
17
|
-
Imgproc.adaptiveThreshold(den,adp, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 17, 2);
|
6
|
+
Imgproc.adaptiveThreshold(den, adp, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 17, 2);
|
18
|
-
Core.bitwise_not(adp,bin);
|
7
|
+
Core.bitwise_not(adp, bin);
|
19
8
|
Mat linesMat = new Mat();
|
20
|
-
int width = 0;
|
21
|
-
Imgproc.HoughLinesP(
|
9
|
+
Imgproc.HoughLinesP(bin, linesMat, 1d, Math.PI / 360d, 200, 100d, 23);
|
22
10
|
|
23
11
|
|
24
|
-
|
12
|
+
Mat bmpmat = new Mat();
|
25
|
-
|
26
|
-
Utils.
|
13
|
+
Utils.bitmapToMat(bmp, bmpmat);
|
14
|
+
double[] data;
|
15
|
+
Point pt1 = new Point();
|
16
|
+
Point pt2 = new Point();
|
27
|
-
|
17
|
+
int rouws = linesMat.rows();
|
18
|
+
for (int i = 0; i < rouws; i++) {
|
19
|
+
data = linesMat.get(i, 0);
|
20
|
+
pt1.x = data[0];
|
21
|
+
pt1.y = data[1];
|
22
|
+
pt2.x = data[2];
|
23
|
+
pt2.y = data[3];
|
24
|
+
Imgproc.line(bmpmat, pt1, pt2, new Scalar(255, 0, 0), 18);
|
28
|
-
|
25
|
+
}
|
26
|
+
cvtColor(bmpmat, bmpmat, COLOR_RGB2GRAY);
|
27
|
+
Core.bitwise_not(bmpmat,mat_output);
|
28
|
+
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
29
|
-
|
29
|
+
Mat hierarchy = new Mat();
|
30
|
-
|
30
|
+
Imgproc.findContours(mat_output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
31
|
+
Imgproc.drawContours(mat, contours, -1, new Scalar(0, 255, 0), 1);
|
31
|
-
```
|
32
|
+
```
|
33
|
+

|