16 - Understanding digital images for Python processing

DigitalSreeni
7 May 201918:31

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

00:00

💻 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.

05:03

📊 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.

10:07

🔢 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.

15:10

🎨 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

Python is a high-level, interpreted programming language known for its readability and ease of use. In the context of the video, Python is used as the primary tool for image processing, highlighting its versatility and the extensive library support it offers for handling and manipulating images.

💡Image Processing

Image processing refers to the techniques and algorithms used to analyze and modify images. The video focuses on using Python for image processing, emphasizing its practical applications in fields like digital microscopy, where images are analyzed and manipulated to extract meaningful information.

💡Numpy Array

A Numpy array is a powerful data structure in Python that allows for efficient storage and manipulation of large, multi-dimensional arrays of data. In the video, images are described as Numpy arrays where each pixel is represented by a number, and the entire image is an array of these numbers, making it a fundamental concept for understanding image data in Python.

💡Pixel

A pixel, short for 'picture element,' is the smallest unit of an image. The video explains that an image can be thought of as an array of pixels, with each pixel having a value that determines its color. This concept is crucial for understanding how images are represented and manipulated at a basic level in Python.

💡RGB

RGB stands for Red, Green, and Blue, which are the primary colors used in digital imaging to create a wide range of colors. The video discusses how each pixel in a color image has associated RGB values that define its color. This RGB model is central to understanding color representation in digital images.

💡Alpha

The alpha channel in digital images represents the transparency of a pixel. The video mentions that, along with the RGB values, the alpha value determines how transparent a color is, allowing for a range of effects from fully opaque to fully transparent.

💡Color Space

Color space is a mathematical model that describes the way colors can be represented in digital systems. The video touches on different color spaces like RGB and HSL (Hue, Saturation, Luminance), explaining that while RGB is more intuitive for programming, other color spaces like HSL might be used for specific image processing tasks.

💡Image Libraries

Image libraries are software libraries that provide functions and tools for working with images. The video mentions libraries like 'Sai kit image' and 'Matplotlib', which are used to open, display, and manipulate images in Python, showcasing the support available for image processing tasks.

💡Unsigned Integer

An unsigned integer is a numerical data type that can only represent non-negative integers. In the video, it is explained that image pixel values are often stored as unsigned integers, with 8-bit unsigned integers ranging from 0 to 255, which is a common format for representing pixel values in images.

💡Floating-Point

A floating-point number is a type of numeric data that can represent fractional values. The video discusses converting images from integer to floating-point representations, where pixel values can range from 0 to 1, providing a different scale for image data manipulation and analysis.

💡Image Manipulation

Image manipulation refers to the process of altering images, such as changing colors, resizing, or adding effects. The video provides examples of how images can be manipulated in Python, such as creating an artificial image with random pixel values or modifying an existing image by changing pixel values, demonstrating the practical applications of image processing.

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

play00:00

yes this is Sweeney and you're watching

play00:02

the basics of Python tutorials on my

play00:06

channel Python for microscopists on

play00:08

YouTube and again these videos are

play00:11

intended for students researchers or

play00:13

anyone who's interested in image

play00:15

processing using Python but again that

play00:17

the basic user basic programmer or basic

play00:19

developer in mind now until now we've

play00:24

talked about the the core functionality

play00:28

of Python including variables functions

play00:32

classes but we haven't talked much about

play00:35

images and let's do that today and as I

play00:40

mentioned in one of the previous videos

play00:41

you know dedicated towards digital

play00:44

images an image is nothing but an array

play00:48

of numbers or pixels and each pixel is

play00:52

nothing but a number

play00:53

so in summary an image is a numpy array

play00:56

and for color images at each pixel the

play01:00

values are red green and blue so this is

play01:03

how the pixel values are defined and in

play01:06

fact when it comes to Python you can

play01:08

think of these as four values red green

play01:10

blue and another one called alpha that

play01:13

defines the transparency of your image

play01:15

how transparent is your is your color

play01:19

and that that are the pixel at that

play01:22

location and and we understand that

play01:25

typically if you work with Microsoft

play01:28

PowerPoint for example and and you

play01:31

change the font colors and you probably

play01:33

have drawn like a few boxes and then

play01:35

filled it with color and when you select

play01:37

that color if you select one of the

play01:39

preset colors you may not have realized

play01:42

this but if you expanded that and if you

play01:44

have gone into selecting your own color

play01:46

you probably define color in RGB let me

play01:51

