Skip to content

Commit de8e05e

Browse files
committed
checkarea up
1 parent 0eee285 commit de8e05e

File tree

1 file changed

+166
-73
lines changed

1 file changed

+166
-73
lines changed

lib/screens/games/select_the_area/select_the_area_screen.dart

+166-73
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:math';
33

44
import 'package:flutter/foundation.dart';
55
import 'package:flutter/material.dart';
6+
import 'package:flutter/widgets.dart';
67
import 'package:http/http.dart' as http;
78
import 'package:image/image.dart' as img;
89

@@ -37,10 +38,12 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
3738
late Image displayedCmappedMaskImage;
3839

3940
Map<int, int> pixelCount = <int, int>{};
41+
Map<int, int> totalTissuePixelFound = <int, int>{};
4042

4143
User myuser = UserManager.instance.user;
4244

43-
late int indexTissueToFind;
45+
int tissueToFind = 0;
46+
4447
Map<int, String> tissueTypes = <int, String>{
4548
1: 'Carcinoma',
4649
2: 'Necrosis',
@@ -57,19 +60,19 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
5760
void _getPixelsTypeCount() {
5861
for (int x = 0; x < maskImage!.width; x++) {
5962
for (int y = 0; y < maskImage!.height; y++) {
60-
final img.Pixel pixelValue = maskImage!.getPixel(x, y);
61-
pixelCount.update(pixelValue.r as int, (int value) => value + 1,
63+
final pixelValue = maskImage!.getPixel(x, y);
64+
totalTissuePixelFound.update(
65+
pixelValue.r as int, (int value) => value + 1,
6266
ifAbsent: () => 1);
6367
}
6468
}
65-
6669
// Print each unique pixel value
67-
for (final int pixelValue in pixelCount.keys) {
68-
if (kDebugMode) {
69-
print('Total Pixel Value: $pixelValue');
70-
print('Total Pixel Count: ${pixelCount[pixelValue]}');
71-
}
72-
}
70+
// for (final int pixelValue in totalTissuePixelFound.keys) {
71+
// if (kDebugMode) {
72+
// print('Total Pixel Value: $pixelValue');
73+
// print('Total Pixel Count: ${totalTissuePixelFound[pixelValue]}');
74+
// }
75+
// }
7376
}
7477

7578
void _getTissueToFind() {
@@ -79,13 +82,13 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
7982
// 4: (0, 255, 255), # Others
8083

8184
// Select randomly one of the keys in pixelCount exept 0
82-
final List<int> pixelValues = pixelCount.keys.toList();
85+
final List<int> pixelValues = totalTissuePixelFound.keys.toList();
8386
pixelValues.remove(0);
84-
indexTissueToFind = pixelValues[Random().nextInt(pixelValues.length)];
87+
tissueToFind = pixelValues[Random().nextInt(pixelValues.length)];
8588

8689
if (kDebugMode) {
87-
final String tissueName = tissueTypes[indexTissueToFind]!;
88-
print('Tissue Index to Find: $indexTissueToFind');
90+
final String tissueName = tissueTypes[tissueToFind]!;
91+
print('Tissue Index to Find: $tissueToFind;');
8992
print('Tissue to Find: $tissueName');
9093
}
9194
}
@@ -130,9 +133,10 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
130133
_getPixelsTypeCount();
131134

132135
// Check if 0 is the only pixel value
133-
if (pixelCount.length == 1 && pixelCount.containsKey(0)) {
136+
if (totalTissuePixelFound.length == 1 &&
137+
totalTissuePixelFound.containsKey(0)) {
134138
if (kDebugMode) {
135-
print('Only Background Pixels');
139+
print('Only Unknown Class Pixels');
136140
}
137141
continue;
138142
}
@@ -195,15 +199,15 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
195199
2) *
196200
scalingFactorX; // Assuming uniform scaling
197201

198-
if (kDebugMode) {
199-
print('Center: ($centerX, $centerY), Radius: $radius');
200-
print('Image Size: ${fullImage!.width} x ${fullImage!.height}');
201-
print('Mask Size: ${maskImage!.width} x ${maskImage!.height}');
202-
print(
203-
'Rendered cmapped Mask Size: ${displayedCmappedMaskImage.width} x ${displayedCmappedMaskImage.height}');
204-
print(
205-
'Rendered Image Size: ${displayedFullImage.width} x ${displayedFullImage.height}');
206-
}
202+
// if (kDebugMode) {
203+
// print('Center: ($centerX, $centerY), Radius: $radius');
204+
// print('Image Size: ${fullImage!.width} x ${fullImage!.height}');
205+
// print('Mask Size: ${maskImage!.width} x ${maskImage!.height}');
206+
// print(
207+
// 'Rendered cmapped Mask Size: ${displayedCmappedMaskImage.width} x ${displayedCmappedMaskImage.height}');
208+
// print(
209+
// 'Rendered Image Size: ${displayedFullImage.width} x ${displayedFullImage.height}');
210+
// }
207211

208212
final Set<int> uniquePixelValues = <int>{};
209213
final Map<int, int> pixelCount = <int, int>{};
@@ -228,13 +232,54 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
228232
}
229233
}
230234

