16 - Understanding digital images for Python processing
Summary
TLDRIn this Python tutorial for microscopists, the basics of image processing using Python are discussed. The video explains that images are essentially numpy arrays with pixel values representing RGB colors. It covers the RGB color model, the concept of alpha for transparency, and the conversion between different color spaces. The tutorial also demonstrates how to open and manipulate images in Python using libraries like scikit-image and matplotlib, highlighting the ability to perform mathematical operations on images as numpy arrays. The video serves as an introduction to the foundational concepts of image processing.
Takeaways
- 💻 The script is a Python tutorial aimed at beginners, focusing on image processing.
- 🖼️ An image is described as an array of numbers or pixels, with each pixel representing a number.
- 🌈 For color images, pixel values are defined by red, green, blue (RGB), and alpha (transparency).
- 🎨 The script explains how to define colors using RGB values, using Microsoft Paint as an example.
- 📈 The tutorial introduces the concept of different color spaces, including RGB and hue, saturation, luminance (HSL).
- 📚 The script demonstrates opening an image in Python using the scikit-image library.
- 🔢 It explains that images can be represented as numpy arrays, with pixel values ranging from 0 to 255 for 8-bit images.
- 🔄 The tutorial shows how to convert images from integer to floating-point data types and vice versa.
- 📊 The script discusses the possibility of performing mathematical operations on images, such as addition and multiplication.
- 🖥️ The presenter demonstrates creating an artificial image with random pixel values using numpy.
- 🛠️ The tutorial concludes with a teaser for the next video, which will cover different libraries for image loading and manipulation.
Q & A
What is the main focus of the Python tutorials on the 'Python for Microscopists' YouTube channel?
-The main focus of the Python tutorials on the 'Python for Microscopists' YouTube channel is to teach image processing using Python, targeting students, researchers, and anyone interested in this field, with an emphasis on basic programming and development skills.
How are images represented in Python according to the tutorial?
-Images in Python are represented as numpy arrays, where each pixel is a number. For color images, each pixel consists of values for red, green, and blue.
What is the significance of the alpha value in the context of image processing?
-The alpha value in image processing defines the transparency of a color. It determines how transparent the color is at a particular location in the image.
How does the tutorial demonstrate the RGB color model?
-The tutorial demonstrates the RGB color model by using Microsoft Paint to show how colors can be defined by adjusting the red, green, and blue values, and how these values combine to create different colors.
What is the difference between an 8-bit and a 16-bit image in terms of pixel value range?
-An 8-bit image has pixel values ranging from 0 to 255, while a 16-bit image has a broader range from 0 to 65535, allowing for more color depth and detail.
How can one convert an image from one data type to another in Python?
-In Python, one can convert an image from one data type to another using functions from libraries like skimage, such as the 'image_as_float' function, which converts images to floating-point representations.
What is the purpose of using 'matplotlib' and 'plt.imshow' in the context of image processing?
-In the context of image processing, 'matplotlib' and 'plt.imshow' are used to display images. They provide a visual representation of the numpy arrays that represent the images.
How does the tutorial illustrate the concept of manipulating pixel values in an image?
-The tutorial illustrates the concept of manipulating pixel values by creating an artificial image with random numbers, modifying the pixel values to darken the image, and replacing certain pixel values to draw colored boxes on an existing image.
What mathematical operations can be performed on images since they are numpy arrays?
-Since images are numpy arrays, any mathematical operation that can be performed on numpy arrays can also be done on images, such as addition, multiplication, and the use of min and max functions to find the range of pixel values.
What is the significance of the tutorial's discussion on different data types for image representation?
-The discussion on different data types for image representation is significant because it highlights the importance of choosing the appropriate data type based on the required color depth and the potential loss of information when converting between types, such as from 16-bit to 8-bit.
Outlines
💻 Introduction to Digital Images in Python
This paragraph introduces the concept of digital images in the context of Python programming, aimed at students, researchers, and developers interested in image processing. The speaker explains that an image can be viewed as a numpy array of numbers, where each pixel is represented by a number. For color images, each pixel is defined by red, green, and blue values, and sometimes an alpha value for transparency. The speaker uses Microsoft Paint as an example to illustrate how colors are defined in RGB. The tutorial also touches on the idea that images can be manipulated in Python, similar to how one might use image editing software, and sets the stage for upcoming tutorials on opening and manipulating images using Python libraries.
📊 Understanding Image Arrays and Data Types
The speaker delves into the technical aspects of image arrays, explaining that they are composed of pixel values ranging from 0 to 255 for each color channel. The dimensions of an image are described, and the speaker demonstrates how to open an image file in Python using the scikit-image library. The paragraph also covers the data types of images, such as unsigned integers, and how they can be converted between different types, such as from 8-bit to floating-point representations. The speaker uses numpy to create an artificial image with random pixel values and discusses the min and max functions to analyze these values, highlighting the flexibility of numpy arrays in image processing.
🔢 Manipulating Image Data with Numpy
This section focuses on the manipulation of image data using numpy, a fundamental library in Python for numerical operations. The speaker demonstrates how to perform mathematical operations on images, such as multiplying an image by a scalar to adjust brightness. The concept of overlaying one image onto another is introduced, and the speaker shows how to create a numpy array with specific values to simulate this. The paragraph reinforces the idea that images are simply numpy arrays and can be manipulated with the same mathematical operations that can be applied to any other numpy array.
🎨 Practical Image Manipulation Techniques
The final paragraph demonstrates practical image manipulation techniques using numpy. The speaker shows how to replace certain pixel values in an image to create a visual effect, such as drawing a colored box. The paragraph emphasizes the power of numpy for image processing, allowing for the application of mathematical operations directly on images. The speaker concludes by summarizing the foundational concepts covered in the tutorial and hints at future tutorials that will explore different libraries for image loading and manipulation, including machine vision applications.
Mindmap
Keywords
💡Python
💡Image Processing
💡Numpy Array
💡Pixel
💡RGB
💡Alpha
💡Color Space
💡Image Libraries
💡Unsigned Integer
💡Floating-Point
💡Image Manipulation
Highlights
Introduction to the basics of Python for image processing, targeting students, researchers, and beginners.
Explanation that an image is essentially a numpy array of numbers representing pixels.
Description of color images as arrays with values for red, green, blue, and alpha (transparency).
Demonstration of defining colors in RGB using Microsoft Paint as an example.
Introduction to the concept of color spaces, including RGB and hue, saturation, luminance.
Practical example of opening an image file in Python using the scikit-image library.
Discussion on the data type of images, such as unsigned integer 8-bit, and its range of values.
Illustration of how to print and inspect the pixel values of an image in Python.
Tutorial on creating an artificial image with random pixel values using numpy.
Explanation of how to display images in Python using matplotlib's imshow function.
Conversion of image data types from unsigned integer to floating-point using skimage.image.as_float.
Demonstration of mathematical operations on images, such as multiplying an image by a scalar.
Introduction to the concept of adding two images together to simulate image overlay.
Practical example of modifying an image by replacing pixel values to create a red box.
Summary of the tutorial emphasizing the foundational role of numpy arrays in image processing.
Anticipation of the next tutorial focusing on different libraries for image loading and manipulation.
Transcripts
yes this is Sweeney and you're watching
the basics of Python tutorials on my
channel Python for microscopists on
YouTube and again these videos are
intended for students researchers or
anyone who's interested in image
processing using Python but again that
the basic user basic programmer or basic
developer in mind now until now we've
talked about the the core functionality
of Python including variables functions
classes but we haven't talked much about
images and let's do that today and as I
mentioned in one of the previous videos
you know dedicated towards digital
images an image is nothing but an array
of numbers or pixels and each pixel is
nothing but a number
so in summary an image is a numpy array
and for color images at each pixel the
values are red green and blue so this is
how the pixel values are defined and in
fact when it comes to Python you can
think of these as four values red green
blue and another one called alpha that
defines the transparency of your image
how transparent is your is your color
and that that are the pixel at that
location and and we understand that
typically if you work with Microsoft
PowerPoint for example and and you
change the font colors and you probably
have drawn like a few boxes and then
filled it with color and when you select
that color if you select one of the
preset colors you may not have realized
this but if you expanded that and if you
have gone into selecting your own color
you probably define color in RGB let me
let me actually open a Microsoft Paint
so I can I can describe exactly what we
are talking about so I believe paint has
very similar tools so when you actually
select red here presets you know what
does that mean if you if I click on edit
colors you can see I can define colors
by changing red
green or blue I put my green and blue
two zeros and then type 255 here this is
nothing but pure red if I put 255 for
green this is equal mixture of red and
green so it's gonna give me yellow in
this case now the colors can also be
defined in hue saturation luminance
color space so not just in RGB space but
then it's very difficult for us to think
in a hue saturation and luminance color
space that's why we tend to use RGB but
there are reasons why you can you need
to switch from one space to the other
and you can do that in in Python pretty
easily so so this is the basic of
exactly a digital image and let me open
an image and this is the same image I've
been using and a bunch of cells and when
I put my cursor you can see the RGB
values up here on the bar down there it
actually says right now the red color is
253 green is 72 and blue is 87 and at
the dark pixel almost all zeros so this
is this is a color image and this is
again nothing but a numpy array a whole
bunch of numbers yeah
so let's let's open this image in Python
and my next tutorial is going to be
about opening Python
I mean opening images in Python using
various libraries so I'm not gonna focus
on how to open these images I'm going to
use a library called Sai kit image and
within that I'm gonna use function
called IO and I'm going to define a
variable my image which is nothing but I
Oh dot I am weed and my my images within
the sub folder called images and it's
called test underscore image start jpg
so this is how I'm actually reading my
image and now let's actually print the
image I'm not going to show the image
let's just print it out what does that
mean yeah again there and let's go ahead
and print
so when I print this it should print the
number array again up here in the
variable explorer you can see that this
is a you into eight type of image which
stands for unsigned integer 8-bit what
is 8-bit mean if you look at two to the
power of eight its 256 so the numbers in
this image go from 0 to 255 this is this
is nothing but when we looked at
Microsoft Paint you know or image J the
pixel values they go from 0 to 255 for
red 0 to 255 for green and same for the
blue so unsigned integer means the
values go from 0 to 255 and the
dimensions of this image are 513 by 6 39
by 3 and the three stands for red green
and blue and if I scroll up here you
should see that the first 0 0 pixel
top-left corner that's where the image
starts that's where am i 0 0 0 is so
that pixel has a value of 12 54 and 92
for our GB in fact we can find that out
from our image J here if I put the
cursor at 0 0 you can see that the value
of that pixel is 12 54 and 92 yeah so
when I open this image in Python this is
no different than opening it in image a
this is exactly the same image now I can
change the values you know because this
is a numpy array I can change these
values so just to reinforce the fact
that this is a numpy array let me go
ahead and create a artificial image and
fill the pixel values with some random
numbers okay for that I'm gonna use
because it's a numpy array I'm going to
use numpy so I'm going to import numpy
as NP which is our convention right here
and let's just call something random
image and this is equal to NP dot random
dot random and
the dimensions we can make it 500 by 500
if I actually before printing let's go
ahead and show this to show images we'll
use from Matt plot lib import pie plot
as PLT you have to memorize this
statement this is if you even look
anywhere online this is exactly what
most people use as convention because
it's very easy to type now PLT dot i am
show and what do what do we want to show
random underscore image so when i do
that you should see random underscore
image RN vom so you type it the right
way and here is the random image nothing
but noise and this is just this is just
an umpire array in fact if I print it
out just like we printed the previous my
image let's print random underscore
image now let's see what type of image
this is obviously we know right away
that this is not an unsigned integer
this is a floating-point 64 you know
image so when I print it out you should
see the values or between 0 & 1
they're all between 0 & 1 which is
nothing but a floating-point number in
fact instead of printing out the random
image let's actually do print out random
image dot min which prints out the
minimum value remember when we talked
about numpy arrays you can find out the
minimum values in the array and maximum
values just by using this min and Max
command so let's do random underscore
image dot max okay so when I print this
out now you should see the minimum value
seems to be 10 to the minus 6 maximum is
0.99 and so this is nothing but 0
practically 0 and 1 if you do the same
for the same for this other image like
my image
let's not print the image the system if
these values instead of random image
let's just do my image here my image and
when we print the minimum and maximum
values so 0 and 255 for my image and
about 0 and 1 for other image and by the
way we can actually convert these from
one type to the other so I can actually
convert this integer image into a into a
into a floating-point image and for that
I can actually do
I was thinking whether I should do it
now but why not so I think from SK image
we can import a function called image as
float okay this one converts these
images so when I come down and let's now
say my load image equals to I am just
converting this image into floating
point it's as simple as since we are
already importing it image as load and I
want to convert my underscore image okay
so this should work my float image is
this and now that's actually print
minimum and maximum for my float image
load image and let's not look at this
random image anymore let's comment that
part out and when I run this you should
see that for the first one for my image
the minimum and maximum is zero and 255
for this new the same image which is
which I'm printing out again I mean
which I converted to float the new
values are 0 and 1 in fact you can why
not go ahead and look at this image so
let's to make sure they look exactly the
same I am sure my image and that's also
I am show and this one should be i
underscore load image okay so let's go
ahead and show these two images and am I
not showing my image and my float image
its comment actually I know I'm here
let's not get into that let's comment
and print this out first and this is my
image right and then let's comment this
and show the other one yeah it looks
exactly the same
well I could have actually opened two
different windows and all that but let's
not worry about it yet so this is these
two images are exactly the same except
here the values are going from 0 to 255
and here they are going from 0 to 1 okay
so this is this is a quick introduction
basically mentioning that the images are
nothing but numpy arrays and then at
every pixel the values are combination
of red green and blue and the value
combination if it then the value itself
depends on whether it is a unsigned
integer or floating and in fact let me
copy and paste a few other data types
this kind of gives you an idea of what
else you know what other data types
exist out there so if you if I just copy
paste it from here excuse me I copy and
paste it right there you can actually
see see that unsigned integer eight goes
from zero to 255 unsigned 16 is nothing
but 2 to the power of 16 so it goes from
0 to 65535 and so on so you can convert
from one format to the other based on
based on the need obviously the
information that's contained in 16-bit
is pretty high so if you convert it down
if you downscale it to an 8-bit then you
will be losing some information if
information is stored within those those
pixels so you need to be careful when
you are when you're changing the for the
file types sorry the data types so now
that this is done let's let's actually
do let's actually do something fun
because these are numpy arrays again you
can add images you can multiply images
for example if I go back to my image I
can actually say let's say dark image
equal to my underscore image multiplied
by 0.5
I'm just reducing all the values within
the image by half okay so I just call it
dark image but if you normalize it it's
not a dog anymore but if I do print
let's say let's say let's go ahead and
print a dark image image and when I do
that you should see that the values are
now not 255 well in fact right now
because yeah you can see 26.5 you know
they're all like half the in fact if you
do dark image we know the minute minimum
is zero so let's just do dark image dot
max and let's just print the maximum
value here and it is 127 0.55 okay half
of 255 so you can do math you can import
a different image which is another numpy
array and you can actually add that
image to this existing image so you can
do all
that a way of simulating that is a way
of simulating that is let's actually
create a numpy array that actually has
all zero values well actually let's say
that has all red or something and then
let's overlay that on top of our image
so to do that let's go ahead and import
numpy as NP which we have already done
there and we are not using that and we
need to create a numpy array and we know
how to create a numpy array right now
okay and in fact let's instead of
creating a blank array let's actually
replace some of the values that we have
in this image with that's more fun
actually so my image let's again go
ahead and look at my image so we have
done a lot of stuff here so let's
actually delete everything there and
let's run this like a sanity check so
this is nothing but my image and it's
going from 0 to 255 and this is this is
just you know an image there so now I'm
going to replace some of the pixels and
you know how to do that we have done
slicing as part of our numpy which is
taking a subset of values from our numpy
array and how do we do that you just
take your numpy array in this arc in
this case or numpy array is called my
image and I'm gonna go from let's say
this is this image is size 500 by 600 so
let's draw a relatively big red box so n
- 200 and also go from 10 to 200 in Y
and in Z I don't want to do anything
this is this is nothing but our three
red channels and replace these with 255
0 and 0 so let's draw a red box right
there and then
PLT dot I am show and let's show my
underscore let's comment this out so
we'll just see
image with a red box on top of it and
again top left is zero zero so we are
going from zero to 200 zero to 200 and
then filling this with red you know so
we can fill this with yellow box we know
that red plus green equals two yellow
okay so red green and we can fill this
with white box so this is again a quick
summary of you know how again
reinforcing the message that images are
nothing but numpy eros which means
whatever maths you can do on numpy eros
you can do it on images and this is
exactly the building block the
foundation of image processing this mat
that we are doing here is the foundation
of image processing so in our next
lecture in our next tutorial let's
actually go through a few ways of
opening images and that way we can look
at if you like different libraries that
are at least that have tools for image
loading and some of them have extensive
tools for image manipulation and even
like getting into machine vision and so
on so I hope you found this tutorial to
be useful and let's continue this
discussion in our next video thank you
very much
5.0 / 5 (0 votes)