let me actually open a Microsoft Paint

play01:53

so I can I can describe exactly what we

play01:57

are talking about so I believe paint has

play02:00

very similar tools so when you actually

play02:02

select red here presets you know what

play02:05

does that mean if you if I click on edit

play02:07

colors you can see I can define colors

play02:10

by changing red

play02:12

green or blue I put my green and blue

play02:15

two zeros and then type 255 here this is

play02:18

nothing but pure red if I put 255 for

play02:22

green this is equal mixture of red and

play02:24

green so it's gonna give me yellow in

play02:27

this case now the colors can also be

play02:29

defined in hue saturation luminance

play02:32

color space so not just in RGB space but

play02:37

then it's very difficult for us to think

play02:39

in a hue saturation and luminance color

play02:42

space that's why we tend to use RGB but

play02:45

there are reasons why you can you need

play02:47

to switch from one space to the other

play02:49

and you can do that in in Python pretty

play02:52

easily so so this is the basic of

play02:56

exactly a digital image and let me open

play02:59

an image and this is the same image I've

play03:02

been using and a bunch of cells and when

play03:07

I put my cursor you can see the RGB

play03:09

values up here on the bar down there it

play03:13

actually says right now the red color is

play03:15

253 green is 72 and blue is 87 and at

play03:19

the dark pixel almost all zeros so this

play03:23

is this is a color image and this is

play03:26

again nothing but a numpy array a whole

play03:28

bunch of numbers yeah

play03:30

so let's let's open this image in Python

play03:33

and my next tutorial is going to be

play03:35

about opening Python

play03:38

I mean opening images in Python using

play03:40

various libraries so I'm not gonna focus

play03:42

on how to open these images I'm going to

play03:45

use a library called Sai kit image and

play03:49

within that I'm gonna use function

play03:52

called IO and I'm going to define a

play03:55

variable my image which is nothing but I

play03:58

Oh dot I am weed and my my images within

play04:05

the sub folder called images and it's

play04:06

called test underscore image start jpg

play04:09

so this is how I'm actually reading my

play04:11

image and now let's actually print the

play04:14

image I'm not going to show the image

play04:15

let's just print it out what does that

play04:17

mean yeah again there and let's go ahead

play04:23

and print

play04:26

so when I print this it should print the

play04:29

number array again up here in the

play04:31

variable explorer you can see that this

play04:33

is a you into eight type of image which

play04:37

stands for unsigned integer 8-bit what

play04:43

is 8-bit mean if you look at two to the

play04:45

power of eight its 256 so the numbers in

play04:50

this image go from 0 to 255 this is this

play04:55

is nothing but when we looked at

play04:57

Microsoft Paint you know or image J the

play04:59

pixel values they go from 0 to 255 for

play05:03

red 0 to 255 for green and same for the

play05:05

blue so unsigned integer means the

play05:08

values go from 0 to 255 and the

play05:11

dimensions of this image are 513 by 6 39

play05:15

by 3 and the three stands for red green

play05:17

and blue and if I scroll up here you

play05:20

should see that the first 0 0 pixel

play05:23

top-left corner that's where the image

play05:26

starts that's where am i 0 0 0 is so

play05:30

that pixel has a value of 12 54 and 92

play05:34

for our GB in fact we can find that out

play05:37

from our image J here if I put the

play05:41

cursor at 0 0 you can see that the value

play05:44

of that pixel is 12 54 and 92 yeah so

play05:48

when I open this image in Python this is

play05:51

no different than opening it in image a

play05:53

this is exactly the same image now I can

play05:56

change the values you know because this

play05:58

is a numpy array I can change these

play06:00

values so just to reinforce the fact

play06:04

that this is a numpy array let me go

play06:07

ahead and create a artificial image and

play06:11

fill the pixel values with some random

play06:14

numbers okay for that I'm gonna use

play06:17

because it's a numpy array I'm going to

play06:19

use numpy so I'm going to import numpy

play06:22

as NP which is our convention right here

play06:25

and let's just call something random

play06:28

image and this is equal to NP dot random

play06:32

dot random and

play06:35

the dimensions we can make it 500 by 500

play06:39

if I actually before printing let's go

play06:43

ahead and show this to show images we'll

play06:45

use from Matt plot lib import pie plot

play06:53

as PLT you have to memorize this

play06:56

statement this is if you even look

play06:58

anywhere online this is exactly what

play07:02

most people use as convention because

play07:04

it's very easy to type now PLT dot i am