231-
// Print each unique pixel value
232-
for (final int pixelValue in uniquePixelValues) {
235+
// if tissueToFind is in pixelCount
236+
if (pixelCount.containsKey(tissueToFind)) {
237+
// sum the total number of pixels in image
238+
double totalCoveredPixels = 0;
239+
for (final int pixelValue in pixelCount.keys) {
240+
totalCoveredPixels += pixelCount[pixelValue]!;
241+
}
242+
243+
// if selected pixels cover the whole image area
244+
final double coveredArea =
245+
totalCoveredPixels / (maskImage!.width * maskImage!.height);
246+
247+
if (coveredArea > 0.7) {
248+
if (kDebugMode) {
249+
print(
250+
'Devi selezionare solo la parte dell\'immagine in cui è presente il tessuto');
251+
return;
252+
}
253+
}
254+
255+
// if number of pixel of correct tissue is less than 50% of the total correct tissue pixels
256+
if (pixelCount[tissueToFind]! <
257+
0.5 * totalTissuePixelFound[tissueToFind]!) {
258+
print('Non hai individuato tutto il tessuto corretto');
259+
return;
260+
}
261+
233262
if (kDebugMode) {
234-
print('Pixel Value in selected Area: $pixelValue');
235-
print('Pixel Count in selected Area: ${pixelCount[pixelValue]}');
263+
print('Tissue Name: ${tissueTypes[tissueToFind]}');
264+
print('Total Pixel Count: ${totalTissuePixelFound[tissueToFind]}');
265+
print('Pixel Count in selected Area: ${pixelCount[tissueToFind]}');
266+
print('Covered Area: $coveredArea');
267+
print('Total Area: ${maskImage!.width * maskImage!.height}');
268+
print('Covered Pixels: $totalCoveredPixels');
236269
}
270+
271+
print('Hai individuato il tessuto corretto');
272+
} else {
273+
print('Non hai individuato il tessuto corretto');
237274
}
275+
276+
// Print each unique pixel value
277+
// for (final int pixelValue in uniquePixelValues) {
278+
// if (kDebugMode) {
279+
// print('Pixel Value in selected Area: $pixelValue');
280+
// print('Pixel Count in selected Area: ${pixelCount[pixelValue]}');
281+
// }
282+
// }
238283
}
239284

