0

I'm using body_detection to get the mask of the selfie in order to remove the background image then I'm trying to remove the background using a CustomPainter but not good results.!!

https://pub.dev/packages/body_detection

Original

with the mask

this is the Painter:


import 'package:flutter/widgets.dart';

class PoseMaskPainter extends CustomPainter {
  PoseMaskPainter(
      {required this.mask, required this.imageSize, required this.image});

  final ui.Image? image;
  final ui.Image? mask;
  final Size imageSize;

  final maskPaint = Paint()
    ..colorFilter =
        const ColorFilter.mode(Color.fromRGBO(0, 0, 0, 1), BlendMode.dstIn);

  final maskPaint2 = Paint();

  @override
  void paint(Canvas canvas, Size size) {
    if (mask == null) return;
    drawImage(canvas, size);
    drawMask(canvas, size);
  }

  void drawMask(ui.Canvas canvas, ui.Size size) {
    canvas.drawImageRect(
        mask!,
        Rect.fromLTWH(
            0, 0, mask?.width.toDouble() ?? 0, mask?.height.toDouble() ?? 0),
        Rect.fromLTWH(0, 0, size.width, size.height),
        maskPaint2);
  }

  void drawImage(ui.Canvas canvas, ui.Size size) {
    final imageSize2 =
        Size(image?.width.toDouble() ?? 0, image?.height.toDouble() ?? 0);
    final src = Offset.zero & imageSize2;
    final dst = Offset.zero & size;

    canvas.drawImageRect(image!, src, dst, maskPaint);
  }

  @override
  bool shouldRepaint(PoseMaskPainter oldDelegate) {
    return oldDelegate.mask != mask || oldDelegate.imageSize != imageSize;
  }
}

This is a good approach to remove an image background ? 

Regards! 
tebitoq
  • 165
  • 1
  • 13

0 Answers0