← Previous lessonFree lesson 3 of 6 · lesson 3 of 48Next lesson →

Unlock all 48 lessons of Advanced Medical Image Processing in Clinical ApplicationsFull course ₹40,000 · paid

Price & syllabus
Free lesson · 22 min · no payment

Sampling, Quantization and Reading Images in Python

A free lesson from Skill-Lync's Advanced Medical Image Processing in Clinical Applications course, on Grow by Skill-Lync · Last updated 16 Nov 2026 · Session: “Module 1 Session 3”

What will you learn in this free Advanced Medical Image Processing in Clinical Applications lesson?

The lesson explains how analog images become digital through sampling and quantization, including sampling pitch, aliasing and the Nyquist frequency. It covers 8, 10, 12 and 14-bit depth, then reads, converts and saves images in Python using OpenCV and NumPy.

  • Sampling the grid and quantizing amplitude levels
  • Aliasing and the Nyquist frequency
  • Bit depth: 256 levels at 8 bit and higher
  • Reading and saving images as NumPy arrays with OpenCV

What comes after lesson 3?

The next free lesson is lesson 23, “Medical Image Segmentation: Motivation and Clinical Applications” (18 min). Later in Skill-Lync's Advanced Medical Image Processing in Clinical Applications course (paid): lesson 4, Module 1 Session 4; lesson 5, Module 2 Session 1; lesson 6, Module 2 Session 2. The full course has 48 lessons (21 h 26 min of video); see the course overview or its price and syllabus.

Lesson transcript

Machine transcript of the lesson audio (lightly cleaned). Technical terms may contain errors; the video is the reference.

Welcome back to session 3 of week 1 of medical image processing. And to give a brief recap of what we have done, we have seen what are the various steps involved in image processing right from acquisition to pre-processing to enhancement to compression to morphological image operations all predominantly coming under image processing part. And we also saw aspects about advanced image processing, bordering to computer vision of how to segment an image and how to compute metrics on an image, how to do object detection on an image, and so on.

So those come under the advanced image processing part. We then also saw aspects about how an image gets formed, what is an image processing system, what are the aspects which are involved in a system, and how they all orchestrate together in getting an image understood and analyzed. We have briefly alluded multiple times to image sampling and quantization. And to understand image processing better, it is integral to understand sampling and quantization in a little more level of detail. And we now get started with what is sampling and quantization and what is quantization and look at some real examples to look like how sampling impacts the image formation process and how quantization impacts the image representation per se.

So I think we remember from our earlier slides that to create a digital image, we need to convert continuous analog data into digital form. And this requires two processes or two steps. One is the grid on which the image is going to be formed should be finalized. What is the granularity in which we represent that XY grid? The X is the X axis, the Y is the Y axis, and that forms the step called sampling. And so this sampling is about digitizing the coordinate values, the X coordinate, the Y coordinate.

Do you want to have 100 units in the X coordinate and 100 in the Y coordinate? Do you want to have 500 in the X coordinate and 5 in the Y coordinate? These decisions are taken by the sampling step. The subsequent step is the quantization part, is that the amplitude, how fine or coarse do you want to represent? So do you want to have the amplitude values having range between 0 to 256? Or can the range go from 0 to 4096? Or do you want to go from 0 to 8000?

Now why are there specific values like either 4000 or 8000 or 512 or 256? These are because they are represented in powers of 2 and that is generally how quantization is done. More details on it when we get to the quantization slide. So let's look at sampling and see how an image, when it is being formed, how is the sampling impacting the representation of an image? So digitizing a 2D image with respect to the X and Y coordinates, deciding how many units are there in X and Y coordinates is sampling process.

The extent to which you sample or the rate in which you sample is called the sampling frequency. So here you see an image like a small bunny like structure and we have representation of a line AB passing through that image. Now if you sample it at all those locations which are represented as red, you do a readout at those locations. And so that many samples in that line we would readout. Now if you can imagine if you readout only 10 samples, you would only get a core structure of what that image is because you only get 10 readouts.

But if you do 100 readouts, you get much more precise. But if you do 1000 readouts, maybe you don't need so much information. So there has to be a trade-off how much do you need to readout in the X direction. And the same is similarly applicable for the Y direction. So AV is a line along the continuous image. And the sampling along that cross section is decided by the sampling part to say where all do you need to sample, which I am highlighting by my house, by my mouse.

And then the amplitude values in those regions are all represented by the red, by the green regions. And that then is the quantization part. So sampling is determined by the sensor arrangement. How coarsely you put your sensors or how finely you put your sensors. Because if you have sensors packed tightly, then you can readout at multiple close locations. So sampling process, if you detail it in a little more methodological way, you have your X and Y represented in a grid format. And your sampling entails how many dots you put in this X and Y grid.

Right. So here you see 0, 0, and then you see some black dots. Each of these dot is one of the sampling region. And at that location, your electromagnetic spectrum or image, your F of X, Y, will then be quantized or represented in those locations. So sampling says that X direction will have 0 to M minus 1 locations in which the image will be represented. And in Y direction, 0 to N minus 1. So you have kind of a matrix grid representation determined by the sampling process.

