0% found this document useful (0 votes)
1 views30 pages

UNIT-1-CV-RIT-CSD

This document provides an overview of computer vision, detailing its definition, history, goals, and applications. It distinguishes between image processing and computer vision, highlighting the challenges faced in implementing computer vision technology. Additionally, it discusses the formation of images, types of images, and pixel manipulation, emphasizing the significance of high-quality data and proper planning in successful computer vision projects.

Uploaded by

temporaryoppoa12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views30 pages

UNIT-1-CV-RIT-CSD

This document provides an overview of computer vision, detailing its definition, history, goals, and applications. It distinguishes between image processing and computer vision, highlighting the challenges faced in implementing computer vision technology. Additionally, it discusses the formation of images, types of images, and pixel manipulation, emphasizing the significance of high-quality data and proper planning in successful computer vision projects.

Uploaded by

temporaryoppoa12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER VISION (UNIT-I)

UNIT-1

SYLLABUS: Introduction to computer vision - Image Processing VS Computer Vision -


Problems in Computer Vision – Applications of Computer Vision.

Introduction to images - How images are formed - Digital Image - Image as a Matrix -
Manipulating Pixels - Displaying and Saving an Image - Display Utility Functions - Color Image
- Image Channels - Splitting and Merging Channels - Manipulating Color pixels - Images with
Alpha Channel.

What is Computer Vision?

Computer vision is a field of artificial intelligence (AI) that enables computers and systems to
derive meaningful information from digital images, videos and other visual inputs — and take
actions or make recommendations based on that information.

Computer vision works much the same as human vision. Human sight has the advantage of
lifetimes of context to train how to tell objects apart, how far away they are, whether they are
moving and whether there is something wrong in an image. Computer vision trains machines to
perform these functions, but it has to do it in much less time with cameras, data and algorithms
rather than retinas, optic nerves and a visual cortex.

Vision is the task of watching or seeing. When we see things. Our eyes (sense organ for used for
vision) capture image, then pass the information to brain (interpreting device). The brain then
interprets the image gives us the meaning of what we are seeing. Similarly, in computer vision,
cameras serve as sensing devices and computers act as interpreting devices to interpret the image
captured by cameras.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

History of Computer vision: in the summer of the year 1966, Seymour Papert and Marvin
Minsky at MIT Artificial Intelligence group started a project titled summer vision project. The
aim of the project was to build a system that can analyze a scene and identify objects in the
scene.

In the 70s, taking ideas from studies of the Cerebellum, hippocampus and cortex for human
perception, David Marr, a neuroscientist at MIT, set up the building blocks for the modern
computer vision and thus is known as the father of the modern Computer Vision.

Goals of Computer Vision:

• To produce a representation of objects by processing images captured through cameras.

• To make useful and intelligent decisions based on sensed images.

• To construct 3D images from 2D images.

Related areas of computer vision

The related areas are,

• Computer Graphics

• Image Processing

• Pattern recognition

• Artificial Intelligence

• Virtual Reality

Examples of computer Vision:

• A robot cab use several image frames per second produced by one or two video cameras
to produce a map of its surroundings for path planning and obstacle avoidance.

• A Printed circuit inspection system which may take one picture per board on a conveyer
belt and produce binary image flagging possible faulty soldering points on the board.

• A zip code reader takes single snapshots of envelops and translates handwritten number
into an ASCII string.

Image Processing VS Computer Vision

Image processing and Computer Vision both are very exciting field of Computer Science.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Computer Vision:

In Computer Vision, computers or machines are made to gain high-level understanding from the
input digital images or videos with the purpose of automating tasks that the human visual system
can do. It uses many techniques and Image Processing is just one of them.

Image Processing is the field of enhancing the images by tuning many parameter and features of
the images. So Image Processing is the subset of Computer Vision. Here, transformations are
applied to an input image and the resultant output image is returned. Some of these
transformations are- sharpening, smoothing, stretching etc.

Now, as both the fields deal with working in visuals, i.e., images and videos, there seems to be
lot of confusion about the difference about these fields of computer science.

