C# Tutorial: How To Create Image Zero Padding


Andraz Krzisnik
C# Tutorial: How To Create Image Zero Padding

Zero padding an image is useful when we’re convolving it with a filter of certain size. How much padding
should we use depends on how big our filter is.

What is the purpose of zero padding?

When we put our kernel at the very edge of our image, some of it will hang off the image. So to prevent any errors we pad the image with zeros around it. The padding thickness is usually the distance from kernel center to its edge.

Obviously this process is not super necessary, because we could just convolve the image without it.
But the end result would yield a border that wouldn’t be processed. This is not noticable if our kernel is 3×3 or 5×5 and our image is of very large size. These example kernels would leave borders of 1 pixel and 2 pixel thick. Noticability of such borders depends also on what kind of kernel or filter are we using.

So to prevent any unwanted borders we use padding (sometimes reffered also as zero padding).
Padding size explained above is reffered to as minimal zero padding.
We can use larger padding of course. Common padding size is twice the each dimensions of the image we are processing (width, height).
Padding an image has its uses, but remember to crop the image when you finish processing it.

Code for padding function

private Bitmap Pad(Bitmap img, int border)
{
    int w = img.Width;
    int h = img.Height;
    int wp = w + 2 * border;
    int hp = h + 2 * border;
    Bitmap ri = new Bitmap(wp, hp);
    BitmapData rd = ri.LockBits(new Rectangle(0, 0, wp, hp),
        ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    BitmapData id = img.LockBits(new Rectangle(0, 0, w, h),
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int imgb = id.Stride * id.Height;
    int borb = rd.Stride * rd.Height;
    byte[] imga = new byte[imgb];
    byte[] bora = new byte[borb];
    for (int i = 3; i < borb; i += 4)
    {
        bora[i] = 255;
    }
    Marshal.Copy(id.Scan0, imga, 0, imgb);
    img.UnlockBits(id);
    for (int y = 0; y < h; y++)
    {
        for (int x = 0; x < w; x++)
        {
            int ip = y * id.Stride + x * 4;
            int rp = y * rd.Stride + x * 4;
            for (int i = 0; i < 3; i++)
            {
                bora[(rd.Stride + 4) * border + rp + i] = imga[ip + i];
            }
        }
    }
    Marshal.Copy(bora, 0, rd.Scan0, borb);
    ri.UnlockBits(rd);
    return ri;
}

This post is acompanied by entire code for this project available for download here.

Download Project

Leave a comment below and let me know what do you think about this

Show Comments (21)

Comments

  • online education programs

    What i do not understood is actually how you are now not really much more well-favored than you might be now. You’re very intelligent. You recognize thus considerably on the subject of this subject, produced me in my view imagine it from a lot of numerous angles. Its like men and women are not interested unless it’s something to do with Lady gaga! Your individual stuffs nice. Always deal with it up!

    • Article Author
  • heart health

    Excellent post. Keep posting such kind of information on your blog. Im really impressed by it.

    • Article Author
  • healthcare

    I think this is among the most significant information for me. And i am glad reading your article. But want to remark on some general things, The site style is perfect, the articles is really nice : D. Good job, cheers

    • Article Author
  • healthcare website

    Hello there, I discovered your web site by way of Google even as looking for a similar subject, your site came up, it looks great. I have bookmarked it in my google bookmarks.

    • Article Author
  • online education

    Quality posts is the important to invite the users to go to see the web page, that’s what this web site is providing.

    • Article Author
  • online education programs

    Right here is the perfect site for everyone who really wants to understand this topic. You realize so much its almost tough to argue with you (not that I really will need to…HaHa). You definitely put a new spin on a subject which has been discussed for a long time. Great stuff, just excellent!

    • Article Author
  • education for all

    What i do not realize is in reality how you’re no longer really much more well-liked than you may be now. You are very intelligent. You know therefore considerably when it comes to this topic, made me in my opinion believe it from so many various angles. Its like men and women aren’t fascinated unless it’s something to do with Girl gaga! Your personal stuffs great. Always handle it up!

    • Article Author
  • masters in education

    Actually no matter if someone doesn’t understand then its up to other users that they will help, so here it takes place.

    • Article Author
  • student loans

    This page definitely has all the information and facts I wanted concerning this subject and didn’t know who to ask.

    • Article Author
  • online degrees

    In fact no matter if someone doesn’t know after that its up to other visitors that they will assist, so here it occurs.

    • Article Author
  • education online courses

    Great article! That is the type of information that are supposed to be shared around the net. Disgrace on Google for no longer positioning this put up upper! Come on over and discuss with my web site . Thanks =)

    • Article Author
  • healthy living

    Do you have a spam issue on this blog; I also am a blogger, and I was curious about your situation; many of us have created some nice practices and we are looking to exchange methods with other folks, please shoot me an email if interested.

    • Article Author
  • health medical news

    My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am worried about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any help would be greatly appreciated!

    • Article Author
  • mental health

    Greate post. Keep posting such kind of information on your page. Im really impressed by it.

    • Article Author
  • free finance advice

    Howdy! This post could not be written any better! Looking at this article reminds me of my previous roommate! He constantly kept talking about this. I am going to forward this post to him. Pretty sure he’s going to have a great read. Many thanks for sharing!

    • Article Author
  • online study

    I’m curious to find out what blog platform you are utilizing?
    I’m experiencing some small security issues with my latest site and I would like to find something more safeguarded.

    Do you have any suggestions?

    • Article Author
  • best financial planning tools

    Very soon this website will be famous among all blogging people, due to it’s good articles

    • Article Author
  • 79Mozelle

    I must say you have very interesting posts here.
    Your page can go viral. You need initial traffic only.
    How to get it? Search for; Miftolo’s tools go viral

    • Article Author
  • Trade Bitcoin

    Appreciation to my father who shared with me on the topic of this blog, this website is
    truly awesome.

    • Article Author
  • Trade Bitcoin

    I enjoy what you guys are up too. This kind of clever work and
    exposure! Keep up the awesome works guys I’ve included you guys to my own blogroll.

    • Article Author
  • my profile

    Hi there would you mind letting me know which webhost you’re using?
    I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most.

    Can you suggest a good hosting provider at a fair price?
    Many thanks, I appreciate it!

    • Article Author

Related Articles

Region Segmentation With K Means Clustering

How To Make K Means Clustering Algorithm With C#

K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.

Posted on by Andraz Krzisnik
Thresholding

How To Make A Basic Global Thresholding Algorithm – C#

This tutorial demonstrates how to get optimal threshold value for basic global thresholding operation for segmentation in image processing.

Posted on by Andraz Krzisnik