回答編集履歴
7
mainについてコメント追加 getMatchPointsの無駄なメソッド呼び出しを改善
answer
CHANGED
@@ -32,20 +32,24 @@
|
|
32
32
|
//平滑化
|
33
33
|
Imgproc.GaussianBlur(im, avem2, new Size(17,13), 0, 0);
|
34
34
|
|
35
|
+
//書き込み
|
35
|
-
Imgcodecs.imwrite("out.png", avem2);
|
36
|
+
//Imgcodecs.imwrite("out.png", avem2);
|
37
|
+
|
38
|
+
//画素値250以上のpointを取得してsetに代入
|
36
39
|
Set<IntPoint> s=getMatchPoints(avem2,c->c>(byte)250);//255*0.98
|
40
|
+
//出力
|
37
41
|
s.forEach(System.out::println);
|
38
42
|
}
|
39
43
|
/**
|
40
44
|
* org.opencv.core.Pointはdoubleしか扱えない罠
|
41
45
|
* @param avem2 グレースケールあるいは二値画像
|
42
|
-
* @param tester
|
46
|
+
* @param tester 条件 ちなみに画素値が渡されてくる
|
43
47
|
* @return 条件を満たした座標のデータ
|
44
48
|
*/
|
45
49
|
public static Set<IntPoint> getMatchPoints(Mat avem2,BytePredicate tester){
|
46
50
|
int width=avem2.width();
|
47
51
|
int height=avem2.height();
|
48
|
-
byte [] data =new byte[
|
52
|
+
byte [] data =new byte[width*height];
|
49
53
|
avem2.get(0,0,data);
|
50
54
|
HashSet<IntPoint> ret=new HashSet<>();
|
51
55
|
for(int y=0;y<height;y++) {
|
6
ファイル名の抜け修正
answer
CHANGED
@@ -92,7 +92,7 @@
|
|
92
92
|
}
|
93
93
|
|
94
94
|
```
|
95
|
-
|
95
|
+
__BytePredicate.java__
|
96
96
|
```java
|
97
97
|
package tsukka.utility.function;
|
98
98
|
|
5
バグ修正
answer
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
__Q103546.java__
|
1
2
|
```java
|
2
3
|
package tsukka.teratail.questions.q103546;
|
3
4
|
|
4
5
|
import java.util.HashSet;
|
5
6
|
import java.util.Set;
|
6
|
-
import java.util.function.IntPredicate;
|
7
7
|
import org.opencv.core.Core;
|
8
8
|
import org.opencv.core.CvType;
|
9
9
|
import org.opencv.core.Mat;
|
10
10
|
import org.opencv.core.Size;
|
11
11
|
import org.opencv.imgcodecs.Imgcodecs;
|
12
12
|
import org.opencv.imgproc.Imgproc;
|
13
|
+
import tsukka.utility.function.BytePredicate;
|
13
14
|
|
14
15
|
/**
|
15
16
|
*
|
@@ -39,14 +40,14 @@
|
|
39
40
|
* org.opencv.core.Pointはdoubleしか扱えない罠
|
40
41
|
* @param avem2 グレースケールあるいは二値画像
|
41
42
|
* @param tester
|
42
|
-
* @return
|
43
|
+
* @return 条件を満たした座標のデータ
|
43
44
|
*/
|
44
|
-
public static Set<IntPoint> getMatchPoints(Mat avem2,
|
45
|
+
public static Set<IntPoint> getMatchPoints(Mat avem2,BytePredicate tester){
|
46
|
+
int width=avem2.width();
|
47
|
+
int height=avem2.height();
|
45
48
|
byte [] data =new byte[avem2.width()*avem2.height()];
|
49
|
+
avem2.get(0,0,data);
|
46
50
|
HashSet<IntPoint> ret=new HashSet<>();
|
47
|
-
avem2.get(0,0,data);
|
48
|
-
int width=avem2.width();
|
49
|
-
int height=avem2.height();
|
50
51
|
for(int y=0;y<height;y++) {
|
51
52
|
for (int x = 0; x < width; x++) {
|
52
53
|
if(tester.test(data[y*width+x])){
|
@@ -56,22 +57,63 @@
|
|
56
57
|
}
|
57
58
|
return ret;
|
58
59
|
}
|
59
|
-
public static class IntPoint{
|
60
|
-
public int x,y;
|
61
|
-
public IntPoint(int x,int y){
|
62
|
-
this.x=x;
|
63
|
-
this.y=y;
|
64
|
-
}
|
65
60
|
|
66
|
-
@Override
|
67
|
-
public String toString() {
|
68
|
-
return '(' + Integer.toString(x) + ',' + Integer.toString(y) + ')';
|
69
|
-
|
61
|
+
}
|
70
|
-
|
62
|
+
```
|
63
|
+
|
64
|
+
__IntPoint.java__
|
65
|
+
```java
|
66
|
+
package tsukka.teratail.questions.q103546;
|
67
|
+
|
68
|
+
import org.opencv.core.Point;
|
69
|
+
|
70
|
+
/**
|
71
|
+
*
|
72
|
+
* @author tsukka
|
73
|
+
*/
|
74
|
+
public class IntPoint {
|
75
|
+
|
76
|
+
public int x, y;
|
77
|
+
|
78
|
+
public IntPoint(int x, int y) {
|
79
|
+
this.x = x;
|
80
|
+
this.y = y;
|
71
81
|
}
|
82
|
+
|
83
|
+
|
84
|
+
@Override
|
85
|
+
public String toString() {
|
86
|
+
return '(' + Integer.toString(x) + ',' + Integer.toString(y) + ')';
|
87
|
+
}
|
88
|
+
|
89
|
+
public Point toPoint() {
|
90
|
+
return new Point(x, y);
|
91
|
+
}
|
72
92
|
}
|
73
93
|
|
74
94
|
```
|
95
|
+
|
96
|
+
```java
|
97
|
+
package tsukka.utility.function;
|
98
|
+
|
99
|
+
/**
|
100
|
+
*
|
101
|
+
* @author tsukka
|
102
|
+
*/
|
103
|
+
@FunctionalInterface
|
104
|
+
public interface BytePredicate {
|
105
|
+
default BytePredicate and(BytePredicate other){
|
106
|
+
return b->this.test(b)&&other.test(b);
|
107
|
+
}
|
108
|
+
default BytePredicate negate(){
|
109
|
+
return b->!this.test(b);
|
110
|
+
}
|
111
|
+
default BytePredicate or(BytePredicate other) {
|
112
|
+
return b->this.test(b)||other.test(b);
|
113
|
+
}
|
114
|
+
boolean test(byte value);
|
115
|
+
}
|
116
|
+
```
|
75
117
|
byteでならとれると思われる
|
76
118
|
ちなみに白なら-1(255)黒なら0
|
77
119
|
1ピクセル当たり1インデックス
|
4
androidでの補足
answer
CHANGED
@@ -74,4 +74,6 @@
|
|
74
74
|
```
|
75
75
|
byteでならとれると思われる
|
76
76
|
ちなみに白なら-1(255)黒なら0
|
77
|
-
1ピクセル当たり1インデックス
|
77
|
+
1ピクセル当たり1インデックス
|
78
|
+
|
79
|
+
ラムダとか使えないなら適宜書き換えてください
|
3
詳細なソース
answer
CHANGED
@@ -1,10 +1,76 @@
|
|
1
1
|
```java
|
2
|
+
package tsukka.teratail.questions.q103546;
|
3
|
+
|
4
|
+
import java.util.HashSet;
|
5
|
+
import java.util.Set;
|
6
|
+
import java.util.function.IntPredicate;
|
7
|
+
import org.opencv.core.Core;
|
8
|
+
import org.opencv.core.CvType;
|
9
|
+
import org.opencv.core.Mat;
|
10
|
+
import org.opencv.core.Size;
|
11
|
+
import org.opencv.imgcodecs.Imgcodecs;
|
12
|
+
import org.opencv.imgproc.Imgproc;
|
13
|
+
|
14
|
+
/**
|
15
|
+
*
|
16
|
+
* @author tsukka
|
17
|
+
*/
|
18
|
+
public class Q103546 {
|
19
|
+
public static void main (String... args){
|
20
|
+
System.out.println(System.getProperty("java.library.path"));
|
21
|
+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
22
|
+
//画像読み込み
|
2
|
-
Mat im = Imgcodecs.imread("test.png"); // 入力画像の取得
|
23
|
+
Mat im = Imgcodecs.imread("test.png"); // 入力画像の取得
|
24
|
+
|
3
|
-
//二値化
|
25
|
+
//二値化
|
4
|
-
Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY); //グレースケールに変換
|
26
|
+
Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY); //グレースケールに変換
|
5
|
-
Imgproc.threshold(im, im, 0, 255, Imgproc.THRESH_OTSU); //閾値を自動で設定
|
27
|
+
Imgproc.threshold(im, im, 0, 255, Imgproc.THRESH_OTSU); //閾値を自動で設定
|
28
|
+
//二値化ここまで
|
29
|
+
|
30
|
+
Mat avem2=new Mat(im.size(),CvType.CV_8U);
|
31
|
+
//平滑化
|
32
|
+
Imgproc.GaussianBlur(im, avem2, new Size(17,13), 0, 0);
|
33
|
+
|
34
|
+
Imgcodecs.imwrite("out.png", avem2);
|
35
|
+
Set<IntPoint> s=getMatchPoints(avem2,c->c>(byte)250);//255*0.98
|
36
|
+
s.forEach(System.out::println);
|
37
|
+
}
|
38
|
+
/**
|
39
|
+
* org.opencv.core.Pointはdoubleしか扱えない罠
|
40
|
+
* @param avem2 グレースケールあるいは二値画像
|
41
|
+
* @param tester
|
42
|
+
* @return
|
43
|
+
*/
|
44
|
+
public static Set<IntPoint> getMatchPoints(Mat avem2,IntPredicate tester){
|
6
|
-
byte [] data =new byte[
|
45
|
+
byte [] data =new byte[avem2.width()*avem2.height()];
|
46
|
+
HashSet<IntPoint> ret=new HashSet<>();
|
7
|
-
|
47
|
+
avem2.get(0,0,data);
|
48
|
+
int width=avem2.width();
|
49
|
+
int height=avem2.height();
|
50
|
+
for(int y=0;y<height;y++) {
|
51
|
+
for (int x = 0; x < width; x++) {
|
52
|
+
if(tester.test(data[y*width+x])){
|
53
|
+
ret.add(new IntPoint(x,y));
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
return ret;
|
58
|
+
}
|
59
|
+
public static class IntPoint{
|
60
|
+
public int x,y;
|
61
|
+
public IntPoint(int x,int y){
|
62
|
+
this.x=x;
|
63
|
+
this.y=y;
|
64
|
+
}
|
65
|
+
|
66
|
+
@Override
|
67
|
+
public String toString() {
|
68
|
+
return '(' + Integer.toString(x) + ',' + Integer.toString(y) + ')';
|
69
|
+
}
|
70
|
+
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
8
74
|
```
|
9
75
|
byteでならとれると思われる
|
10
76
|
ちなみに白なら-1(255)黒なら0
|
2
補足説明
answer
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
```java
|
2
2
|
Mat im = Imgcodecs.imread("test.png"); // 入力画像の取得
|
3
|
+
//二値化
|
4
|
+
Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY); //グレースケールに変換
|
5
|
+
Imgproc.threshold(im, im, 0, 255, Imgproc.THRESH_OTSU); //閾値を自動で設定
|
3
6
|
byte [] data =new byte[im.width()*im.height()];
|
4
7
|
im.get(0,0,data);
|
5
8
|
```
|
6
|
-
byteでならとれると思われる
|
9
|
+
byteでならとれると思われる
|
10
|
+
ちなみに白なら-1(255)黒なら0
|
11
|
+
1ピクセル当たり1インデックス
|
1
まぬけなことにgetを入れ忘れたので入れた
answer
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
```java
|
2
2
|
Mat im = Imgcodecs.imread("test.png"); // 入力画像の取得
|
3
3
|
byte [] data =new byte[im.width()*im.height()];
|
4
|
+
im.get(0,0,data);
|
4
5
|
```
|
5
6
|
byteでならとれると思われる
|