Image Processing Computer Vision

Computer vision is focused on extracting


Image processing is mainly focused on
information from the input images or videos to
processing the raw input images to enhance
have a proper understanding of them to predict the
them or preparing them to do other tasks
visual input like human brain.

Image processing uses methods like


Image processing is one of the methods that is used
Anisotropic diffusion, Hidden Markov
for computer vision along with other Machine
models, Independent component analysis,
learning techniques, CNN etc.
Different Filtering etc.

Image Processing is a subset of Computer


Computer Vision is a superset of Image Processing.
Vision.

Examples of some Image Processing


Examples of some Computer Vision applications
applications are- Rescaling image (Digital
are- Object detection, Face detection, Hand writing
Zoom), Correcting illumination, Changing
recognition etc.
tones etc.

Problems in Computer Vision or Challenges in Computer vision

CV Technology is revolutionizing many industries, including healthcare, retail, automotive, etc.


As more companies invest in CV technology, the global market is projected to multiply 9 times
by 2026 to $2.4 Billion.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

However, implementing computer vision in your business can be a challenging and expensive
process, and improper preparation can lead to CV and AI project failure. Therefore, business
managers need to be careful before initiating computer vision projects. Some of the key
challenges / problems are,

 Inadequate hardware

Computer vision technology is implemented with a combination of software and hardware. To


ensure the system’s effectiveness, a business needs to install high-resolution cameras, sensors,
and bots. This hardware can be costly and, if suboptimal or improperly installed, can lead to
blind spots and ineffective CV systems.

 Poor data quality

Poor Quality

High-quality labeled and annotated datasets are the foundation of a successful computer vision
system. In industries such as healthcare, where computer vision technology is
being abundantly used, it is crucial to have high-quality data annotation, and labeling since the
consequences of inaccurate computer vision systems can be significantly damaging. For
example, many tools built to catch Covid-19 are failed due to poor data quality.

Lack of training data

Collecting relevant and sufficient data can have various challenges. These challenges can lead to
a lack of training data for computer vision systems. For example, gathering medical data is a
challenge for data annotators. This is mainly due to the sensitivity and privacy aspects of
healthcare data. Most medical images are either of sensitive nature or are strictly private and are
not shared by healthcare professionals and hospitals. Additionally, it is possible that the
developers do not have the resources to collect sufficient data.

 Weak planning for model development

Another problem can be weak planning for creating the ML model that is deployed for the
computer vision system. During the planning stage, executives tend to set overly ambitious
targets, which are hard to achieve for the data science team.

Due to this, the business model:

 Does not meet business objectives


 Demands unrealistic computing power
 Becomes too costly
 Delivers insufficient accuracy and performance

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

 Object detection challenges

Viewpoint Variation: One of the biggest difficulties of object detection is that an object viewed
from different angles may look completely different. For example, images of a cake look
different from different sides. Thus, the goal of detectors is to recognize objects from different
viewpoints.

Deformation: The subject of computer vision analysis is not only a solid object but also bodies
that can be deformed and change their shapes, which provides additional complexity for object
detection. For example; a football player may change his pose at different [Link], the images of
the football players are different in different poses. If the object detector is trained to find a
person only in a standing or running position, it may not be able to detect a player who is lying
on the field or preparing to make a maneuver by bending down.

Occlusion: Sometimes objects can be covered by other things, which makes it difficult to read
the signs and identify these objects. For example, in the first below image, a cup is covered by
the hand of the person holding this cup.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Illumination Conditions: Lighting has a very large influence on the definition of objects. The
same object will look different depending on the lighting conditions. the less illuminated space,
the less visible the objects are. All of these factors affect the detector’s ability to define objects.

Cluttered or textured background: Objects that need to be identified may blend into the
background, making it difficult to identify them. For example, the below picture shows a lot of
items, the location of which is confusing when identifying scissors or other items of interest. In
such cases, the object detector will encounter detection problems.

Applications of Computer Vision

Some of the applications of computer vision are given below.

 Human Face identification/ Biometrics authentication

