AI13 Opencv

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Chapter 13

OpenCV
▪Learning contents
13.1 Image data
13.2 Image visualization using Matplotlib
13.3 OpenCV module
13.4 Filters for image processing
13.5 Manipulating image data
13.6 Question
13.1 Image data

Big Data Analytics &


Artificial Intelligence
Starting with Python
Image data
Image comes from
the Latin imago,
which means some-
thing artificially
made to resemble
something.

• Digital image data is divided into a bitmap method


in which points representing colors are gathered to
form a single image, and a vector image in which
points, curves, and planes are expressed in mathe-
matical expressions.
Image data

Two-dimen- 0 to black , If 1 is
sional arrays 0 displayed in
and 1 white, it becomes
this image.

An image representing one pixel as 0 and 1


Image data
• An image in which one pixel contains only brightness information with-
out hue is called grayscale. At this time, a pixel can be expressed with
one channel that expresses only brightness information.
• Usually, one channel is represented by one byte, and the picture shows a
grayscale image from level 0 to level 255.
One byte can express 256
levels of brightness.
The more bytes you have,
the more colors you can ex-
press.

0 50 88 167 209 255

Red
If one pixel is expressed
Green using the Red + Green +
Blue channels, a color
Blue pixel can be expressed.
Image data

What Is the Alpha Channel in PNG


Images? (makeuseof.com)

• The image looks like a two-dimensional matrix with w columns hori-


zontally and h rows vertically. When pixel values are transferred and
saved to a file as they are, a name ending with the extension .bmp,
which usually means bitmap, is used.
• You can compress it and make it into a jpg, gif, png file.
Image data

• There are many ways to express color, but the representative


way is to mix the intensities of red, green, and blue, which
represent the light spectrum. This method is called the RGB
method.
13.2 Image visualiza-
tion using Matplotlib

Big Data Analytics &


Artificial Intelligence
Starting with Python
Image visualization using Matplotlib

• The mandrill monkey has various colors on its face, so it is often used
as an example image.

Image data is an array


with intensity values
of Red, Green, and
Blue colors
Image visualization using Matplotlib

• Use imshow(), a drawing function


of the pyplot module. And call
pyplot's show() function to draw
the window. Confirm that the im-
age read in the matplotlib window
is drawn as shown in the figure.
13.1 OpenCV module

Big Data Analytics &


Artificial Intelligence
Starting with Python
OpenCV module
cv2.IMREAD_GRAYSCALE
Read the file as a property and cv2.IMREAD_COLOR
display it. Read the file as a prop-
erty and display it.

When dealing with


images, an image
tool called OpenCV
is better than mat-
plotlib.
more convenient

waitkey() function allows
users to display a window for
given milliseconds or until
any key is pressed. It takes
time in milliseconds as a pa-
rameter and waits for the
given time to destroy the win-
dow, if 0 is passed in the argu-
ment it waits till any key is
pressed.
OpenCV module
You can
draw straight
lines or
squares on
In OpenCV,
the picture.
colors repre-
sent their com-
ponent values
in the order of
Blue, Green,
and Red.
OpenCV module
OpenCV module
addWeighted Function:
img = cv2.addWeighted(source1, alpha, source2, beta, gamma[, dst[, dtype]])

(dst = src1*alpha + src2*beta + gamma)

OpenCV: Operations on arrays


OpenCV module
addWeighted Function:
img = cv2.addWeighted(source1, alpha, source2, beta, gamma[, dst[, dtype]])

(dst = src1*alpha + src2*beta + gamma)

This function
allows you to
adjust the
transparency
using the track
bar.
OpenCV module

Combine the
two images us-
ing the top
trackbar bar.
OpenCV module
• Using a mask, only a specific part of an image can be left or disappeared.

With white background


All color components are 1

In the case of black


All color components are 0

Mask image

• The inside of the image to be used as a mask is expressed in black and the background
is white. Therefore, it can be understood that all internal color components are set to 0
and the background is set to 1.
OpenCV module
• The black part is zeroing out whatever the background image is. And the 1 of
the mask, that is, the white part, is the background image value and deter-
mines the color of the corresponding pixel.

• This means that the background image passes through transparently.

• In other words, the bitwise AND operation covers the background image with
the black part of the mask.

Background image Mask Bitwise AND


Filters for image processing
Bitwise operators and their meaning

Operator Meaning Explanation


& Bitwise AND 1 if both corresponding bits are 1,
otherwise 0
| Bitwise OR 1 if either corresponding bits are 1,
otherwise 0
^ Bitwise XOR 1 if two corresponding bits have same value,
otherwise 0
OpenCV module
Masking with
bitwise AND op-
eration : Black is
always black
Masking with
bitwise XOR
operation: as
the XOR
AND

color of the
se

two images
bitwi

background image
Bitwise XOR
bitw
ise O
R

Masking with bitwise OR