240285
void checkAnswer() {
@@ -247,7 +292,7 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
247292
appBar: AppBar(
248293
title: const Text('Circle Area Comparison'),
249294
),
250-
body: imageBytes.isEmpty
295+
body: imageBytes.isEmpty || tissueToFind == 0
251296
? const Center(child: CircularProgressIndicator())
252297
: Column(
253298
children: <Widget>[
@@ -261,7 +306,7 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
261306
),
262307
children: <TextSpan>[
263308
TextSpan(
264-
text: tissueTypes[indexTissueToFind],
309+
text: tissueTypes[tissueToFind],
265310
style:
266311
const TextStyle(fontWeight: FontWeight.bold)),
267312
const TextSpan(text: ' Tissue!'),
@@ -272,7 +317,7 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
272317
Row(
273318
children: <Widget>[
274319
Padding(
275-
padding: const EdgeInsets.only(left: 20, right: 20),
320+
padding: const EdgeInsets.only(left: 50, right: 20),
276321
child: Center(
277322
child: ClipRect(
278323
child: FittedBox(
@@ -303,56 +348,104 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
303348
),
304349
),
305350
),
351+
Column(
352+
mainAxisAlignment: MainAxisAlignment.spaceAround,
353+
children: <Widget>[
354+
const Row(
355+
children: <Widget>[
356+
Align(
357+
alignment: Alignment.topLeft,
358+
child: SizedBox(
359+
height: 200,
360+
width: 550,
361+
child: Text(
362+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
363+
'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. '
364+
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. '
365+
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. '
366+
'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
367+
style: TextStyle(
368+
color: Colors.black,
369+
fontSize: 16,
370+
overflow: TextOverflow.visible,
371+
),
372+
maxLines: 20,
373+
),
374+
),
375+
),
376+
],
377+
),
378+
const SizedBox(height: 30),
379+
Row(
380+
children: <Widget>[
381+
if (_isVisible)
382+
Column(
383+
children: <Widget>[
384+
Padding(
385+
padding: const EdgeInsets.only(left: 20),
386+
child: RichText(
387+
text: TextSpan(
388+
text: 'Image visibility: ',
389+
style: DefaultTextStyle.of(context)
390+
.style
391+
.apply(
392+
fontSizeFactor: 1.5,
393+
),
394+
children: <TextSpan>[
395+
TextSpan(
396+
text: imageVisibility
397+
.toStringAsFixed(2),
398+
style: const TextStyle(
399+
fontWeight: FontWeight.bold)),
400+
],
401+
),
402+
),
403+
),
404+
const SizedBox(height: 10),
405+
Slider(
406+
value: imageVisibility,
407+
onChanged: (double value) {
408+
setState(() {
409+
imageVisibility = value;
410+
});
411+
},
412+
),
413+
const SizedBox(width: 30),
414+
const Text('legenda')
415+
],
416+
),
417+
],
418+
)
419+
],
420+
)
306421
],
307422
),
308423
const SizedBox(height: 30),
309424
// Display bottom left button to confirm the selection
310425
Row(
311426
children: <Widget>[
312-
if (_isVisible)
313-
Column(
314-
children: <Widget>[
315-
Text(
316-
'Image Visibility',
317-
style: DefaultTextStyle.of(context).style.apply(
318-
fontSizeFactor: 1.5,
319-
),
320-
),
321-
const SizedBox(height: 10),
322-
Slider(
323-
value: imageVisibility,
324-
onChanged: (double value) {
325-
setState(() {
326-
imageVisibility = value;
327-
});
328-
},
329-
),
330-
Text(
331-
imageVisibility.toStringAsFixed(2),
332-
style: DefaultTextStyle.of(context)
333-
.style
334-
.apply(fontSizeFactor: 1.5),
335-
),
336-
],
337-
),
338427
const Spacer(),
339428
Align(
340429
alignment: Alignment.bottomRight,
341-
child: ElevatedButton(
342-
onPressed: _isEnabled
343-
? () {
344-
setState(() {
345-
if (!_isVisible &&
346-
_startPoint != null &&
347-
_endPoint != null &&
348-
_isDrawing) {
349-
_isVisible = true;
350-
checkAnswer();
351-
}
352-
});
353-
}
354-
: null,
355-
child: const Text('Confirm Selection'),
430+
child: SizedBox(
431+
height: 50,
432+
width: 200,
433+
child: FilledButton(
434+
onPressed: _isEnabled
435+
? () {
436+
setState(() {
437+
if (!_isVisible &&
438+
_startPoint != null &&
439+
_endPoint != null &&
440+
_isDrawing) {
441+
_isVisible = true;
442+
checkAnswer();
443+
}
444+
});
445+
}
446+
: null,
447+
child: const Text('Confirm Selection'),
448+
),
356449
),
357450
),
358451
],

0 commit comments

Comments
 (0)