This is especially relevant for facial recognition technology. Not only does the computer
recognize human faces in general, it can also recognize the unique faces of particular individuals.
Take facial recognition phone lock systems: your phone knows your face and can distinguish you
from any other person.

Facial recognition is one of the better known computer vision advantages that does not just
protect your phone; it can also be used in retail, banking, transportation, and other industries as a
security measure. Your face is a primary visual identifier by which you get recognized by other
people, and now, computers too can recognize your face to give you access to your private and
sensitive data.

 Image database query

Image querying refers to the problem of finding objects that are relevant to a user query within
image databases

 Inspecting products

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

A computer vision system can detects defects, contaminants, functional flaws, and other
irregularities in manufactured products. Examples include inspecting tablets of medicine for
flaws, checking safety seals, caps, and rings on bottles, verify proper label placement, Count
Items in packages etc.

 Healthcare

Computer vision has contributed significantly to the development of health technology.


Automating the process of looking for malignant moles on a person's skin or locating indicators
in an x-ray or MRI scan is only one of the many applications of computer vision algorithms.

Image processing has proven vital for the identification of abnormalities in organ scans in
clinical environments. Doctors can use computer vision advantages in different situations,
including for cancer identification. This can help the physicians to process the collected patient
data much faster and give more precise diagnoses.

Additionally, the AI can help in identifying the amount of blood loss in women after giving birth.
With the new technology, the doctors can adjust treatment in accordance with each particular
patient’s condition and prescribe relevant treatment. The use of computer vision will soon
expand to other areas enhancing the quality of care and life expectancy of the patients.

 Optical character recognition

Optical Character Recognition (OCR) is the process of detecting and reading text in images
through computer vision. Detection of text from document images enables Natural Language
Processing algorithms to decipher the text and make sense of what the document conveys.

Furthermore, the text can be easily translated into multiple languages, making it easily
interpretable to anyone. OCR, however, is not limited to the detection of text from document
images only. Novel OCR algorithms make use of Computer Vision and NLP to recognize text
from supermarket product names, traffic signs, and even from billboards, making them an
effective translator and interpreter.

 Analyze satellite images

Satellite image processing and analysis is one of the significant computational methods which
finds application in military, agriculture, natural disaster prevention, natural resource
identification and so forth

 Self-driving cars/ Autonomous vehicles

With the use of computer vision, autonomous vehicles can understand their environment.
Multiple cameras record the environment surrounding the vehicle, which is then sent into

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

computer vision algorithms that analyzes the photos in perfect sync to locate road edges,
decipher signposts, and see other vehicles, obstacles, and people. Then, the autonomous vehicle
can navigate streets and highways on its own, change direction around obstructions, and get its
passengers where they need to go safely.

 Natural language processing

As a part of the Artificial Intelligence domain, CV is intertwined with the concept of Natural
Language Processing too. Computers can now read the written or typed text. This can be used for
processing contracts and finding any errors or inconsistencies in the written text.

 Deep fake detection

With fake news taking over the media space, it becomes harder for the average person to
determine what’s real and what’s not. Deep fakes are becoming so good, even the experts might
fail to identify them. The system can identify the elements of photos and videos that have been
manipulated in any way. It may detect fake product review, fake customer complaint, fake news
etc.

 Law enforcement and defense

Technology can be extremely helpful in ensuring public security. Of course, there’s a


controversy surrounding public surveillance, but the fact remains that this technology can help
detect suspicious individuals, dangerous criminals, and terrorists in public places. The
technology can also be used in defense, helping the military identify weapons of mass
destruction and other hazardous objects over vast areas.

Introduction to images:

What is an image?

An image is defined as a two-dimensional function,F(x,y), where x and y are spatial coordinates,


and the amplitude of F at any pair of coordinates (x,y) is called the intensity of that image at that
point. When x,y, and amplitude values of F are finite, we call it a digital image.

In other words, an image can be defined by a two-dimensional array specifically arranged in


rows and columns.

