OpenCV Python Tutorial #2 - Image Fundamentals and Manipulation
Skills:
CV Basics90%
Key Takeaways
This video tutorial covers the fundamentals of image representation and manipulation in OpenCV using Python, including loading images into numpy arrays, understanding pixel values, and performing basic operations such as copying and pasting regions of an image. The tutorial uses OpenCV and numpy libraries to demonstrate these concepts.
Full Transcript
[Music] hello everybody and welcome to the second video in this opencv python tutorial series in this video we're going to be talking about image manipulation how images are actually represented in the computer and just giving you a bunch of information about how images actually work because that's going to be very important for this series it's kind of a prerequisite for the rest of the videos anyways one of the kind of cool things i will show you how to do here is to copy a part of one image to another part of the image so let's take a generic example let's say you had an image where there was like a hockey puck in it or a soccer ball or something you wanted to copy that multiple times well i'll show you how you could do that anyways that is what we're going to do in this video before i dive into that though i will quickly mention that i am following along with opencv documentation for this tutorial so a lot of people ask me you know how do you come up with this stuff like are you just copying this from opencv um i'm trying to be open and transparent with you guys what i do before these videos is i go i read the opencv documentation i pull out stuff that i think is interesting and that i want to show i skip over a lot of it and then i just go ahead and you know teach something interesting essentially so i want to make that clear i will link all of the opencv documentation that i use for videos in the description so if you want the more detailed stuff where you want to see all the stuff that i skipped over then click the link in the description last thing there is a github repository for this tutorial series this will be linked in all of the descriptions for all of these videos and you can see all of the code that we write in each video right now i'm obviously only up to tutorial one so i have the tutorial one code upload on github and as the code gets more complicated this will be more useful so just check the descriptions anyways let's dive in here so the first thing that i want to show you is how an image is actually represented because obviously we loaded in this image but what does the image actually look like what is representing this image and how do we display this image well we know how to display it but how does the computer kind of interpret it so let me just print out this image here and let's just have a look at what it is so when i print it out we actually get what's known as a numpy array here you can see we have a bunch of pixel values which are all just white because this is the border of the image that it's showing us anyways the reason i'm showing you this is because we need to understand how an image is actually represented now in our opencv we actually use numpy so opencv and numpy are very closely correlated and when you load an image what it actually does is it extracts the pixels from this image and loads them into a numpy array so if i were to type or i were to print sorry the type of image you would see that we get a numpy.nd array you don't have to understand numpy you don't have to know numpy at all to follow along with this but if you do know numpy then this is probably useful information so numpy is kind of a high performance array library for python it just allows you to do more optimized operations with arrays i believe it's backend or it's you know lower level code is written in c or c plus and well it just makes this we can do faster and better operations on arrays in python anyways anything that you would normally know about a regular list in python will apply to a numpy array except there's just a ton of other stuff that you can do so for example i can print out the shape of a numpy array now the shape of this array tells me the number of rows the number of columns and then the number of channels in my image specifically when we're talking about images so if i print this out we get 995 by a thousand by three that tells me that i have 995 rows in my image when i say rows that's the height of the image this tells me i have a thousand columns so that is the width of the image and then it tells me that i have three channels so again the order is height width and then channels now when i say channel a channel is really just the color space of our image so how many pixels or how many values sorry are representing each pixel now it turns out that uh in our numpy arrays or in our opencv library whatever we have three values that represent each of our pixels now those values are red green and blue and specifically in opencv it's actually going to be blue then green then red now let me show you what i mean by this so firstly you have to understand that when we're representing this image we have a three dimensional array we have an interior array so let me let me draw a kind of example here it'll look something like this we have our first array then inside of this array we have a bunch of other arrays these arrays represent the rows of our images then inside of these arrays we have a bunch of pixel values and these are the columns in each row so i might have a pixel that is black so that would be 0 zero i might have a pixel that's white so 255 255 but this is kind of how it's laid out so if i just had a two by two image so two pixels by two pixels then let's just copy this it will be represented in our program uh something like this we just have two pixels by two pixels we'd have two black pixels and then two white pixels so we would know that this pixel here was at position zero zero or it was the top left pixel because that's where it is in the array so hopefully that's clear but you have three values that represent each pixel and regularly it's going to be red green blue in opencv it's actually going to be blue green red now what i mean by that is if we have three values 0 0 0 here we read them like this blue green then red so this is the amount of blue in our pixel the amount of green in our pixel and the amount of red in our pixel now the values here that are uh valid is between 0 and 255 so 0 represents none 255 represents all so if i were to have say 255 0 0 that means i have a blue pixel i have no green no red and all blue if i were to have like 128 here that would mean i would have like a slightly lighter blue than if i just had 255. so hopefully that's clear but that is how you represent a pixel with blue green and red values you have three of them all all of which which are in the range of zero to 255. so we will continue on one second but i need to quickly thank the sponsor of this video and this series which is algo expert algo expert is the best platform to use when preparing for your software engineering coding interviews they have a data structures crash course mock interviews and over 125 high quality coding interview questions that all have a detailed conceptual overview and code walkthrough i'm actually an instructor at algo expert and you can check out some of the questions that i've created by going to the link in the description and using the code tech with tim for a discount on the platform now to bring this back to what i just explained these pixels are all set up in columns and those columns are all in a row you have multiple rows all which have all of the column values in them and then inside of the column uh you have your pixels so hopefully that's clear that's how this works that's the basic representation and the reason that's important is because when we manipulate images all we're really doing is modifying this array for example even when we rotated this image all that actually happened in kind of the lower level when i wrote that cb2.rotate is it took this array that we had and it just rotated the values in that array such that we have the rotated image so you don't really need to use the cv2 library to do all of this it is going to be better to do that but you actually can manually modify images by just modifying the array data structure that represents them so let's show some more concrete examples let us actually change the i'm trying to think of a good example here let us look at the first row of our image so if i wanted to look at the first row of my image i would print out img at index 0. so this will give me the first row so if i do that you can see we have our first row which is filled with a bunch of white pixels now let's just go and look at row 257 when we do that uh 257 is still giving us white pixels uh oh it makes sense because the border it's showing us okay so i'm just going to look at the first row and then what i want to do in the first row is i want to look at the let's see here the middle pixels so i'm going to look at pixels between 45 and 400. so when i do this notice that we now find some pixels that have different colors associated with them just to be clear here the reason i keep getting white is because the border of my image is white so when it just shows me the first and last element in the array so that's why that's happening anyways this is how we would print out the first row and then this is how we would look at the pixels between 45 and 400 in the first row so that column value now if i wanted to look at one pixel in particularly i could remove this slice so we could look at 257 400 and now if i print this out it's going to give me the value of one of these pixels so what i'm going to do now is i'm going to change a bunch of the pixels in my image and i'll show you what that actually looks like when we uh when we should put the image up all right so my screen recording software just bugged out for a second i don't know why that was happening anyways what i'm going to do now is i'm going to loop through the first 100 rows in our image so recall what we're doing is we're going to loop through i just picked 100 as an arbitrary number you can loop through whatever you want i just want to show you how we can actually change pixels and what that looks like so i'm going to say 4i in range and then 100 we're going to look through the first 100 rows we're going to say 4j in range and then here i'm actually going to say img.shape at index 1. now recall that when we do the shape this is going to give us the rows columns and channels so number of values that represent a pixel that's what channels is uh so i want to look at the columns so for each 100 rows i want to loop through the entire width of the image so the entire column i want to actually modify so we're going to loop through img.shape 1 which is well the width of the image so anyways then once i have this i'm going to say img at index i at index j is equal to and then whatever color i want to make all of these pixels so we can make these pixels random colors in fact let's do that because i think that's more fun than just making them black i'm going to import random and then inside of here let's say random dot and then rand and we'll just go up to 255 and then we will do this three times okay and i think that should be good oops i didn't mean to copy the start of the list here let's remove that remove that and okay so let me zoom out so you guys can see this what i've done is said img i j uh is equal to and then random.rand into random.randin random.randint so i'll have three random values now i want to show the image so what i'm going to say is cv2 dot i am show we will say image we will show img and we'll say cv2 dot weight key zero so wait an infinite amount of time then cb2 dot destroy all windows and there we go so let's run this and we got an issue i'll be right back after i look at that so i made a small mistake here i said rand int instead of rand range although we can still make this work i just have to put a zero first for my rand int when you take a random integer you need to have a lower bound and an upper bound so i just had a small mistake but anyways we need 0 to 255 0-255 0-235 i think that should work and now notice that the entire top of my image is a bunch of random colors because we just randomly set the pixels to whatever we wanted them to be or to whatever this randomly selected so that is how you modify the image you can just go ahead and change the pixel values and then when you display the new image it will have those new pixel values so doing things like looping through you know the border of an image and changing it is actually really easy like i just showed you so that's one way to manipulate images and there's a few other ways we can do it as well so what i'm going to show you now is how we can actually copy one part of an image and paste it to another part so i'm going to get rid of this for loop because we don't need that anymore and let's just look at this image and we can see that i kind of have this like logo here right so i don't know the exact dimensions or the exact coordinates of this logo but what i'm going to try to do is is take this logo here take this like little html tag i don't know why i'm calling it a logo sorry and copy it and just throw it somewhere else on the image again this is not to look good it's just for an example so let's try to do that so i'm just trying to figure out where this would probably be okay that's probably about 500 600 something like that anyways let's try this so if i want to copy part of the image what i can do is just copy part of the array so i'll find the part in the array that i want and i'll show you how we do that with the slice and then we'll just take that and we'll just paste that into another part of the array this seems strange but it's pretty straightforward i'm going to say logo or let's just call this tag is equal to and then img and i'm going to use a numpy array slice now i will describe how this works but it's just like a regular slice in python except you can do it twice inside of here so i'm going to say i want to copy from row 500 to row 700 and i want to copy in those rows so in row 500 to 700 i want to copy which columns well i want to copy let's say columns 600 to 900 so what this is going to do is it's going to copy a square of my array so it will copy from 500 to 700 so all of the rows from 500 to 700 not including 700 then it will copy all of the columns from 600 to 900 that are in this row so hopefully that's clear but that's how that works we're going to copy that into this tag variable here then what i'm going to do after that is i'm going to paste this into part of the array so i'm going to say img and then we'll just pick a random position here that we want to paste it at we will say that we want to paste it let's go from row 100 to 300. now notice that when i paste this it needs to be the same shape as what i've copied so i need to pick a location in my image that is the same dimensions as the the image that i copied so say 100 300 then we'll put it at you know what one hundred actually not one hundred let's go like nine no maybe not nine hundred um seven hundred two and then a thousand no we don't have a thousand sorry i'm just trying to pick something that's going to work here let's go 650 to 950. so i'm now going to take whatever part of the image is in this section and i'm going to paste it in this section so i'm going to say this is equal to and then tag and then i will show the image now i understand this might be slightly confusing but i'm just again taking a slice of my image array and then i'm going to replace this entire slice with this slice that we just took previously and notice these are going to be the same dimensions right it's the same number of blocks in between or same number of elements in between so anyways let's run this and now notice that i've completely messed up what i was trying to copy but i've taken whatever this was here this mouse i guess and i've pasted it in the top right hand corner of the image so that is how you can take something from the image copy it and then paste it somewhere else now obviously you need to know where the thing is i was clearly quite off i got the hand here but that's kind of the general idea so i think that's all i wanted to show you in this video again not super interesting or useful we're going to get into all that stuff later on but it's very important to understand this and i would recommend that you mess around with these numpy arrays modify them see if you can write some cool scripts that go and even maybe generate an image and then display that image see what it looks like and just mess around with it so anyways that has been this video i hope this was helpful to you if it was make sure to leave a like subscribe to the channel and i will see you in another open cv [Music] tutorial you
Original Description
Welcome to the second video of the series on OpenCV and Python. I'll start this episode with Image Manipulation, how images are represented in the computer. I'll also give you information on how images work as they are going to be very important for this series.
💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim
📄 Relevant Documentation: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html
📝 Code For This Series: https://github.com/techwithtim/OpenCV-Tutorials
📺 Fix Pip on Windows: https://www.youtube.com/watch?v=AdUZArA-kZw&t
📺 Fix Pip on Mac: https://www.youtube.com/watch?v=E-WhAS6qzsU
🔍 Playlist: https://youtube.com/playlist?list=PLzMcBGfZo4-lUA8uGjeXhBUUzPYc6vZRn
⭐️ Timestamps ⭐️
00:00 | Intro
01:45 | Image Representation
04:02 | Values that Represent our Pixels
07:20 | Accessing Pixel Values
08:45 | Changing Pixel Colors
11:37 | Copying & Pasting Parts of Image
15:07 | Outro
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python
👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop
🔗 Social Medias 🔗
📸 Instagram: https://www.instagram.com/tech_with_tim
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/twt
📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: https://techwithtim.net
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
🎬 My YouTube Gear 🎬
🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9
🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV
📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r
🕹 Tripod: https://amzn.to/3hpSprv
🎤 Main Micropho
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
The Role of 3D Cuboid Annotation in Autonomous Vehicle Perception
Dev.to AI
Vision AI: Transforming Business Operations with Computer Vision AI
Medium · AI
Vision AI: Transforming Business Operations with Computer Vision AI
Medium · Machine Learning
Cloud-Optimized OpenCV + A Special Surprise Announcement on OpenCV Live
OpenCV Blog
Chapters (7)
| Intro
1:45
| Image Representation
4:02
| Values that Represent our Pixels
7:20
| Accessing Pixel Values
8:45
| Changing Pixel Colors
11:37
| Copying & Pasting Parts of Image
15:07
| Outro
🎓
Tutor Explanation
DeepCamp AI