0% found this document useful (0 votes)
69 views

Lab Material

This document discusses using OpenCV in Python for image processing and computer vision tasks. It introduces OpenCV as an open-source library containing optimized algorithms for computer vision and machine learning. It then demonstrates how to read images using cv2.imread(), display images using cv2.imshow(), and access pixel data. Additional code examples show extracting RGB pixel values, displaying images in grayscale, calculating image data type and negatives, applying gamma correction and log transformations.

Uploaded by

adu g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Lab Material

This document discusses using OpenCV in Python for image processing and computer vision tasks. It introduces OpenCV as an open-source library containing optimized algorithms for computer vision and machine learning. It then demonstrates how to read images using cv2.imread(), display images using cv2.imshow(), and access pixel data. Additional code examples show extracting RGB pixel values, displaying images in grayscale, calculating image data type and negatives, applying gamma correction and log transformations.

Uploaded by

adu g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Vision and Image Processing

Lab #1
By Diriba Regasa (MSc)
OpenCV to Display Images in Python

• This is a very famous, beginner-friendly and open-source, and


powerful package that is responsible for image processing.
• It stands for Open-Source Computer Vision Library.
• This library consists of around 2000+ optimized algorithms
that are useful for computer vision and machine learning.
• There are two main functions OpenCV provides to read and
display images.
– cv2.imread()
– cv2.imshow()

2
code

import sys # to access the system


import cv2
img = cv2.imread("C:/Users/Diriba/Desktop/CV
Lab/tigre.jpg", cv2.IMREAD_ANYCOLOR)

while True:
cv2.imshow(“tigre", img)
cv2.waitKey(0)
sys.exit() # to exit from all the processes
cv2.destroyAllWindows() # destroy all windows

3

• Import the OpenCV package to access the functions.


• import the sys module for additional packages.
• Create a variable as img that holds our image.
• Call the cv2.imread() function and deliver the image path/image
• Then set the cv2.IMREAD_ANYCOLOR is to read every color of the
image.
• Then set a while loop and that will help us render the image an infinite
number of times till; we exit the system.
• cv2.imshow() function takes two parameters, the image title and the image
path variable img.
• The cv2.waitkey() method waits till we exit or click on the close button.
• Then call the sys.exit() method to safely exit the technique.
• Destroy all the created windows using cv2.destroyAllWindows().

4
How to Access Pixel Data in Image using Python OpenCV

import cv2
img = cv2.imread('C:/Users/Diriba/Desktop/CV Lab/tigre.jpg', 1)
print(img) #print pixels value in the form of array
print(img[11, 21]) #To access any image pixels(the 11th row and
21st column)
print(img.size) #To find the total number of pixels of the Image

5
Extract RGB values
import sys
import cv2
import numpy as np
img = cv2.imread("C:/Users/Diriba/Desktop/CV Lab/p1.jpeg")
# Extracting RGB values.
# Here we have randomly chosen a pixel
# by passing in 100, 100 for height and width.
(B, G, R) = img[100, 100]
# Displaying the pixel values
print("R = {}, G = {}, B = {}".format(R, G, B))
# We can also pass the channel to extract
# the value for a specific channel
cv2.imshow("tigre", img)
cv2.waitKey(0)

6
Showing color image

import sys
import cv2
import numpy as np
img_gray = cv2.imread("C:/Users/Diriba/Desktop/CV
Lab/p1.jpeg", 0)
img = cv2.imread("C:/Users/Diriba/Desktop/CV
Lab/p1.jpeg",1)
#Showing gray image
cv2.imshow('tigre', img)
cv2.imshow('Gray picture', img_gray)
cv2.waitKey(0)

7
Data type of an image and image negative
#python script
import cv2
import numpy as np

#load the image


img=cv2.imread('C:/Users/Diriba/Desktop/CV Lab/p1.jpeg')

#check the data type of an image


print(img.dtype)

#subtract the image from maximum value


img_neg=255-img

#show the image


cv2.imshow('negative',img_neg)
cv2.waitKey(0)

8
Power-low(gamma)
import numpy as np
import cv2
img=cv2.imread("C:/Users/Diriba/Desktop/CV Lab/p1.jpeg")

#gamma=2.2
gamma_two_point_two=np.array(255*(img/255)**2.2,dtype='uint8')

#gamma=4.4
gamma_point_four=np.array(255*(img/255)**4.4,dtype='uint8')

cv2.imshow("p1", img)
cv2.imshow('a2',gamma_two_point_two)
cv2.imshow('a3',gamma_point_four)
cv2.waitKey(0)

9
Log-transformation
import cv2
import numpy as np
#load the image
img1=cv2.imread("C:/Users/Diriba/Desktop/CV Lab/p1.jpeg")
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

#log transformation formula


img_log=(np.log(img)/(np.log(1+np.max(img))))*255

#specify datatype
img_log=np.array(img_log,dtype=np.uint8)

#display the image


cv2.imshow("p1",img1)
cv2.imshow('log image',img_log)
cv2.imshow('original',img)
cv2.waitKey(0)

10
The End

You might also like