Digital Image is composed of a finite number of elements, each of which elements have a
particular value at a particular location. These elements are referred to as picture elements, image
elements, and pixels. A Pixel is most widely used to denote the elements of a Digital Image.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Types of an image

• BINARY IMAGE– The binary image as its name suggests, contain only two pixel
elements i.e 0 & 1,where 0 refers to black and 1 refers to white. This image is also known
as Monochrome.

• 8 bit COLOR FORMAT– It is the most famous image format. It has 256 different shades
of colors in it and commonly known as Grayscale Image. In this format, 0 stands for
Black, and 255 stands for white, and 127 stands for gray.

• 16 bit COLOR FORMAT– It is a color image format. It has 65,536 different colors in it.
It is also known as High Color Format. In this format the distribution of color is not as
same as Grayscale image.

A 16 bit format is actually divided into three further formats which are Red, Green and Blue.
That famous RGB format.

Image as a Matrix (how an image is represented?)

As we know, images are represented in rows and columns we have the following syntax in which
images are represented:

The right side of this equation is digital image by definition. Every element of this matrix is
called image element, picture element, or pixel.

A grayscale image is a 2-dimensional array of numbers. An 8-bit image has entries between 0
and 255. The value 255 represents a white color, and the value 0
represents a black color. Lower numbers translate to darker pixels, while
higher numbers translate to lighter pixels. For an image that has (m * n)
pixels (i.e., “picture elements”), we represent that image using a matrix of
size m*n. The entries of the matrix indicate the pixel value of the
corresponding part of the image. Example: This table represents an image
that has 4 * 5 pixels.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Color images can be stored in a similar fashion to a grayscale image. Instead of one number (0-255) per
pixel, one stores three numbers per pixel – these three numbers denote the “amount” of red, “amount”
of green, and “amount” of blue in each pixel. These three numbers can be used to depict a wide range
of colors.

Manipulating pixels

Once images are represented as matrices, we can describe many transformations of those images using
basic matrix operations!

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Image formation (How image are formed?)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Above figure shows the reflection of a ray of light at the object surface. The object surface
reflects the light in all directions.

The ray of light from the surface patch is reflected in the direction of the human eye and
projected on the retina: the inner surface of the eye that contains the light sensitive cells which
send signals to brain for interpretation.

