25 - Reading Images, Splitting Channels, Resizing using openCV in Python
Summary
TLDRIn this Python for Microscopists tutorial, the host introduces OpenCV, a library for computer vision applications like image and video processing. The video focuses on five fundamental image processing techniques in OpenCV: splitting and merging channels, resizing images, denoising, edge detection, and image enhancement through histogram equalization. The host demonstrates how to manipulate images using OpenCV in Python, including reading and displaying images, and emphasizes the importance of understanding the BGR color convention in OpenCV. The tutorial is practical, providing step-by-step coding examples to help viewers grasp these essential image processing operations.
Takeaways
- 😀 OpenCV is a library dedicated to computer vision applications, including image and video processing, with a focus on real-time video analysis.
- 🔎 It's widely used for various applications such as facial recognition, object detection, motion tracking, and optical character recognition, which are also beneficial for microscopy image processing.
- 🖼️ The tutorial introduces five basic pre-processing operations in image processing using OpenCV: splitting and merging channels, scaling, denoising, edge detection, and histogram equalization.
- 🌐 To install OpenCV in Python, use the command 'pip install opencv-python' in the command prompt.
- 📚 Importing OpenCV in Python is done using 'import cv2'.
- 📸 To open an image in OpenCV, use 'cv2.imread', which can read images in color or grayscale depending on the flag provided (0 for grayscale, 1 for color, -1 for native).
- 🎨 The script clarifies that OpenCV reads color images in BGR format by default, not RGB, which is a common source of confusion.
- 📊 The tutorial demonstrates how to extract individual pixel values and explains that images in OpenCV are stored as numpy arrays.
- 🔄 It shows how to split and merge image channels using both numpy slicing and the 'cv2.split' function, as well as how to merge them back using 'cv2.merge'.
- 🔍 The script also covers image resizing using 'cv2.resize', explaining the use of scaling factors and interpolation methods like 'cv2.INTER_LINEAR' for image upscaling.
- 📈 The tutorial is part of a series, with the remaining topics such as image smoothing, denoising, edge detection, and image enhancements to be covered in subsequent videos.
Q & A
What is OpenCV and what is it used for?
-OpenCV is a library dedicated to computer vision applications, including image and video processing, especially real-time videos. It is used for various applications such as facial recognition, object detection, motion tracking, optical character recognition, and microscopy image processing.
Why is OpenCV useful for microscopy image processing?
-OpenCV is useful for microscopy image processing because it can automatically detect and analyze objects like cells or nuclei, similar to object detection applications. It can also analyze time series data where cells evolve or divide, akin to real-time video analysis.
What are the five basic pre-processing operations in image processing using OpenCV mentioned in the tutorial?
-The five basic pre-processing operations in image processing using OpenCV mentioned are: splitting and merging channels, scaling or resizing images, denoising or smoothing, edge detection, and enhancing images using histogram equalization.
How does OpenCV represent color images by default?
-By default, OpenCV represents color images in the BGR (Blue, Green, Red) format, not RGB.
How can you view the individual pixel values of an image in OpenCV?
-You can view individual pixel values by accessing the numpy array that represents the image. For example, to view the top-left pixel, you can use the coordinates (0, 0) on the image array.
What is the significance of the 'cv2.imread' function in OpenCV?
-The 'cv2.imread' function is used to load images in OpenCV. It's a common function across many packages, and it's used to read images as grayscale or color, depending on the argument passed (0 for grayscale, 1 for color, or -1 for the native image including alpha channel).
How can you split the color channels of an image using OpenCV?
-You can split the color channels of an image by using either numpy array slicing to manually extract each channel or by using the built-in 'cv2.split' function, which separates the image into its blue, green, and red components.
What is the purpose of the 'cv2.merge' function in OpenCV?
-The 'cv2.merge' function is used to combine separate color channel images back into a single color image.
How can you resize an image using OpenCV?
-You can resize an image using the 'cv2.resize' function, where you can specify the new dimensions or use scaling factors. You also need to specify the interpolation method, such as 'cv2.INTER_LINEAR' or 'cv2.INTER_CUBIC'.
What is the difference between the interpolation methods 'cv2.INTER_LINEAR' and 'cv2.INTER_CUBIC' in image resizing?
-Both 'cv2.INTER_LINEAR' and 'cv2.INTER_CUBIC' are interpolation methods used in image resizing. 'cv2.INTER_LINEAR' is used for zooming in on an image, while 'cv2.INTER_CUBIC' is suitable for zooming out, as it provides smoother results by using a more complex algorithm that takes into account the surrounding pixels.
Outlines
💻 Introduction to OpenCV for Microscopy Image Processing
This paragraph introduces an OpenCV tutorial aimed at microscopists, focusing on its applications in microscopy image processing. OpenCV is a library used for computer vision tasks, including real-time video analysis and image processing. It's applicable for tasks such as facial recognition, object detection, and character recognition. The tutorial will cover five basic image processing operations: channel splitting and merging, image scaling or resizing, denoising or smoothing, edge detection, and image enhancement through histogram equalization. The presenter also discusses installing OpenCV using pip and demonstrates how to import and read an image using OpenCV, highlighting the difference between reading an image in color or grayscale.
🖼️ Understanding Color Channels in OpenCV
The presenter explains how to examine individual pixel values in an image using OpenCV, which is imported as a numpy array. They clarify that the color convention in OpenCV is BGR, not RGB, which is crucial for accurate image processing. The tutorial proceeds to demonstrate how to extract individual color channels from an image using both numpy slicing and the built-in OpenCV function 'split'. Additionally, the presenter shows how to visualize images using 'cv2.imshow' and discusses the importance of setting the wait key and window properties correctly to avoid issues with image display.
🔄 Channel Splitting and Merging in OpenCV
This section delves into the process of splitting and merging color channels in an image using OpenCV. The presenter first uses numpy array slicing to extract individual color channels and then demonstrates the use of the 'cv2.split' function for the same purpose. They also cover the reverse operation, merging separate color channels back into a single color image, using the 'cv2.merge' function. The importance of using the correct variable names and channel order when merging channels is emphasized.
🔍 Resizing Images with OpenCV
The paragraph discusses the process of resizing images in OpenCV. The presenter introduces the 'cv2.resize' function, explaining the two methods for resizing: using scaling factors or specifying new dimensions. They also touch on the concept of interpolation methods, which determine how new pixels are calculated when resizing images. The presenter demonstrates resizing an image using a scaling factor and shows the original and resized images using 'cv2.imshow'. The practicality and simplicity of image resizing in OpenCV are highlighted.
📈 Concluding and Previewing Upcoming Topics
In the final paragraph, the presenter concludes the current tutorial and provides a preview of upcoming topics. They acknowledge that covering image smoothing, denoising, edge detection, and image enhancement in one video would be too lengthy, and thus decide to create a separate video for these advanced image processing techniques. The presenter thanks the viewers for their attention and sets the stage for further tutorials on pre-processing functions in OpenCV.
Mindmap
Keywords
💡OpenCV
💡Image Processing
💡Facial Recognition
💡Object Detection
💡Real-Time Video Analysis
💡Channel Splitting and Merging
💡Denoising
💡Edge Detection
💡Histogram Equalization
💡Anaconda Distribution
Highlights
Introduction to OpenCV, a library dedicated to computer vision applications.
OpenCV's wide range of applications including facial recognition, object detection, motion tracking, and optical character recognition.
Relevance of OpenCV for microscopy image processing, such as automatic cell and nuclei detection.
Tutorial on five basic pre-processing operations in image processing using OpenCV.
Explanation of splitting and merging channels in multi-channel images.
Guidance on scaling or resizing images using OpenCV.
Denoising or smoothing images as a pre-processing step.
Edge detection as a fundamental operation in image processing.
Enhancing images using histogram equalization in OpenCV.
How to install OpenCV using pip in Python.
Importing OpenCV in Python and opening an image file.
Understanding image dimensions and channels using the shape attribute.
Reading images in grayscale versus color and the impact on image dimensions.
The default color channel order in OpenCV is BGR, not RGB.
How to extract individual pixel values from an image.
Using numpy array slicing to split image channels.
Built-in function 'split' in OpenCV to separate image channels.
Merging image channels back into a single color image using 'merge'.
Demonstration of resizing images with scaling factors and interpolation methods.
Practical example of resizing an image and comparing the original and resized versions.
Announcement of a follow-up video covering image smoothing, denoising, edge detection, and image enhancements.
Transcripts
hey guys you're watching Python
tutorials on my channel Python for
microscopists on YouTube in today's
tutorial let's talk about open CV this
would be an introductory video for open
CV and as I mentioned in one of my
previous videos open CV is library
that's dedicated for computer vision
type of applications it can be images it
can be videos and especially the
real-time videos now open CV is used for
many many types of applications for
example facial recognition object
detection motion tracking optical
character recognition and many many
others now they're still useful for
microscopy image processing applications
because let's say you have a bunch of
cells of nuclei that you want to
automatically detect this is no
different than any other object
detection type of application if you
have a time series you know where the
cells are evolving there where the cells
are dividing for example this can be
very close to the real time video
analysis so in this tutorial I am going
to talk about five basic pre-processing
operations in image processing using
open CV one would be splitting and
merging channels if we have
multi-channel images like red green and
blue images scaling or resizing if you
want to call them scaling these images
denoising or smoothing if you want to
call it and edge detection and finally
enhancing images using histogram
Equalization all of these are very
useful operations if you want to perform
for example image segmentation okay
so let's jump into our spider IDE which
is part of anaconda distribution and
first of all I should have actually
mentioned OpenCV I think this is the
first video where we are talking about
open CV if you want to install OpenCV if
you do not have it already this is where
the biggest issue is with open CV once
you have it in stir
it's very easy to use it OpenCV exists
in you know see it also in Python so for
Python in your command prompt you can do
pip install and to install OpenCV this
is a bit tricky open CV - Python this
actually works at at least this is how I
installed it on my system here so it's
pip install OpenCV - python ok to import
it we are going to import it using
import c v2 this is how we import open
CV in open CV to open an image let's say
let's say we're gonna open a CV - dot M
read again this M read is pretty much
common in most packages as you as you
probably can imagine and the format is
pretty much the same C v2 dot MB I'm
gonna import an image called RGB Y dot
jpg okay
so this is we'll see the will will see
the image in a minute so RG b y dot jpg
and let's go ahead and first print i am
g dot shape okay i just want to print
the shape and let's see what it is it is
a 586 by 415 image and three channels so
you can see right away that this is a a
color image now you can read images as
gray or as color so if you actually say
0 right next to it now let's run this a
586 by 4:15 so this means 0 basically
means we are reading our image as a gray
level image if i put 1 it should be 586
415 and three this means we are reading
it as a color image let's see what
happens if I put two it says 586 by 4:15
to is basically nothing but the native
image without making any changes
including the alpha Channel I've never
tried to in so I don't want to
experiment in this video but to is
basically if you have also want to
import the alpha Channel okay so now we
are importing our color image so let's
let's actually have a look at this image
so I'm going to open this image and I
let me okay so we can see exactly what
what we are looking for okay so that
image is loaded on my different screen
so let me go ahead and drag it so this
is nothing but blue on the top left
yellow here on the top right green and
red let's go ahead now and look at how
to extract or how to look at individual
pixel values okay so to look at
individual pixel values again this is
nothing but a a numpy array okay so the
image when you import it into cb2 is
nothing but a numpy array so to look at
pixel values all you need to do is let's
say let's look at top left so all you
need to do is just type okay this is my
image and I just want to look at zero
comma zero okay so what is this this is
my top left pixel so top left pixel is
38 3 and 0 okay
so typically when you look at when you
look at by the way this is a 8 bit image
so the values go from 0 to 255 now this
is saying this is 38 3 and 0 so our
typical convention is that this is a RGB
image right color image red green and
blue so red is 38 green is 3 and blue is
0 which means on the top left we should
have red but I have blue on the top left
in OpenCV when you read color images by
default the convention is not RGB it is
B G or blue green and red and please
remember this otherwise you can mess up
your calculations you can verify this by
opening an image using matplotlib pipe
lot and then when you just do not
convert anything you open it then all
the colors look pretty weird so you can
do that exercise by yourself just to
show you one more time let's now go to
our top right image or it image is 586
by 4:15 so if I just do 0 and 400 this
should give me top right and well let's
do all four corners by not and the other
one would be 580 this would be bottom
and finally let's do the bottom right
and you can guess what the coordinates
would be 580 by 400 okay it's hundred
something but this is close enough we
should so let me open up the image and
now you look at this top left is 38 I'm
in blue and if you look at the top right
this seems to be no blue at all but
green plus red okay which is nothing but
yellow bottom left is no blue no red and
green b:g are again the whole point of
I'm trying to make here is that the
images when you read it in open CV it is
BG are not RGB okay so I hope we got the
point and by the way let me go ahead and
zoom the left hand side so you can
easily see this without me zooming and
digitally so once we have the image so
let me go ahead and delete this and now
let's see how to split we have a RGB
image now how can we split the channels
well because this is an umpire array we
know how to do that right I mean blue
equals nothing but my image and then I
want to use all my pixels from X all my
pixels from Y but in Z okay in the color
space I just want 0 this is nothing but
all of my blue blue pixels so if I
actually
I'm actually thinking about whether I
should talk about how to show images
using cb2 let's go ahead and do that so
far until now we've used matplotlib
pie plot module to have a look at images
to have a look at histograms and
everything so let me introduce how to I
mean see v2 dot I M show this is how you
can actually visualize images I like
this better than pie part because in pie
plot the output is only shown within my
console
it can be very tiny sometimes but the
good thing with pie plot is you can
arrange you know things in in a specific
way so you can it's it looks very
professional let's say but with CV to
dot M show it uses whatever your native
default photo viewer is on Windows in
fact I don't know what it is on Windows
10 I know they we have some sort of a
photo view or on every window so we'll
see so it uses the native windows photo
viewer okay now one thing you need to be
careful about when you do see v2 dot M
show which I'm going to talk about in a
second blue image let's say our blue
this is not blue image this is just blue
pixels okay so our blue pixels is
nothing but blue if I go ahead and run
this it's gonna hang the reason is
whenever you have CV to M show go ahead
and blindly put I think that K is
uppercase in weight key and always put
this CV to dot I'm gonna tell exactly
what this is
destroy all and W is probably uppercase
I think so so the weight t is nothing
but after M shell is done it looks for
how long do you want the window to be
opened and the weight key basically
tells if it is 0 that means indefinitely
until I close it until I hit hit the
close and if you put like 100 it is 100
milliseconds the window is up for 100
milliseconds it's gone after 100
milliseconds what do you want to do I
want to destroy
windows that's exactly this next line is
okay so let's go ahead and run this and
the window let me move this image from
the other screen so this is what I have
here okay so you can see that I'm only
looking at blue pixels in fact there
seem to be a few blue pixels right along
this line yeah so let's go ahead and do
one more actually let's not do all just
do one more let's do red pixels so you
can see so red pixels and show me red
but then we haven't defined what right
is okay so let me copy this and red is
nothing but BG all right B equals to
zero green is one red is two so all I
need to do is two right there and this
is right this is how you can extract and
why not you know let's go ahead and do
one so that way we can see all three and
let's add another line here so we can
see green pixels and what do we want to
see green let's run this and now we
should see again let me drag these other
windows so these are red and the green
okay so if you look at the original
image not sure if I have it open anymore
if you look at the original image we
have yellow on the top right which is
nothing but a mixture of green and red
so you see red right there bottom right
is red so you see right here
bottom left was green so you see green
right here okay so and blue we only have
it at one location right there
so I'm splitting so now instead of M
show I could have just in saved that you
know we can save these images so this is
how you can extract you can separate the
channels but there is an easier way in
fact so after all this there is an
easier way if you actually do see v2 dot
split your image this image is split
into red green up oops sorry
I make this mistake blue green and red
so you can assign this to blue green red
okay so blue green red equals two and
let me go ahead and delete these lines
this is equivalent to whatever I have
defined before okay so if I go ahead and
run this one more time you see exactly
the same result
okay so extracting the channels we
looked at it a couple of ways one using
the traditional numpy approach where we
are only slicing or numpy array and the
other one is just using the built-in
function split with the function within
the cv2 okay so this is how we can
extract and by the way once we have this
once we have these split let's say you
do some image segmentation or whatever
and then you end up with three different
images you know and you want to combine
them into RGB channels and get like one
image one color image that has all these
then exact opposite of split we have
something called merge so you can just
say image merged equal to c v2 dot
merged and this would be so when we
merge this we need to tell exactly what
are the three images that we are merging
so obviously in this case blue green and
red this is the name of my Emma you know
my my images here just so I'm not
confusing you these are just names it
doesn't have to be blue green and red
okay it can be BG or if you want in
which case this would be B G R okay so
I'm not just typing some color names
here it's whatever variable name you
give give here for each of these
channels that's what we are trying to
combine here okay so now instead of all
of these showing all of this let's
actually just say combined image or
merged image is nothing but image so
this merged operation takes BGR and
stacks the images accordingly so you can
clearly see see v2 has no
attribute emerged CV to my white shirt
oh it's merch okay so let's run it there
it is
here is the image and it's a smashed
this is just the reverse we split the
channels and we merge them back together
okay so this is merging images and so on
and now let's look at a resizing images
so I promised to show you five things
and the second one would be resizing
images so let me delete all of this and
let's go ahead and read a small image
one of the small images I have is effect
okay you'll see that in a second and
resized is nothing but you know let's
assign a variable called resized and the
way you do that is see v2 dot resize
okay this is it
most operations in OpenCV even the
advanced ones you know typically are one
line sometimes we have to do some
pre-processing to get to that one line
but it's very easy to implement so in
this case our input image is image and
there are some parameters you know you
can actually look at when it's when you
do resize it should come up with I'm not
sure it should have actually prompted
with okay what input parameters can
actually go in there if you look at the
documentation you know that there is one
there is another one that you can input
and the other one that you need to do is
FX I don't know why it's not prompting
what is the scaling factor in X there
are two ways you can actually zoom our
scale you can give actual dimensions of
to what size you want to scale the image
or resize the image or the scaling
factor so in this example I'm just
giving a scaling factor in Y and then
there is something called interpolation
okay when you expand the image by two
times how do you want to fill the pixels
in between because you're adding pixels
right so there are various algorithms
again you can go ahead and look at you
know the documentation and one is the
one I know there is like in Terraria
I believe in Terraria is when you string
the image that works very well
interlinear is when you zoom in or inter
pubic is also so you can either use
inter cubic or inter inter linear again
you can look at documentation online so
all you need to do is in upper cases in
Turk Ubik I think this is pretty much it
so let's do CV to dot M show and let's
see original image which would be IMG
and let's also look at let me copy that
line let's also look at resized image we
call that resized and do not ever forget
this CV to dot
wait he I shouldn't have deleted that CV
to dot B is not uppercase okay that
should work now so let's go ahead and
run this
there is the small monkey and on my
other screen
it's the resized image resized image in
X it's two times ya if I move this this
is one that's two invites two times one
and two right there okay so if I look at
the image size it should be it should be
too tired it should be right there
resized image is 660 by four forty and
our regular image is three thirty by two
220 okay you can look at that in
variable Explorer so resizing image is
pretty much straight forward you know
it's as easy as easy as that so let me
cover two more topics which probably
makes this a relatively long video in
fact let's do this I'm gonna record
another video in fact I thought of
actually covering the image smoothing I
mean be noising edge detection and
something else saying that is going to
take a while and nobody likes to watch
one hour video and learn only five
things in fact nobody wants to watch
five hour I mean one hour video so it's
almost twenty minutes so let me go ahead
and stop here and create another video
for the next topic which is
smoothing or denoising edge detection
and image enhancements these are all the
pre-processing functions thank you very
much
Посмотреть больше похожих видео
26 - Denoising and edge detection using opencv in Python
Konsep Dasar Citra Digital - Perkuliahan Pengolahan Citra Digital #01
Introduction to OCR (OCR in Python Tutorials 01.01)
Python ANPR with OpenCV and EasyOCR in 25 Minutes | Automatic Number Plate Recognition Tutorial
Image Representation
Plant Leaf Disease Detection Using CNN | Python
5.0 / 5 (0 votes)