AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Extract Image from Blob

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Extract Image from Blob

Postby Blitzit » Sat Dec 17, 2011 1:34 pm

Hello everybody,
I am developping an application to try clothes :P like a magic mirror..
The problem is that I'm stucked at the part where i need to insert clothes into the system.
The image of the cloth is provided by a shot from a webcam. So, i could use the method to extract the biggest blob, the problem is that the final result should be only the cloth with transparent background.
I've searched a lot and i still didnt' found a way to achieve this: ONLY extract an image, NOT the rectangular area that corresponds to the blob.

A pratical example would be turning this Image

into this: Image

Any help would be appreciated :)
Blitzit
 
Posts: 3
Joined: Sat Dec 17, 2011 1:16 pm

Re: Extract Image from Blob

Postby andrew.kirillov » Sat Dec 17, 2011 3:33 pm

Hello,

You may try using ExtractBlobsImage() and specify original image containing your cloth.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2567
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Extract Image from Blob

Postby Blitzit » Sat Dec 17, 2011 5:19 pm

andrew.kirillov wrote:Hello,

You may try using ExtractBlobsImage() and specify original image containing your cloth.


Thx for the tip! unfortunately, it still didnt' work, my code is the following:
Code: Select all
Bitmap imagem = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);

//Apply treshold to get the biggest blob (the entire cloth)
FiltersSequence commonSeq = new FiltersSequence();
commonSeq.Add(Grayscale.CommonAlgorithms.BT709);
commonSeq.Add(new BradleyLocalThresholding());
commonSeq.Add(new DifferenceEdgeDetector());

Bitmap temp =  commonSeq.Apply(imagem);

BlobCounterBase cbase = new BlobCounter();

cbase.ProcessImage(temp);

Blob[] blobs = cbase.GetObjectsInformation();

//get the biggest blob
int temparea = 0;
int blobindice = 0;
int i=0;
foreach (Blob blob in blobs){
   if (blob.Area > temparea){
      temparea = blob.Area;
      blobindice = i;
   }
   i=i+1;
}

//extracting image from original image but with an blob that comes from the "adulterated" image.
cbase.ExtractBlobsImage(imagem, blobs[blobindice], true);

Bitmap teste1 = new Bitmap(800,600);

teste1 = blobs[blobindice].Image.ToManagedImage();

//transform black pixels into nothing to achieve transparency
teste1.MakeTransparent();

teste1.Save("c:\\result.png",System.Drawing.Imaging.ImageFormat.Png) ;


Original image: Image

The result is the following : Image
without aplying transparency : Image

Can you tell me what I am doing wrong ? :\
Blitzit
 
Posts: 3
Joined: Sat Dec 17, 2011 1:16 pm

Re: Extract Image from Blob

Postby andrew.kirillov » Sat Dec 17, 2011 5:30 pm

When you say the code does not work, what does it really mean? Exception? Error? An image which does not look right? Or?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2567
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Extract Image from Blob

Postby Blitzit » Sat Dec 17, 2011 5:45 pm

andrew.kirillov wrote:When you say the code does not work, what does it really mean? Exception? Error? An image which does not look right? Or?


i say that it doesn't work because the result as you can see in my previous post is not what i pretend. can you see the pictures that i posted?

I found a post where you posted the following:
Postby andrew.kirillov » Sat Aug 13, 2011 7:02 am
Here is the code, which will do what you need (follow documentation if there is something unclear):
Code: Select all
    Bitmap image1 = (Bitmap) Bitmap.FromFile( "d:\\test.jpg" );
    Bitmap temp = Grayscale.CommonAlgorithms.BT709.Apply( image1 );

    Invert invert = new Invert( );
    invert.ApplyInPlace( temp );

    Threshold threshold = new Threshold( );
    threshold.ApplyInPlace( temp );

    BlobCounter blobCounter = new BlobCounter( );
    blobCounter.FilterBlobs = true;
    blobCounter.MinWidth = 80;
    blobCounter.MinWidth = 80;
    blobCounter.ObjectsOrder = ObjectsOrder.Size;
    blobCounter.ProcessImage( temp );

    Blob[] blobs = blobCounter.GetObjectsInformation( );

    if ( blobs.Length > 0 )
    {
        blobCounter.ExtractBlobsImage( temp, blobs[0], true );

        FillHoles fillHoles = new FillHoles( );
        fillHoles.ApplyInPlace( blobs[0].Image );

        GrayscaleToRGB toRgb = new GrayscaleToRGB( );
        UnmanagedImage mask = toRgb.Apply( blobs[0].Image );

        Intersect intersect = new Intersect( );
        intersect.UnmanagedOverlayImage = mask;

        image1 = intersect.Apply( image1 );
    }


And it works like a charm :) , after adding "image1.MakeTransparent();" !
The only small problem now is that some black pixels on cloth where obviously made transparent.. any tip to apply transparency only around the image?
Blitzit
 
Posts: 3
Joined: Sat Dec 17, 2011 1:16 pm

Re: Extract Image from Blob

Postby andrew.kirillov » Sat Dec 17, 2011 6:07 pm

Blitzit wrote:can you see the pictures that i posted?

No, because you did not post any pictures.

Blitzit wrote: any tip to apply transparency only around the image?

Yes, try [url]=http://www.aforgenet.com/framework/docs/html/68bd57bd-1fd6-6c4e-4500-ed4726bc836e.htmfill holes[/url] in the biggest blob. The apply it as a mask to the original image.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 2567
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to AForge.NET Framework