The optical principle of the human eye is the same as for any optical camera, be it a photo
camera or a video camera. The most simple (but surprisingly accurate) model for such an optical
camera is the pinhole camera. This is just a box (you can build one yourself about the size of a
shoe box) with a small hole (about half a millimeter in diameter; the easiest way to make one is
to use aluminum foil for the side where you have to make the hole) and a photosensitive layer on
the opposite side (for the homebuilt pinhole camera you can use a translucent piece of paper: See
image below for pinhole camera.

Light reflected from an object travels in a straight line through the pinhole and hits the
photosensitive surface. The use of optical lenses is a physical trick to enlarge the hole to get
more light into the camera without blurring the projected image.

The projection of the 3D world onto the 2D retina of the camera is the cause of many problems
in the analysis of the 3D world based on 2D images. In the projection, information about the 3D
structure is lost (see Fig. below showing an image of an `impossible’ 3D object). Reconstruction
of the 3D structure from several images of the same scene or from a video sequence is the goal in
computer vision.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Where the rays of light hit the retina we are able to measure the electromagnetic energy and
these measurements, as a function of the position on the retina, provide a representation of the
image. If we think of the retina as a plane, all positions on the retina are given by (x,y)
cordinates. This results in a function f whose value f(x,y) at position (x,y) on the retina (or
image plane or image domain) is proportional to the amount of measured energy.

Digital Image:

A digital image is a representation of a real image as a set of numbers that can be stored and
handled by a digital computer. In order to translate the image into numbers, it is divided into
small areas called pixels (picture elements). For each pixel, the imaging device records a
number, or a small set of numbers, that describe some property of this pixel, such as its
brightness (the intensity of the light) or its color. The numbers are arranged in an array of rows
and columns that correspond to the vertical and horizontal positions of the pixels in the image.

Digital images have several basic characteristics. One is the type of the image. For example, a
black and white image records only the intensity of the light falling on the pixels. A color image
can have three colors, normally RGB (Red, Green, Blue) or four colors, CMYK (Cyan, Magenta,
Yellow, blacK). RGB images are usually used in computer monitors and scanners, while CMYK
images are used in color printers. There are also non-optical images such as ultrasound or X-ray
in which the intensity of sound or X-rays is recorded. In range images, the distance of the pixel
from the observer is recorded. Resolution is expressed in the number of pixels per inch (ppi). A
higher resolution gives a more detailed image. A computer monitor typically has a resolution of
100 ppi, while a printer has a resolution ranging from 300 ppi to more than 1440 ppi. This is why
an image looks much better in print than on a monitor.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

The color depth (of a color image) or "bits per pixel" is the number of bits in the numbers that
describe the brightness or the color. More bits make it possible to record more shades of gray or
more colors. For example, an RGB image with 8 bits per color has a total of 24 bits per pixel
("true color"). Each bit can represent two possible colors so we get a total of 16,777,216 possible
colors. A typical GIF image on a web page has 8 bits for all colors combined for a total of 256
colors. However, it is a much smaller image than a 24 bit one so it downloads more quickly. A
fax image has only one bit or two "colors," black and white. The format of the image gives more
details about how the numbers are arranged in the image file, including what kind of
compression is used, if any. Among the most popular of the dozens of formats available are
TIFF, GIF, JPEG, PNG, and Post-Script.

How images are displayed and saved?

We have two popular formats in which images are saved – Grayscale and RGB format.

How B&W or Grayscale images are stored in a Computer?

Let’s take an example. Here we have taken a black and white image, also known
as a Grayscale image.

This is the image of a number 8. Now, if we zoom in further


and if you look closely you can see that the images getting
distorted and you would see some small square boxes on this
image. These small boxes are called Pixels. We often use the
term- the dimension of the image is X * Y. What does that
actually mean? This means that the dimension of the image is
simply the number of pixels across the height(x) and width(y)
of the image. In this case, if you count, it would be some
number (such as 24 pixels across the height and 16 pixels
across the width). Hence the dimension of this image will
be 24 x 16. Although we see an image in this format the
computer store image in the form of numbers-

Each of these pixels is denoted as the numerical value and


these numbers are called Pixel Values. These pixel values
denote the intensity of the pixels. For a grayscale or b&w
image, we have pixel values ranging from 0 to 255.

The smaller numbers closer to zero represent the darker


shade while the larger numbers closer to 255 represent the
lighter or the white shade.

So every image in a computer is saved in this form where


you have a matrix of numbers and this matrix is also known
as a Channel.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

So, what is the shape of this


matrix? It is same as the number
of pixel values across the height
and width of the image. In this
case, the shape of the matrix
would be 24 x 16.

Now let’s quickly summarize the


points that we’ve learned so far-

Images are stored in the form of a


matrix of numbers in a computer
where these numbers are known
as pixel values.

These pixel values represent the


intensity of each pixel.

0 represents black and 255


represents white.

The matrix of numbers is known


as the channel and in the case of a
grayscale image, we have only
one channel.

How Colored images are stored on a computer?

Let’s take an example of a colored image, this is


an image of a dog-

This image is composed of many colors and


almost all colors can be generated from the three
primary colors- Red, Green, and Blue. We can say
that each colored image is composed of these
three colors or 3 channels- Red, Green, and Blue-

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

This means that in a colored image the number of matrices or the number of channels will be
more. In this particular example, we have 3 matrices- 1 matrix for red known as Red channel-

Another metrics for green known as the Green channel-

and finally a matrix for the blue color also known as the Blue channel.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Each of these metrics would again have values ranging from 0 to 255 where each of these
numbers represents the intensity of the pixels or you can say that the shades of red, green, and
blue. Finally, all of these channels or all of these matrices are superimposed so the shape of the
image, when loaded in a computer, will be- N * M *3.

Where, N is the number of pixels across the height, M would be the number of pixels across the
width, and 3 is representing the number of channels, in this case, we have 3 channels R, G, and
B. In our example, the shape of the colored image would be- 6 x 5 x 3 since we have 6 pixels
across the height, 5 across the width and there are 3 channels present.

How images are displayed and saved in OpenCV?

OpenCV: OpenCV (Open Source Computer Vision Library) is an open source computer vision
and machine learning software library. OpenCV was built to provide a common infrastructure for
computer vision applications and to accelerate the use of machine perception in the commercial
products. Being an Apache 2 licensed product, OpenCV makes it easy for businesses to utilize
and modify the code.

The library has more than 2500 optimized algorithms, which includes a comprehensive set of
both classic and state-of-the-art computer vision and machine learning algorithms. These
algorithms can be used to detect and recognize faces, identify objects, classify human actions in
videos, track camera movements, track moving objects, extract 3D models of objects, produce
3D point clouds from stereo cameras, stitch images together to produce a high resolution image
of an entire scene, find similar images from an image database, remove red eyes from images
taken using flash, follow eye movements, recognize scenery and establish markers to overlay it
with augmented reality, etc. The library is used extensively in companies, research groups and by
governmental bodies.

Along with well-established companies like Google, Yahoo, Microsoft, Intel, IBM, Sony,
Honda, Toyota that employ the library, there are many startups such as Applied Minds,
VideoSurf, and Zeitera, that make extensive use of OpenCV.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Reading Images in OpenCV

To read the contents of an image, we have a function [Link] (). The image should be in the
same directory. If not, then the full path of the image should be given.

The function takes two arguments:

The first argument is the path of the image.

The second argument is a flag that describes the way the image is read. The default is:

 cv2.IMREAD_COLOR : Loads image in color. It is used by default.


 cv2.IMREAD_GRAYSCALE : Loads image in gray mode.
 cv2.IMREAD_UNCHANGED : Loads image as it is, including alpha channels.

Code: (Run in Google colab and save the Desert image in the sample data file)

import numpy as np
import cv2
# loads the image in color
img = [Link]('/content/sample_data/[Link]')
# loads the image in grayscale
gray_img = [Link]('/content/sample_data/[Link]', cv2.IMREAD_GRAYSC
ALE)

As you can see, we also import numpy in our program. OpenCV uses numpy and with numpy,
we can easily manipulate the data. After running this program in Python, we will be assigning
the image data into the img variable.

Displaying Images

To display an image on the window, we have a function [Link] (). This function creates a
window and displays the image with the original size.
Code: (Run in google Colab and save the Desert image in the sample data file)
from [Link] import cv2_imshow
import numpy as np
import cv2
# load image in color
img = [Link]('/content/sample_data/[Link]')
#load image in grey
gimg = [Link]('/content/sample_data/[Link]',0)
#Displaying images
cv2_imshow(img)
cv2_imshow(gimg)
[Link](0)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

[Link]()
Output:

The function has two parameters. The first parameter is a string that describes the name of the
window. The second parameter is the image array which contains the image data.
[Link]() function waits for a keyboard interrupt for a specific amount of time in
milliseconds. Here we passed 0 (zero) which specifies that it has to wait for an indefinite amount
of time until we press any key from keyboard and the execution continues.
The next function [Link]() closes all the windows in which images are
displayed.

Saving an image using OpenCV

While working with images in Image Processing applications, it is quite often that you need to
store intermediate results of image transformations or save the final resulting image. When
working with OpenCV Python, images are stored in numpy ndarray. To save an image to
the local file system, use [Link]() function of opencv python library. After executing the
above code, we can run the following code to save the images in different formats ( such as png).

Code

[Link]('[Link]',img)
[Link]('[Link]',gimg)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Display Utility Functions

1. Color to GrayScale - We convert the BGR images to grayscale images so that we can
observe the intensity of the pixels in the image rather than the color configurations in the
image.
Code:

import cv2
from [Link] import cv2_imshow
img = [Link](r'[Link]')
cv2_imshow(img)
gray = [Link](img,cv2.COLOR_BGR2GRAY)
cv2_imshow(gray)
[Link](0)

Blurring an image

Blurring an image using GuassianBlur. We blur an image in order to reduce the noise in the
image caused by bad lighting and some issues with the sensors in cameras while capturing the
image. In the code, in order to increase the range of blur, we can increase the kernel size from
(3,3) to (7,7).

Code:

import cv2
from [Link] import cv2_imshow
img = [Link](r'[Link]')
cv2_imshow(img)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

blur = [Link](img,(7,7),cv2.BORDER_DEFAULT)
cv2_imshow(blur)
[Link](0)

Output:

Edge Cascade (Edge Detection)

Edge Cascade to detect edges in an image for better model learning. We using the Canny Edge
detector which is very popular.

Code (Google-Colab)
import cv2
from [Link] import cv2_imshow
img = [Link](r'[Link]')
cv2_imshow(img)
canny = [Link](img,125,175)
cv2_imshow(canny)
[Link](0)

Here, Canny has two thresholds (upper and lower):

a. If a pixel gradient is higher than the upper threshold, the pixel is accepted as an edge
b. If a pixel gradient value is below the lower threshold, then it is rejected.
c. If the pixel gradient is between the two thresholds, then it will be accepted only if it is
connected to a pixel that is above the upper threshold.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Output

Resize and cropping

Resizing allows you to make your image smaller or larger without cutting anything out. Resizing
alters the image's dimensions, which typically affects the file size and image quality. The most
common reason for resizing photos is to reduce the size of large files to make them easier to
email or share online.
Cropping is done to remove all unwanted objects or areas from an image. Or even to highlight a
particular feature of an image. There is no specific function for cropping using OpenCV, NumPy
array slicing is what does the job. Every image that is read in, gets stored in a 2D array (for each
color channel). Simply specify the height and width (in pixels) of the area to be cropped.
Code:

import cv2
from [Link] import cv2_imshow
img = [Link](r'[Link]')
cv2_imshow(img)
#resizing the images:
resize = [Link](img,(500,500))
crop = img[50:200,200:400]
cv2_imshow(crop)
cv2_imshow(resize)
[Link](0)
Output:

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Splitting an image into its respective RED, GREEN, and BLUE Channels parts. Here it is
depicted as a GrayScale image and therefore we can observe the concentration of pixels for
respective colors.
Code:
import numpy as np
import cv2 as cv
from [Link] import cv2_imshow
img = [Link](r'[Link]')
cv2_imshow(img)
b,g,r = [Link](img)
cv2_imshow(b)
cv2_imshow(g)
cv2_imshow(r)

Output:

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Merging Channels
Now let us merge these separate blue, green and red channels into a BGR image. For this
purpose, we use the [Link]() function, which takes the three channels that we separated
previously as input and returns us a picture with all the three channels merged.
image_merged = [Link]((b,g,r))
cv2_imshow(image_merged)

After the merge operation, we get the original image as output.


Manipulating Color Pixels
To access a single pixel value in an image we can use indexing the same as we do to NumPy
array indexing. We can use slicing to access a sequence of pixel values. To modify the pixel
values we use the simple Python assignment operator ("=").

Steps

To access and modify pixel values in an image we could follow the below steps-
 Import the required library. In all the following examples, the required Python library
is OpenCV. Make sure you have already installed it.
 Read the input RGB image using [Link](). The RGB image read using this method
is in BGR format. Optionally assign the read BGR image to img.
 To access a single pixel use indexing and to modify a single pixel value use assignment to
indexing. Like to modify the pixel value at [200,150] to red color we apply
img[200,150] = (0, 0, 255)
 To access a sequence of pixels use slicing and to modify these pixel values use
assignment to slicing. Like to modify the pixel values at [100:300,150:350] to red color
we apply
img[100:300,150:350] = (0, 0, 255)
We will use this image as the Input File in the following example.

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Example 1:

In this Python program, we access the pixel value at a point in the input image. We also find the
pixel value for three different color channels and modify the pixel value at that point for the red
color channel.
# program to access and modify a pixel value
# import required libraries
import cv2
# read the input image
img = [Link]('[Link]')
# access pixel values using indexing
print("pixel value at [200,150]:", img[200,150])
print("pixel value blue channel at [200,150]:", img[200,150][0])
print("pixel value green channel at [200,150]:", img[200,150][1])
print("pixel value red channel at[200,150]:", img[200,150][2])
# modify the pixel value at [200,150] for red color channel
img[200,150] = (0, 0, 255)

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

print("after modifying pixel value at [200,150]:", img[200,150])

Output

When you run the above program, it will produce the following output −
pixel value at [200,150]: [115 192 254]
pixel value blue channel at [200,150]: 115
pixel value green channel at [200,150]: 192
pixel value red channel at [200,150]: 254
after modifying pixel value at [200,150]: [ 0 0 255]

Example 2:

In this Python program, we access the pixel values of a region in the input image. We also
modify these pixel values to red color.
# program to access and modify the pixel values of a region
# import required libraries
import cv2
# read the input image
img = [Link]('[Link]')
# access pixel values using indexing and slicing
# modify pixel color of a region to red color
img[100:300,150:350] = (0, 0, 255)
# display the modified image
[Link]('Modified Image', img)
[Link](0)
[Link]()

Output

When you run the above program, it will produce the following output −

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

Notice the output image after modifying the pixel values to red color.
Images with Alpha Channel.

The alpha channel is a special channel that handles transparency. When an image has an alpha
channel on it, it means you can adjust the image's opacity levels and make bits translucent or
totally see-through.

The alpha channel is an 8-bit channel, which means it has 256 levels of gray from 0 (black) to
255 (white). White acts as the visible area; black acts as the transparent area (you see the
background behind the image when displayed). The level of gray in between determines the level
of visibility. For example, 50 percent gray allows for 50 percent visibility. Alpha channels are
usually used with 16.8M color RGB images. The resulting image is called RGBA (RGB+A, A
means alpha channel).
Normally a colored image has 3 channels (RGB), which specify the red, green and blue
intensities. When you add the alpha channel as the fourth channel you can specify the
transparency of each pixel, therefore the alpha channel is of the same dimension as the color
channels.

When displaying a 4-channel image the intensity of each image is calculated as

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

As you know a normal RGB image has 3 channels, but transparent images have 4 channels, the
4th channel is the alpha channel, the alpha channel stores information regarding transparency,
in simple terms this alpha channel is just a binary black and white mask where the white area
represents the foreground and the black area represents the background.

Example Code:

import cv2

img1 = [Link]('[Link]')
img2 = [Link]('[Link]')
img2 = [Link](img2, [Link][1::-1])
[Link]("img 1",img1)
[Link](0)
[Link]("img 2",img2)
[Link](0)
choice = 1
while (choice) :
alpha = float(input("Enter alpha value"))
dst = [Link](img1, alpha , img2, 1-alpha, 0)
[Link]('alpha_mask_.png', dst)
img3 = [Link]('alpha_mask_.png')
[Link]("alpha blending 1",img3)
[Link](0)
choice = int(input("Enter 1 to continue and 0 to exit"))

Output

alpha = 0.8 alpha = 0.5

**********************End of UNIT-1************************

Dr. Om Prakash Samantray, CSE


COMPUTER VISION (UNIT-I)

IMPORTANT QUESTIONS
1. What is computer vision? Write about goals and examples of computer vision.
[4M]
2. Compare and contrast image processing and computer vision. [4M]
3. What is an image? Explain different types of images with representation.[4M]
4. How different types of images are represented as matrix? Explain.[4M]
5. What is a pixel? How to manipulate pixels? Explain with example.[4M]
6. Write about problems and challenges in computer vision.[10M]
7. Explain different application areas of computer vision.[10M]
8. What is a digital image? How images are formed? Explain.[10M]
9. What is OpenCV? Write python code to read, save and display images in
OpenCV.[10M]
10. How pixels are accessed and manipulated in OpenCV? Explain with an example
program.[10M]

Dr. Om Prakash Samantray, CSE

You might also like