Share via


Image processing: How to remove background from an image

Question

Monday, October 21, 2019 3:20 PM

I am working on image processing. The work is to remove background from an image. I read lots of notes online, made several researches but all to no avail. I use c# and vb. please help me with the flow/algorithm to achieve this. This has been on me for close to 2months now.

All replies (3)

Monday, October 21, 2019 4:21 PM

Have you tried Bitmap.MakeTransparent ?

See also

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2f76a549-d4bb-49e0-aa90-a8b15393fc01/maketransparent-does-not-delete-all-background-of-my-image?forum=csharpgeneral

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Wednesday, October 23, 2019 10:15 AM

Hi,

Use MakeTransparent, you can achieve simple image background deletion, but when the background color is complex, it is recommended to use PhotoShop mentioned above.

Since the image processing algorithm relies on a large amount of training or basic data, it is not a good idea for some results to use a static algorithm to complete the long training independently for each application. Therefore, many similar results will be provided as an api service interface. Of course, third-party services may require payment, but generally have a certain amount of free. 

Best Regards,

Julie

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Wednesday, October 23, 2019 11:18 AM

Use SetColorKey as shown below to set a range of color to make transparent in the image. In the example I have removed all the background from the image in Karen's link above.

One can remove colors from two passes of the colorkey ie range of black-gray and range of smoke to white.

There are other ways to remove single colors as well and if you define a fence around the image colors you can do that and etc.

See how all the high gray to white color range was removed from the original dithered image they said could not be done?

Public Class Form5
    Private srcBmp As New Bitmap("c:\bitmaps\a.jpg")

    Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BackColor = Color.White
    End Sub

    Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim destRect As New Rectangle(10, 10, srcBmp.Width, srcBmp.Height)
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.DrawImage(srcBmp, Rectangle.Round(destRect))
        e.Graphics.DrawString("Original", Font, Brushes.Black, 40, 210)

        Dim attribs As New System.Drawing.Imaging.ImageAttributes
        attribs.SetColorKey(Color.FromArgb(200, 200, 200), Color.FromArgb(255, 255, 255))

        'draw pic with color filter
        destRect.X = 10
        destRect.Y = 240
        e.Graphics.DrawImage(srcBmp, Rectangle.Round(destRect), 0, 0, srcBmp.Width, srcBmp.Height, GraphicsUnit.Pixel, attribs)
        e.Graphics.DrawString("Filtered Duplicate", Font, Brushes.Black, 40, 430)

    End Sub
End Class

PS Here is the filtered transparent image drawn on a black form background. You can see the accuracy between the book pages.