質問編集履歴

5

2021/05/14 06:11

投稿

2r4h8k3m
2r4h8k3m

スコア22

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
 
20
20
 
21
- 以前は掲載のソースファイルにそのままコピペし、確かに同じエラーは出ていたのですが、
21
+ 以前は掲載のソースファイルにそのまま貼り付け、確かに同じエラーは出ていたのですが、
22
22
 
23
23
  以前AndroidXが関係しているとの回答を頂きましたので以下のURL参考にして
24
24
 
@@ -38,11 +38,11 @@
38
38
 
39
39
 
40
40
 
41
- 自身が画像比較アプリをコピペする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
41
+ 自身が画像比較アプリを貼り付けるする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
42
-
43
-
44
-
42
+
43
+
44
+
45
- コピペする前AndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残したコピペの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
45
+ 貼り付ける前AndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残した貼り付けの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
46
46
 
47
47
 
48
48
 

4

2021/05/14 06:11

投稿

2r4h8k3m
2r4h8k3m

スコア22

test CHANGED
File without changes
test CHANGED
@@ -433,3 +433,11 @@
433
433
 
434
434
 
435
435
  ```
436
+
437
+
438
+
439
+ 文字数最大数行きましたので、Android Studioでの画像比較サンプルアプリ実行エラー 2の方にKotlin6 activity_main.xml、TestApp001のMainActivity.Javaとactivity_main.xmlのソース掲載しております。
440
+
441
+
442
+
443
+ 時間がある方いらっしゃればご回答お願い致します。

3

2021/05/13 06:44

投稿

2r4h8k3m
2r4h8k3m

スコア22

test CHANGED
@@ -1 +1 @@
1
- Android Studioでのサンプルアプリ実行エラー
1
+ Android Studioでの画像比較サンプルアプリ実行エラー
test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
 
48
48
 
49
- Kotlin6 MainActivity.xml
49
+ Kotlin6 MainActivity.xml(自分で記載のURLよりソース改変)
50
50
 
51
51
  ```ここに言語を入力
52
52
 

2

2021/05/13 06:36

投稿

2r4h8k3m
2r4h8k3m

スコア22

test CHANGED
File without changes
test CHANGED
@@ -50,6 +50,386 @@
50
50
 