So samples are usually represented in Cartesian coordinates. Cartesian coordinates is your X and Y coordinate system. But it is not only limited to Cartesian coordinates. One could do other coordinate systems, like one can have an arc coordinate system and so on. But those get into complex representation of an image and complex sampling. This may become relevant as you go into advanced image processing concepts. So the representation in a coordinate, in a Cartesian coordinate system with sampling in place, leads to an image being represented in a matrix format, like represented in the diagram in the left.

So your image F of X, Y, would be represented at F of 0, 0, F of 0, 1, up to F of 0, N minus 1, 0 being the 0th row, N minus 1 being the N minus 1th column. And then you start with the row 1, 1, 0, 1, 1, going up to 1, N minus 1. And final row would be F of M minus 1, all the way to F of M minus 1, N minus 1. So I hope it is clear why an image gets in the end represented like a matrix format, because of the inherent sampling process.

Once you sample, you also need to then convert the electromagnetic values into quantized or bucketed representations. And that we get into the sampling, into the quantization part. So this digitization is digitization of coordinate values, x-axis and y-axis. X-axis, well, here it is represented for a 2D signal. If you have, like a signal, if you have x as time and y as amplitude, then you get a digitized signal per se. But in an image, you have x and y as two axes, and then F of x, y is the amplitude that you then digitize.

There is a concept of aliasing, and aliasing is an artifact or a noise which gets introduced into the data, image data. And one of the requirements which comes from signal processing domain is that you shouldn't have sampling frequency less than your Nyquist frequency. If this is done, this leads to aliasing. And for those who want to recap on Nyquist frequency, Nyquist is two times maximum frequency. When you digitize on the x and y coordinates, you should have a fair sense of what the maximum frequency of your signal is, and then have the sampling frequency at least higher than the Nyquist frequency.

So, we see examples of image sampling, and here we have a signal, which is like a sinusoidal signal. You have your sampling pitch. You sample at each of these locations, and the gap between that is the sampling pitch. Now, the aperture width is how much gap you have between each of your sensing elements, right? So, if you have two cores sampling, then you would have effect cost like what you see here, right? The image could lead to a blurry representation of that A that you want to represent, and this could lead to what is called as image aliasing.

So, if sampling is done, much higher frequency rate, your image would be represent far more accurately. Now, we come to the second digitization aspect, which is about the quantization. And quantization is the... Representation on the y-axis, right? How cores or fine do you want to bucketize your amplitude values? Amplitude is your f of xy, right? Your intensity into reflectance, illumination into reflectance that we saw how an image is represented. How do you want to break it down into individual component, right? So analogs to digital converters are used to convert this amplitude into digital formats.

We said in the earlier slide for your quantization, for your sampling, it is dependent on your sensor placements. And here for your quantization, it is dependent on your ADC, analog to digital converter. So you can use an 8-bit image, which then says we have 256 levels of representation, or you can use a 10 or a 12 or a 14-bit image. And each of this would mean that you have 2 power 10, 2 power 12, or 2 power 14 levels of representation of your amplitude. The larger it is, the larger it is, the better the image representation. The coarser it is, the poorer the image representation, but the size will be less. So it depends on your choice. Do you want a large size image? Then you need to go for a larger quantization.

But you want a very small size image? Then you need to go for a 8-bit representation or something lesser than that. So the quality of an image depends on the quantization levels that we use. So quantization also leads to some error if it is not properly done. Sampled values need to be placed on the nearest gray values, right? So if you do very coarse quantization, your values will get shifted because of the error in quantization. So as I mentioned, there is a trade-off between quantization and the size of the 2D image. In the sense, if you do a very fine quantization, you may get the image size to be in megabytes and several megabytes.

If you do coarse quantization, meaning use only 8 bits or 4 bits, then the size of the image will drop down to kilobytes, right? This again is an indicator that one could utilize quantization as a mechanism to compress an image. More about it, we can see in the image compression part. So here we have the same image that we saw earlier. What is represented on the right on the top is the sampling part to say this is the grid we want to choose. Even after choosing the sampling part, your quantization will then say how many gray level values do you have to represent it.

And as I said, if you have 8, you have 256 values. If you have 3 bits, you have 8 values. And if you have 12 bits, you have 4096 values. And again, as mentioned in the previous slide, the more the number of bits, less the number of errors that are caused due to the quantization process. So that doesn't mean we want the error to be very, very less. Then you do make the image size to be megabytes and gigabytes. And that doesn't also mean we use very less number of bits. So then the error goes high.

So it's a trade-off between how much size you want versus how much quantization error you are affording or willing to accommodate. Now we jump into some of the image processing libraries. There are many libraries to process images. Some of them are very specific to medical images. And since this course is on medical imaging, we'll focus more on what are the medical imaging libraries which we can use. In general, my recommendation is that you would have to get acquainted and familiar with multiple of these libraries to get a cross understanding and parallel views of the same concept from different libraries.