play07:08

show and what do what do we want to show

play07:12

random underscore image so when i do

play07:16

that you should see random underscore

play07:20

image RN vom so you type it the right

play07:26

way and here is the random image nothing

play07:29

but noise and this is just this is just

play07:32

an umpire array in fact if I print it

play07:35

out just like we printed the previous my

play07:37

image let's print random underscore

play07:40

image now let's see what type of image

play07:44

this is obviously we know right away

play07:46

that this is not an unsigned integer

play07:49

this is a floating-point 64 you know

play07:54

image so when I print it out you should

play07:57

see the values or between 0 & 1

play08:00

they're all between 0 & 1 which is

play08:03

nothing but a floating-point number in

play08:07

fact instead of printing out the random

play08:10

image let's actually do print out random

play08:12

image dot min which prints out the

play08:15

minimum value remember when we talked

play08:17

about numpy arrays you can find out the

play08:19

minimum values in the array and maximum

play08:22

values just by using this min and Max

play08:23

command so let's do random underscore

play08:26

image dot max okay so when I print this

play08:31

out now you should see the minimum value

play08:34

seems to be 10 to the minus 6 maximum is

play08:38

0.99 and so this is nothing but 0

play08:40

practically 0 and 1 if you do the same

play08:43

for the same for this other image like

play08:47

my image

play08:49

let's not print the image the system if

play08:52

these values instead of random image

play08:55

let's just do my image here my image and

play09:00

when we print the minimum and maximum

play09:02

values so 0 and 255 for my image and

play09:07

about 0 and 1 for other image and by the

play09:12

way we can actually convert these from

play09:15

one type to the other so I can actually

play09:17

convert this integer image into a into a

play09:23

into a floating-point image and for that

play09:26

I can actually do

play09:28

I was thinking whether I should do it

play09:30

now but why not so I think from SK image

play09:35

we can import a function called image as

play09:41

float okay this one converts these

play09:44

images so when I come down and let's now

play09:48

say my load image equals to I am just

play09:56

converting this image into floating

play09:58

point it's as simple as since we are

play10:01

already importing it image as load and I

play10:06

want to convert my underscore image okay

play10:09

so this should work my float image is

play10:13

this and now that's actually print

play10:15

minimum and maximum for my float image

play10:28

load image and let's not look at this

play10:32

random image anymore let's comment that

play10:35

part out and when I run this you should

play10:37

see that for the first one for my image

play10:41

the minimum and maximum is zero and 255

play10:43

for this new the same image which is

play10:47

which I'm printing out again I mean

play10:50

which I converted to float the new

play10:51

values are 0 and 1 in fact you can why

play10:55

not go ahead and look at this image so

play10:57

let's to make sure they look exactly the

play11:00

same I am sure my image and that's also

play11:07

I am show and this one should be i

play11:11

underscore load image okay so let's go

play11:16

ahead and show these two images and am I

play11:23

not showing my image and my float image

play11:31

its comment actually I know I'm here

play11:35

let's not get into that let's comment

play11:37

and print this out first and this is my

play11:39

image right and then let's comment this

play11:42

and show the other one yeah it looks

play11:49

exactly the same

play11:50

well I could have actually opened two

play11:51

different windows and all that but let's

play11:53

not worry about it yet so this is these

play11:56

two images are exactly the same except

play11:58

here the values are going from 0 to 255

play12:02

and here they are going from 0 to 1 okay

play12:05

so this is this is a quick introduction

play12:12

basically mentioning that the images are

play12:14

nothing but numpy arrays and then at

play12:17

every pixel the values are combination

play12:21

of red green and blue and the value

play12:23

combination if it then the value itself

play12:26

depends on whether it is a unsigned

play12:29

integer or floating and in fact let me

play12:31

copy and paste a few other data types

play12:34

this kind of gives you an idea of what

play12:35

else you know what other data types

play12:38

exist out there so if you if I just copy

play12:41

paste it from here excuse me I copy and

play12:45

paste it right there you can actually

play12:47

see see that unsigned integer eight goes

play12:52

from zero to 255 unsigned 16 is nothing

play12:55

but 2 to the power of 16 so it goes from

play12:57

0 to 65535 and so on so you can convert

play13:01

from one format to the other based on

play13:03

based on the need obviously the

play13:05

information that's contained in 16-bit

play13:08

is pretty high so if you convert it down

play13:10

if you downscale it to an 8-bit then you

play13:12

will be losing some information if