51
51
  ```ここに言語を入力
52
52
 
53
+
54
+
55
+ package com.example.kotlin6;
56
+
57
+
58
+
59
+ import androidx.appcompat.app.AppCompatActivity;
60
+
61
+
62
+
63
+ import android.os.Bundle;
64
+
65
+
66
+
67
+ import android.app.Activity
68
+
69
+ import android.content.Intent
70
+
71
+ import android.graphics.Bitmap
72
+
73
+ import android.graphics.BitmapFactory
74
+
75
+ import android.graphics.drawable.BitmapDrawable
76
+
77
+ import android.net.Uri
78
+
79
+ import androidx.appcompat.app.AppCompatActivity
80
+
81
+ import android.os.Bundle
82
+
83
+ import android.os.ParcelFileDescriptor
84
+
85
+ import android.util.Log
86
+
87
+ import android.widget.ImageView
88
+
89
+ import kotlinx.android.synthetic.main.activity_main.*
90
+
91
+ import org.opencv.android.OpenCVLoader
92
+
93
+ import org.opencv.android.Utils
94
+
95
+ import org.opencv.core.*
96
+
97
+ import org.opencv.features2d.AKAZE
98
+
99
+ import org.opencv.features2d.DescriptorMatcher
100
+
101
+ import org.opencv.features2d.Features2d
102
+
103
+ import org.opencv.imgproc.Imgproc
104
+
105
+ import java.io.FileDescriptor
106
+
107
+ import java.io.IOException
108
+
109
+
110
+
111
+
112
+
113
+ public class MainActivity extends AppCompatActivity {
114
+
115
+
116
+
117
+ @Override
118
+
119
+ protected void onCreate(Bundle savedInstanceState) {
120
+
121
+ super.onCreate(savedInstanceState);
122
+
123
+ setContentView(R.layout.activity_main);
124
+
125
+
126
+
127
+ if(!OpenCVLoader.initDebug()) {
128
+
129
+ Log.d("OpenCV", "error_openCV")
130
+
131
+ }
132
+
133
+
134
+
135
+ // 画像選択ボタン1のリスナー
136
+
137
+ select_img1_btn.setOnClickListener {
138
+
139
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
140
+
141
+ intent.addCategory(Intent.CATEGORY_OPENABLE)
142
+
143
+ intent.setType("*/*")
144
+
145
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE1)
146
+
147
+ }
148
+
149
+
150
+
151
+ // 画像選択ボタン2のリスナー
152
+
153
+ select_img2_btn.setOnClickListener {
154
+
155
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
156
+
157
+ intent.addCategory(Intent.CATEGORY_OPENABLE)
158
+
159
+ intent.setType("*/*")
160
+
161
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE2)
162
+
163
+ }
164
+
165
+
166
+
167
+ // 決定ボタンのリスナー
168
+
169
+ decition_btn.setOnClickListener {
170
+
53
- コード
171
+ try {
172
+
173
+ // src_img1の画像をMatに
174
+
175
+ var bitmap: Bitmap = Bitmap.createScaledBitmap(getBitmapFromImageView(src_img1), 640, 480, false)
176
+
177
+ val scene1 = Mat(bitmap!!.height, bitmap!!.width, CvType.CV_8UC1).apply { Utils.bitmapToMat(bitmap, this) }
178
+
179
+
180
+
181
+ // src_img2の画像をMatに
182
+
183
+ bitmap = Bitmap.createScaledBitmap(getBitmapFromImageView(src_img2), 640, 480, false)
184
+
185
+ val scene2 = Mat(bitmap!!.height, bitmap!!.width, CvType.CV_8UC1).apply { Utils.bitmapToMat(bitmap, this) }
186
+
187
+
188
+
189
+ // アルゴリズムはAKZEで
190
+
191
+ val algorithm: AKAZE = AKAZE.create()
192
+
193
+
194
+
195
+ // 特徴点抽出
196
+
197
+ val keypoint1 = MatOfKeyPoint().apply { algorithm.detect(scene1, this) }
198
+
199
+ val keypoint2 = MatOfKeyPoint().apply { algorithm.detect(scene2, this) }
200
+
201
+
202
+
203
+ // 特徴量記述
204
+
205
+ val descriptor1 = Mat().apply { algorithm.compute(scene1, keypoint1, this) }
206
+
207
+ val descriptor2 = Mat().apply { algorithm.compute(scene2, keypoint2, this) }
208
+
209
+
210
+
211
+ // マッチング (アルゴリズムにはBruteForceを使用)
212
+
213
+ val matcher = DescriptorMatcher.create("BruteForce")
214
+
215
+
216
+
217
+ var matches_list: MutableList<DMatch> = mutableListOf()
218
+
219
+ val match12 = MatOfDMatch().apply { matcher.match(descriptor1, descriptor2, this) }
220
+
221
+ val match21 = MatOfDMatch().apply { matcher.match(descriptor2, descriptor1, this) }
222
+
223
+
224
+
225
+ // クロスチェック(1→2と2→1の両方でマッチしたものだけを残して精度を高める)
226
+
227
+ val size: Int = match12.toArray().size - 1
228
+
229
+ val match12_array = match12.toArray()
230
+
231
+ val match21_array = match21.toArray()
232
+
233
+ var count: Int = 0
234
+
235
+ for(i in 0..size) {
236
+
237
+ val forward: DMatch =match12_array[i]
238
+
239
+ val backward: DMatch = match21_array[forward.trainIdx]
240
+
241
+ if(backward.trainIdx == forward.queryIdx) {
242
+
243
+ matches_list.add(forward)
244
+
245
+ count++
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ val matches = MatOfDMatch().apply { this.fromList(matches_list) }
254
+
255
+
256
+
257
+ // 結果画像の背景真っ黒になるのを防ぐ
258
+
259
+ val scene1rgb = Mat().apply { Imgproc.cvtColor(scene1, this, Imgproc.COLOR_RGBA2RGB, 1) }
260
+
261
+ val scene2rgb = Mat().apply { Imgproc.cvtColor(scene2, this, Imgproc.COLOR_RGBA2RGB, 1) }
262
+
263
+
264
+
265
+ // マッチ結果を出力
266
+
267
+ val dest = scene1.clone().apply {
268
+
269
+ Features2d.drawMatches(scene1rgb, keypoint1, scene2rgb, keypoint2, matches, this)
270
+
271
+ }
272
+
273
+
274
+
275
+ val result_btm: Bitmap = Bitmap.createBitmap(dest.cols(), dest.rows(), Bitmap.Config.ARGB_8888)
276
+
277
+ .apply { Utils.matToBitmap(dest, this) }
278
+
279
+
280
+
281
+ // マッチング結果画像の出力
282
+
283
+ result_img.setImageBitmap(result_btm)
284
+
285
+
286
+
287
+ // マッチング数を出力
288
+
289
+ count_txt.text = "マッチング数: ${count}"
290
+
291
+
292
+
293
+ } catch(e: NullPointerException) {
294
+
295
+ e.printStackTrace()
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ }
304
+
305
+
306
+
307
+ // 画像を選択したときの動き
308
+
309
+ override fun onActivityResult(requestCode: Int, resultCode: Int, resultdata: Intent?) {
310
+
311
+ if((requestCode == RESULT_PICK_IMAGEFILE1 || requestCode == RESULT_PICK_IMAGEFILE2)
312
+
313
+ && resultCode == Activity.RESULT_OK) {
314
+
315
+ val image_view: ImageView =
316
+
317
+ if(requestCode == RESULT_PICK_IMAGEFILE1) src_img1
318
+
319
+ else src_img2
320
+
321
+
322
+
323
+ if(resultdata?.data != null) {
324
+
325
+ try {
326
+
327
+ val uri: Uri = resultdata.data
328
+
329
+ val parcelFileDesc: ParcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r")
330
+
331
+
332
+
333
+ val fDesc: FileDescriptor = parcelFileDesc.fileDescriptor
334
+
335
+ val bmp: Bitmap = BitmapFactory.decodeFileDescriptor(fDesc)
336
+
337
+ parcelFileDesc.close()
338
+
339
+ image_view.setImageBitmap(bmp)
340
+
341
+
342
+
343
+ } catch(e: IOException) {
344
+
345
+ e.printStackTrace()
346
+
347
+ }
348
+
349
+ }
350
+
351
+
352
+
353
+ }
354
+
355
+ }
356
+
357
+
358
+
359
+
360
+
361
+ // 画像を選択したときの動き
362
+
363
+ override fun onActivityResult(requestCode: Int, resultCode: Int, resultdata: Intent?) {
364
+
365
+ if((requestCode == RESULT_PICK_IMAGEFILE1 || requestCode == RESULT_PICK_IMAGEFILE2)
366
+
367
+ && resultCode == Activity.RESULT_OK) {
368
+
369
+ val image_view: ImageView =
370
+
371
+ if(requestCode == RESULT_PICK_IMAGEFILE1) src_img1
372
+
373
+ else src_img2
374
+
375
+
376
+
377
+ if(resultdata?.data != null) {
378
+
379
+ try {
380
+
381
+ val uri: Uri = resultdata.data
382
+
383
+ val parcelFileDesc: ParcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r")
384
+
385
+
386
+
387
+ val fDesc: FileDescriptor = parcelFileDesc.fileDescriptor
388
+
389
+ val bmp: Bitmap = BitmapFactory.decodeFileDescriptor(fDesc)
390
+
391
+ parcelFileDesc.close()
392
+
393
+ image_view.setImageBitmap(bmp)
394
+
395
+
396
+
397
+ } catch(e: IOException) {
398
+
399
+ e.printStackTrace()
400
+
401
+ }
402
+
403
+ }
404
+
405
+ }
406
+
407
+ }
408
+
409
+
410
+
411
+ // BitmapをImageViewから取得する
412
+
413
+ private fun getBitmapFromImageView(view: ImageView): Bitmap {
414
+
415
+ view.getDrawingCache(true)
416
+
417
+ return (view.drawable as BitmapDrawable)?.let { it.bitmap }
418
+
419
+ }
420
+
421
+
422
+
423
+ companion object {
424
+
425
+ private val RESULT_PICK_IMAGEFILE1: Int = 1001
426
+
427
+ private val RESULT_PICK_IMAGEFILE2: Int = 1002
428
+
429
+ }
430
+
431
+ }
432
+
433
+
54
434
 
55
435
  ```

1

2021/05/13 06:32

投稿

2r4h8k3m
2r4h8k3m

スコア22

test CHANGED
File without changes
test CHANGED
@@ -36,18 +36,20 @@
36
36
 
37
37
 
38
38
 
39
+
40
+
41
+ 自身が画像比較アプリをコピペする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
42
+
43
+
44
+
39
45
  コピペする前のAndroidX対応になっていると予測したTestAppアプリの書き方を出来るだけ残したコピペの仕方でKotlin6を作成しました。ですがおなじエラーがでます。
40
46
 
41
47
 
42
48
 
49
+ Kotlin6 MainActivity.xml
43
50
 
51
+ ```ここに言語を入力
44
52
 
45
- 自身が画像比較アプリをピペする前に作ったアプリはAndroidX対応のなっており、比較アプリは古い文献でAndroidX対応になっていないのだと自分なりに解釈し、
53
+ ード
46
54
 
47
-
48
-
49
-
50
-
51
-
52
-
53
- 以下のサイトで
55
+ ```