質問編集履歴
5
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
https://akira-watson.com/android/helloworld.html
|
9
9
|
|
10
10
|
|
11
|
-
以前は掲載のソースファイルにそのまま
|
11
|
+
以前は掲載のソースファイルにそのまま貼り付け、確かに同じエラーは出ていたのですが、
|
12
12
|
以前AndroidXが関係しているとの回答を頂きましたので以下のURL参考にして
|
13
13
|
|
14
14
|
androidXの改変
|
@@ -18,9 +18,9 @@
|
|
18
18
|
import androidx.appcompat.app.AppCompatActivityに改変し、
|
19
19
|
|
20
20
|
|
21
|
-
自身が画像比較アプリを
|
21
|
+
自身が画像比較アプリを貼り付けるする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
|
22
22
|
|
23
|
-
|
23
|
+
貼り付ける前にAndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残した貼り付けの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
|
24
24
|
|
25
25
|
Kotlin6 MainActivity.xml(自分で記載のURLよりソース改変)
|
26
26
|
```ここに言語を入力
|
4
title
CHANGED
File without changes
|
body
CHANGED
@@ -215,4 +215,8 @@
|
|
215
215
|
}
|
216
216
|
}
|
217
217
|
|
218
|
-
```
|
218
|
+
```
|
219
|
+
|
220
|
+
文字数最大数行きましたので、Android Studioでの画像比較サンプルアプリ実行エラー 2の方にKotlin6 activity_main.xml、TestApp001のMainActivity.Javaとactivity_main.xmlのソース掲載しております。
|
221
|
+
|
222
|
+
時間がある方いらっしゃればご回答お願い致します。
|
3
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Android Studioでのサンプルアプリ実行エラー
|
1
|
+
Android Studioでの画像比較サンプルアプリ実行エラー
|
body
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
|
23
23
|
コピペする前のAndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残したコピペの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
|
24
24
|
|
25
|
-
Kotlin6 MainActivity.xml
|
25
|
+
Kotlin6 MainActivity.xml(自分で記載のURLよりソース改変)
|
26
26
|
```ここに言語を入力
|
27
27
|
|
28
28
|
package com.example.kotlin6;
|
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,5 +24,195 @@
|
|
24
24
|
|
25
25
|
Kotlin6 MainActivity.xml
|
26
26
|
```ここに言語を入力
|
27
|
+
|
28
|
+
package com.example.kotlin6;
|
29
|
+
|
30
|
+
import androidx.appcompat.app.AppCompatActivity;
|
31
|
+
|
32
|
+
import android.os.Bundle;
|
33
|
+
|
34
|
+
import android.app.Activity
|
35
|
+
import android.content.Intent
|
36
|
+
import android.graphics.Bitmap
|
37
|
+
import android.graphics.BitmapFactory
|
38
|
+
import android.graphics.drawable.BitmapDrawable
|
39
|
+
import android.net.Uri
|
40
|
+
import androidx.appcompat.app.AppCompatActivity
|
41
|
+
import android.os.Bundle
|
42
|
+
import android.os.ParcelFileDescriptor
|
43
|
+
import android.util.Log
|
44
|
+
import android.widget.ImageView
|
45
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
46
|
+
import org.opencv.android.OpenCVLoader
|
47
|
+
import org.opencv.android.Utils
|
48
|
+
import org.opencv.core.*
|
49
|
+
import org.opencv.features2d.AKAZE
|
50
|
+
import org.opencv.features2d.DescriptorMatcher
|
51
|
+
import org.opencv.features2d.Features2d
|
52
|
+
import org.opencv.imgproc.Imgproc
|
53
|
+
import java.io.FileDescriptor
|
54
|
+
import java.io.IOException
|
55
|
+
|
56
|
+
|
57
|
+
public class MainActivity extends AppCompatActivity {
|
58
|
+
|
59
|
+
@Override
|
60
|
+
protected void onCreate(Bundle savedInstanceState) {
|
61
|
+
super.onCreate(savedInstanceState);
|
62
|
+
setContentView(R.layout.activity_main);
|
63
|
+
|
64
|
+
if(!OpenCVLoader.initDebug()) {
|
65
|
+
Log.d("OpenCV", "error_openCV")
|
66
|
+
}
|
67
|
+
|
68
|
+
// 画像選択ボタン1のリスナー
|
69
|
+
select_img1_btn.setOnClickListener {
|
70
|
+
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
71
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
72
|
+
intent.setType("*/*")
|
73
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE1)
|
74
|
+
}
|
75
|
+
|
76
|
+
// 画像選択ボタン2のリスナー
|
77
|
+
select_img2_btn.setOnClickListener {
|
78
|
+
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
79
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
80
|
+
intent.setType("*/*")
|
81
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE2)
|
82
|
+
}
|
83
|
+
|
84
|
+
// 決定ボタンのリスナー
|
85
|
+
decition_btn.setOnClickListener {
|
27
|
-
|
86
|
+
try {
|
87
|
+
// src_img1の画像をMatに
|
88
|
+
var bitmap: Bitmap = Bitmap.createScaledBitmap(getBitmapFromImageView(src_img1), 640, 480, false)
|
89
|
+
val scene1 = Mat(bitmap!!.height, bitmap!!.width, CvType.CV_8UC1).apply { Utils.bitmapToMat(bitmap, this) }
|
90
|
+
|
91
|
+
// src_img2の画像をMatに
|
92
|
+
bitmap = Bitmap.createScaledBitmap(getBitmapFromImageView(src_img2), 640, 480, false)
|
93
|
+
val scene2 = Mat(bitmap!!.height, bitmap!!.width, CvType.CV_8UC1).apply { Utils.bitmapToMat(bitmap, this) }
|
94
|
+
|
95
|
+
// アルゴリズムはAKZEで
|
96
|
+
val algorithm: AKAZE = AKAZE.create()
|
97
|
+
|
98
|
+
// 特徴点抽出
|
99
|
+
val keypoint1 = MatOfKeyPoint().apply { algorithm.detect(scene1, this) }
|
100
|
+
val keypoint2 = MatOfKeyPoint().apply { algorithm.detect(scene2, this) }
|
101
|
+
|
102
|
+
// 特徴量記述
|
103
|
+
val descriptor1 = Mat().apply { algorithm.compute(scene1, keypoint1, this) }
|
104
|
+
val descriptor2 = Mat().apply { algorithm.compute(scene2, keypoint2, this) }
|
105
|
+
|
106
|
+
// マッチング (アルゴリズムにはBruteForceを使用)
|
107
|
+
val matcher = DescriptorMatcher.create("BruteForce")
|
108
|
+
|
109
|
+
var matches_list: MutableList<DMatch> = mutableListOf()
|
110
|
+
val match12 = MatOfDMatch().apply { matcher.match(descriptor1, descriptor2, this) }
|
111
|
+
val match21 = MatOfDMatch().apply { matcher.match(descriptor2, descriptor1, this) }
|
112
|
+
|
113
|
+
// クロスチェック(1→2と2→1の両方でマッチしたものだけを残して精度を高める)
|
114
|
+
val size: Int = match12.toArray().size - 1
|
115
|
+
val match12_array = match12.toArray()
|
116
|
+
val match21_array = match21.toArray()
|
117
|
+
var count: Int = 0
|
118
|
+
for(i in 0..size) {
|
119
|
+
val forward: DMatch =match12_array[i]
|
120
|
+
val backward: DMatch = match21_array[forward.trainIdx]
|
121
|
+
if(backward.trainIdx == forward.queryIdx) {
|
122
|
+
matches_list.add(forward)
|
123
|
+
count++
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
val matches = MatOfDMatch().apply { this.fromList(matches_list) }
|
128
|
+
|
129
|
+
// 結果画像の背景真っ黒になるのを防ぐ
|
130
|
+
val scene1rgb = Mat().apply { Imgproc.cvtColor(scene1, this, Imgproc.COLOR_RGBA2RGB, 1) }
|
131
|
+
val scene2rgb = Mat().apply { Imgproc.cvtColor(scene2, this, Imgproc.COLOR_RGBA2RGB, 1) }
|
132
|
+
|
133
|
+
// マッチ結果を出力
|
134
|
+
val dest = scene1.clone().apply {
|
135
|
+
Features2d.drawMatches(scene1rgb, keypoint1, scene2rgb, keypoint2, matches, this)
|
136
|
+
}
|
137
|
+
|
138
|
+
val result_btm: Bitmap = Bitmap.createBitmap(dest.cols(), dest.rows(), Bitmap.Config.ARGB_8888)
|
139
|
+
.apply { Utils.matToBitmap(dest, this) }
|
140
|
+
|
141
|
+
// マッチング結果画像の出力
|
142
|
+
result_img.setImageBitmap(result_btm)
|
143
|
+
|
144
|
+
// マッチング数を出力
|
145
|
+
count_txt.text = "マッチング数: ${count}"
|
146
|
+
|
147
|
+
} catch(e: NullPointerException) {
|
148
|
+
e.printStackTrace()
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
}
|
153
|
+
|
154
|
+
// 画像を選択したときの動き
|
155
|
+
override fun onActivityResult(requestCode: Int, resultCode: Int, resultdata: Intent?) {
|
156
|
+
if((requestCode == RESULT_PICK_IMAGEFILE1 || requestCode == RESULT_PICK_IMAGEFILE2)
|
157
|
+
&& resultCode == Activity.RESULT_OK) {
|
158
|
+
val image_view: ImageView =
|
159
|
+
if(requestCode == RESULT_PICK_IMAGEFILE1) src_img1
|
160
|
+
else src_img2
|
161
|
+
|
162
|
+
if(resultdata?.data != null) {
|
163
|
+
try {
|
164
|
+
val uri: Uri = resultdata.data
|
165
|
+
val parcelFileDesc: ParcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r")
|
166
|
+
|
167
|
+
val fDesc: FileDescriptor = parcelFileDesc.fileDescriptor
|
168
|
+
val bmp: Bitmap = BitmapFactory.decodeFileDescriptor(fDesc)
|
169
|
+
parcelFileDesc.close()
|
170
|
+
image_view.setImageBitmap(bmp)
|
171
|
+
|
172
|
+
} catch(e: IOException) {
|
173
|
+
e.printStackTrace()
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
|
181
|
+
// 画像を選択したときの動き
|
182
|
+
override fun onActivityResult(requestCode: Int, resultCode: Int, resultdata: Intent?) {
|
183
|
+
if((requestCode == RESULT_PICK_IMAGEFILE1 || requestCode == RESULT_PICK_IMAGEFILE2)
|
184
|
+
&& resultCode == Activity.RESULT_OK) {
|
185
|
+
val image_view: ImageView =
|
186
|
+
if(requestCode == RESULT_PICK_IMAGEFILE1) src_img1
|
187
|
+
else src_img2
|
188
|
+
|
189
|
+
if(resultdata?.data != null) {
|
190
|
+
try {
|
191
|
+
val uri: Uri = resultdata.data
|
192
|
+
val parcelFileDesc: ParcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r")
|
193
|
+
|
194
|
+
val fDesc: FileDescriptor = parcelFileDesc.fileDescriptor
|
195
|
+
val bmp: Bitmap = BitmapFactory.decodeFileDescriptor(fDesc)
|
196
|
+
parcelFileDesc.close()
|
197
|
+
image_view.setImageBitmap(bmp)
|
198
|
+
|
199
|
+
} catch(e: IOException) {
|
200
|
+
e.printStackTrace()
|
201
|
+
}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
// BitmapをImageViewから取得する
|
207
|
+
private fun getBitmapFromImageView(view: ImageView): Bitmap {
|
208
|
+
view.getDrawingCache(true)
|
209
|
+
return (view.drawable as BitmapDrawable)?.let { it.bitmap }
|
210
|
+
}
|
211
|
+
|
212
|
+
companion object {
|
213
|
+
private val RESULT_PICK_IMAGEFILE1: Int = 1001
|
214
|
+
private val RESULT_PICK_IMAGEFILE2: Int = 1002
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
28
218
|
```
|
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,11 +17,12 @@
|
|
17
17
|
ActivityMain.ktのimport android.support.v7.app.AppCompatActivityを
|
18
18
|
import androidx.appcompat.app.AppCompatActivityに改変し、
|
19
19
|
|
20
|
-
コピペする前のAndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残したコピペの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
|
21
20
|
|
21
|
+
自身が画像比較アプリをコピペする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
|
22
22
|
|
23
|
-
|
23
|
+
コピペする前のAndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残したコピペの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
|
24
24
|
|
25
|
-
|
25
|
+
Kotlin6 MainActivity.xml
|
26
|
-
|
27
|
-
|
26
|
+
```ここに言語を入力
|
27
|
+
コード
|
28
|
+
```
|