play13:15

information is stored within those those

play13:17

pixels so you need to be careful when

play13:19

you are when you're changing the for the

play13:23

file types sorry the data types so now

play13:27

that this is done let's let's actually

play13:29

do let's actually do something fun

play13:34

because these are numpy arrays again you

play13:37

can add images you can multiply images

play13:40

for example if I go back to my image I

play13:44

can actually say let's say dark image

play13:47

equal to my underscore image multiplied

play13:51

by 0.5

play13:53

I'm just reducing all the values within

play13:55

the image by half okay so I just call it

play13:59

dark image but if you normalize it it's

play14:01

not a dog anymore but if I do print

play14:07

let's say let's say let's go ahead and

play14:10

print a dark image image and when I do

play14:16

that you should see that the values are

play14:19

now not 255 well in fact right now

play14:22

because yeah you can see 26.5 you know

play14:26

they're all like half the in fact if you

play14:29

do dark image we know the minute minimum

play14:31

is zero so let's just do dark image dot

play14:34

max and let's just print the maximum

play14:37

value here and it is 127 0.55 okay half

play14:43

of 255 so you can do math you can import

play14:48

a different image which is another numpy

play14:50

array and you can actually add that

play14:52

image to this existing image so you can

play14:54

do all

play14:55

that a way of simulating that is a way

play15:00

of simulating that is let's actually

play15:01

create a numpy array that actually has

play15:05

all zero values well actually let's say

play15:09

that has all red or something and then

play15:13

let's overlay that on top of our image

play15:15

so to do that let's go ahead and import

play15:18

numpy as NP which we have already done

play15:22

there and we are not using that and we

play15:25

need to create a numpy array and we know

play15:28

how to create a numpy array right now

play15:30

okay and in fact let's instead of

play15:34

creating a blank array let's actually

play15:37

replace some of the values that we have

play15:39

in this image with that's more fun

play15:41

actually so my image let's again go

play15:44

ahead and look at my image so we have

play15:49

done a lot of stuff here so let's

play15:50

actually delete everything there and

play15:53

let's run this like a sanity check so

play15:57

this is nothing but my image and it's

play16:00

going from 0 to 255 and this is this is

play16:03

just you know an image there so now I'm

play16:07

going to replace some of the pixels and

play16:10

you know how to do that we have done

play16:12

slicing as part of our numpy which is

play16:15

taking a subset of values from our numpy

play16:19

array and how do we do that you just

play16:21

take your numpy array in this arc in

play16:23

this case or numpy array is called my

play16:26

image and I'm gonna go from let's say

play16:30

this is this image is size 500 by 600 so

play16:34

let's draw a relatively big red box so n

play16:37

- 200 and also go from 10 to 200 in Y

play16:41

and in Z I don't want to do anything

play16:44

this is this is nothing but our three

play16:46

red channels and replace these with 255

play16:51

0 and 0 so let's draw a red box right

play16:56

there and then

play16:57

PLT dot I am show and let's show my

play17:01

underscore let's comment this out so

play17:06

we'll just see

play17:08

image with a red box on top of it and

play17:11

again top left is zero zero so we are

play17:13

going from zero to 200 zero to 200 and

play17:17

then filling this with red you know so

play17:20

we can fill this with yellow box we know

play17:24

that red plus green equals two yellow

play17:26

okay so red green and we can fill this

play17:29

with white box so this is again a quick

play17:33

summary of you know how again

play17:37

reinforcing the message that images are

play17:40

nothing but numpy eros which means

play17:42

whatever maths you can do on numpy eros

play17:45

you can do it on images and this is

play17:48

exactly the building block the

play17:49

foundation of image processing this mat

play17:53

that we are doing here is the foundation

play17:55

of image processing so in our next

play17:58

lecture in our next tutorial let's

play18:00

actually go through a few ways of

play18:03

opening images and that way we can look

play18:06

at if you like different libraries that

play18:09

are at least that have tools for image

play18:12

loading and some of them have extensive

play18:14

tools for image manipulation and even

play18:17

like getting into machine vision and so

play18:19

on so I hope you found this tutorial to

play18:23

be useful and let's continue this

play18:27

discussion in our next video thank you

play18:29

very much

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Python TutorialImage ProcessingPixel ValuesArray ManipulationRGB Color ModelDigital ImagesNumpy ArraysProgramming BasicsData TypesImage Analysis
هل تحتاج إلى تلخيص باللغة الإنجليزية؟