How does OpenCV do it? How does ITK do it? And so on. So some of the libraries help you to write minimal code and achieve whatever are your designed applications to realize. And some of them have very sophisticated packages with libraries which make your coding experience far more easier. Currently, the practice is that a number of libraries are Python-dependent because deep learning is now majorly driven using Python-based libraries. So there are libraries like TensorFlow, PyTorch, FastAI, Keras. These are all deep learning-based libraries. You would learn about this when you get to those sessions on deep learning.

But all those are Python-based. So it's fundamental that you should learn Python-based coding and be familiar with Python to get a good grasp on utilizing it for image processing tasks. So some of the libraries that you see here, the scikit-image is Python-based. Pillow is also Python-based. ITK is a C++-based library, but there has been a conversion of ITK with Python wrappers so that you can also use ITK within Python. So these two libraries, ITK and OpenCV, are C++-based, but there are Python wrappers around it. So one can utilize these C++ functions even within a Python environment.

So what I'm trying to say is that for image processing libraries, especially if you want to code and create your own solutions, Python is going to be an integral part and knowledge of Python is going to be very vital. There is also the scientific computing libraries like SciPy. It is not mentioned here. Numerical library like NumPy. And there is MathPlotlib for plotting these graphs that you get and representing visually what are the results you have. So these are many of these libraries which all have capabilities to do image processing and also medical image processing.

And my recommendation is to get familiar with at least two, three different of these libraries, especially scikit-image, OpenCV and simple ITK. I would highly recommend. So we start with one of these libraries to get a first feel for how does image processing in Python look like. And here we have a small example of taking an image, reading an image into your code, and then trying to get some characteristics of the image represented, like what are the gray level values or pixel values that I see in the image, right?

So we start with importing the required libraries. PIL stands for Python Image Processing Library. You can import star, then you get all the library functions to have with. Then we have image.open. Image is the library that you have utilized. It has commands to open an image. You give the path for the image and the image gets read into matrix format called IMG. IMG could be a NumPy format. We have to see what format it is. It depends on the library and what it returns. So PIL could return a NumPy.

Some other library could return maybe a slightly different format, right? Maybe an array matrix format directly. So print the format is image.format returns what the format of the image is. So the image format is JPEG. The size of the image is then computed using the command, using image.size. So this image has 1920 rows and 1273 columns. And for those who have paid attention so far, this is the quantization in the XY axis. That is the sampling part, right? So this is the digitization in the XY axis.

That is the sampling part. And the mode of the image is an RGB, meaning it is a color image. There is a red, green and blue channels in this image, right? So that is the first aspect about reading an image. The second aspect is about what is the mathematical representation of the image. As we saw, an image has to be represented internally in an matrix format. And the matrix generally in Python is a numpy format. Numerical processing in Python is called numpy. So here, once again, we go about importing the required libraries.

We utilize numpy, which is numerical processing in Python, and have this command called as array to convert maybe from image format to array format. We read the image like as in earlier, and we now go into printing what is, we use as array from numpy to convert that image to array format. And then we can print the shape of the image, which is how many rows and columns. And then you see it is 1273,1920,3. The 3 is for the red, green and blue channels. Now, you would see that there is inherently a difference between what you saw here, 1920,273,1273.

What you see here is 1273,1920. And that is because row-specific formats versus column-specific formats. Some specify the columns first, some specify the rows first. OpenCV has one particular way. NumPy has a particular way. So you should get familiar and accustomed to this. It is beyond the scope of this course to go into this level of detail and how is the image represented in NumPy. But it is at least worthwhile to be mentioned that the representation in OpenCV is different as compared to NumPy, and one has to pay attention to it.

Now as you get more familiar... And comfortable with working with NumPy images or OpenCV, you would get comfortable in the way these images are represented in the computer. Now the last aspect is about saving an image what you have read. Maybe you do some enhancements, convert the image to grayscale. So here you have an image which is a color image. We read the image. We use this library called image operations, image ops, and there is a command in it to convert it into grayscale. We pass IM1, which is the image which is read, to this method to convert to grayscale, and you get IM2.

Show, show increases the image, and you see that this image is now black and white image or a grayscale image. And finally, we use IM2.save to save this modified image into a PNG file called modify.png. One can choose different file formats to write this image. It can be JPEG, it can be PNG, it can be TIFF image, and so, GIF image, and so on. Well, medical images, they also come in varied formats. And so, we had to look at what are the image formats in PNG, like PNG in computer vision, what are the image formats in medical images.

So, we finish this session now with having understood some bit of sampling and quantization. And in the next session, we would go into the quality metrics.

Want the rest of Advanced Medical Image Processing in Clinical Applications?

Get the full course syllabus and fees.

The full course is paid (₹40,000). We'll send the syllabus and call you back within one working day.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Full course · paid

Unlock the full Advanced Medical Image Processing in Clinical Applications course

The remaining 42 lessons, mentor-reviewed projects and a certificate come with the paid course (₹40,000). Leave your details: we'll send the syllabus and fees and call you back.

Get the syllabus and fees

A counsellor calls you back within one working day.

By submitting you agree to our Terms and Privacy Policy.

Grow by Skill-Lync · free lesson

Start watching

Free, no payment. Leave your details once and every free lesson on Grow plays straight away. A Skill-Lync counsellor may call you about the full Advanced Medical Image Processing in Clinical Applications course.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.