operation : Black is uncondi-
tionally the background im-
age color
mask image
OpenCV module
• To extract only pixels of specified color, the HSV method is more suit-
able than the RGB or BGR color mode. The figures below show how
the color changes according to the change in the color value when the
saturation and brightness are set to constant values.
When the color value
is stretched from 0 to
360, the range of blue
is approximately 160
to 260 degrees.
OpenCV module
• The ranges that OpenCV manage for HSV format are the follow- ing:
– For HSV, Hue range is [0,179],
– Saturation range is [0, 255]
– Value range is [0, 255].

Find the pixel between the


lower and upper boundary
13.4 Filters for image
processing

Big Data Analytics &


Artificial Intelligence
Starting with Python
Filters for image processing

A kernel that aver-


Input Kernel Output image ages 8 adjacent pix-
image(src) (dst)
els and itself
Filters for image processing
Filter2D

Ddepth:  means desired depth of the destination image. It has information about
what kinds of data stored in an image, and that can be unsigned char ( CV_8U),
signed char (CV_8S), unsigned short (CV_16U), and so on...
Filters for image processing
Filter2D
Image my_mask

Only the part


highlighted in
red is extracted
from the image
on the left.

• See the black and white image on the right in the picture above. The white
part is where the original image was blue. By using the extracted image as
a mask, only blue pixels can be picked out from the original image.
• Since the two images have different shapes, bitwise_and()cannot be ap-
plied.
Filters for image processing
Filter2D

kernel 1

kernel 2
Filters for image processing
Blur filter
Filters for image processing
GaussianBlur

The Gaussian filter puts a


lot of weight in the central
part and reduces the
weight as you move away
from the center.

sigmaX is the standard deviation in the x-axis


direction, and the shape of the Gaussian func-
tion changes depending on this value.
Filters for image processing
GaussianBlur
Filters for image processing
Removing noise with various blur filters
Image with lots of
small bouncing dots
• OpenCV provides various blur functions
such as medianBlur() and bilateralFilter()
as well as box filter and Gaussian filter..
• medianBlur:
o Find the median value median in the
filter area of size nn and set the pixel
to that value
o Effective at erasing small dots (salt
and pepper noise) from an image that
pop out relative to its surroundings.
Filters for image processing
Removing noise with various blur filters
Filters for image processing
Removing noise with various blur filters

In OpenCV, cv2.bilateralFilter () is useful for


effectively removing noise from an image
without disturbing the edges of the image.
Filters for image processing
13.5 Manipulating im-
age data

Big Data Analytics &


Artificial Intelligence
Starting with Python
Manipulating image data

• When handling images, when it is necessary to classify val-


ues that are higher than a certain threshold as valid values
and values that are not valid as invalid values.
• Binarization: Using OpenCV's threshold() function,
– You can do binarization, or you can change all values that
exceed a certain threshold to a certain value.
Manipulating image data
Original image
(input)

• cv2.THRESH_BINARY - sets the pixel value that satisfies the condition to the
maximum value and the pixel value that does not satisfy the condition to be 0
• cv2. THRESH_BINARY_INV - sets pixel values that satisfy the condition to 0,
and sets the pixel value that does not satisfy the condition to the maximum value
• cv2. THRESH_TRUNC- Sets the pixel value that satisfies the condition as the
maximum value, otherwise sets it to the original value.
• cv2. THRESH_TOZERO - Set pixel values that satisfy the condition as original
values, and pixel values that do not satisfy the condition as 0
• cv2. THRESH_TOZERO_INV - Set pixel values that satisfy the condition to 0,
and set the pixel values that do not satisfy the condition to the original value
Manipulating image data
Reference

OpenCV 3 Image Thresholding and Segmentation - 2020 (bogotobogo.com)


Manipulating image data

If you apply binarization to


color_image, you can get an im-
age with only a few colors like a
cartoon.
Manipulating image data

• The thresholding method we have seen so far does not con-


sider how a pixel relates to the value of its surrounding pix-
els.
• Sometimes the threshold is set based on the value of neigh-
boring pixels rather than a fixed threshold.
– Finding the outlines of an image
– A contour is a pixel with a value that jumps out com-
pared to its surroundings.
– This threshold is called an adaptive threshold.
– Using the adaptiveThreshold() function
Manipulating image data
The maximum value to apply to
pixels that satisfy the condition block size
Original image How to apply a Correction value
(input) threshold of threshold

• ADAPTIVE_THRESH_MEAN_C: Average value of pixels within


blocksize blocksize area centered on pixels - C (last factor) as thresh-
old
• ADAPTIVE_THRESH_GAUSSIAN_C : The value obtained by sub-
tracting C (the last factor) value from the average value of applying
Gaussian weights to pixels within the blocksize blocksize area cen-
tered on the pixel is set as the threshold value. Since Gaussian weight-
ing is applied, pixels close to the currently inspected pixel are treated
as more important.
Manipulating image data

https://docs.opencv.org/3.4.3/d7/d4d/tutorial_py_thresholding.html
Manipulating image data
13.6 Question

Big Data Analytics &


Artificial Intelligence
Starting with Python
Question
Convert the original image to the right image?
Thank you

You might also like