C# Tutorial: How To Scale Images Without Deforming Them
Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...
Filter by Category
Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...
List of contents: Short intro First Method – Google Opinion Rewards Second Method – Taking & Fortifying Gyms Third Method – Answer a Few Questions List of...
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...
Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen...
A negative image is a complete inversion of an image, we would say to be normal. In other words, dark areas will appear light, and light areas will become dark. More in detail, a...
Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...
Erosion is a morphological process, where pixels at an object boundaries are removed. Erosion and dilation are a pair of basic morphological transformations, which are completely...
Dilation is a simple morphology process which changes pixel intensities based on the change of intensities that occur at object boundaries. This process is used on grayscale...
What is sobel operator? Well, basically it’s 2 kernels, with which we can process an image in a way, that only edges are visible. It is commonly used for grayscale images,...
Contrast stretch or otherwise known as normalization is a process where your image’s intensity is changed in such a way, that dark pixels become darker and light pixels...
A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary size.
This tutorial will show you, how you can take images of different sizes and process them in a way, they all come out with a desired size, without stretching them. The no stretching part will be done by adding padding to a smaller dimension, width or height.
This process can prove very useful when preparing training, testing and validating data for a convolutional neural network. Padding added to the image hold 0 for all color channels or in other words it’s black. Image being processed will always be placed in the middle, regardless if the padding will be added horizontally or vertically.
This project is related to a Zero Padding Project, which adds a border of desired thickness all around the image.
Let’s take a look at the function that will be doing the heavy lifting for us.
private Bitmap Resize(Bitmap img, int dim) { img = ScaleImage(img, dim, dim); int w = img.Width; int h = img.Height; int p = (dim - Math.Min(w, h)) / 2; int padw = (w < h) ? p : 0; int padh = (h < w) ? p : 0; BitmapData sd = img.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int bytes = sd.Stride * sd.Height; byte[] buffer = new byte[bytes]; Marshal.Copy(sd.Scan0, buffer, 0, bytes); img.UnlockBits(sd); Bitmap resimg = new Bitmap(dim, dim); BitmapData rd = resimg.LockBits(new Rectangle(0, 0, dim, dim), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int resbytes = rd.Stride * rd.Height; byte[] result = new byte[resbytes]; Array.Clear(result, 0, resbytes); int rcurrent,bcurrent = 0; for (int y = padh; y < h + padh; y++) { for (int x = padw; x < w + padw; x++) { rcurrent = y * rd.Stride + x * 4; bcurrent = (y - padh) * sd.Stride + (x - padw) * 4; for (int i = 0; i < 3; i++) { result[rcurrent + i] = buffer[bcurrent + i]; } result[rcurrent + 3] = 255; } } Marshal.Copy(result, 0, rd.Scan0, resbytes); resimg.UnlockBits(rd); return resimg; }
As you can see this is a function that takes 2 arguments, one for the image we are processing and the other for a dimension of which will be the new width and height. Since this was purposefully made for further processing with convolution, both dimensions are of the same size.