In various projects we need to analyze images. This program is to analyze an image for its details. However, this is a collection of some of the basic functions of opencv. It’s up to you to develop the code to suite your project. The main objective of this tutorial is to show you how the opencv detects key presses. We can use it to create various interactive programs.
Let’s get into the project step by step.
As usual we need to import necessary libraries.
import numpy as np
import cv2
Next let’s import an image.
img = cv2.imread("Your Image File")
We need some global variables.
hori = 100 # this is to select the region of interest (roi) horizontally.
vert = 0 # this is to select the roi vertically.
expan = 0 # this value defines the size of the roi
scl = 1 # this defines the scale of the roi
Then we define some functions. (I hope you are familiar with the basic concepts of functions, no matter what the language is.)
First one is to select the roi horizontally.
You might wonder about the values I have used in my functions.
In opencv the cv2.waitKey() function returns a code value to each key you pressed in keyboard. So in the function horiMove() the code value 65363 represents the right arrow key. That means, if you press the right arrow key, the value of the variable ‘hori’ is increased by 10. If you press the left arrow key (key code value is 65361), the value of the variable ‘hori’ is reduced by 10.
def horiMove(k,hori):
if k==65363: # Press right arrow key
hori = hori+10
if hori>img.shape[1]-10:
hori = img.shape[1]-10
elif k==65361: # Press left arrow key
hori = hori-10
if hori<1:
hori = 1
return hori
Same way, this function moves/selects the roi vertically.
def vertMove(k,vert):
if k==65364: # Press up arrow key
vert = vert + 10
if vert>img.shape[0]-50:
vert = img.shape[0]-50
if k==65362: # Press down arrow key
vert = vert - 10
if vert<1:
vert = 1
return vert
This is to increase the selected area of roi.
def expanROI (k,expan):
if k==101: # press 'e'
expan = expan + 10
if expan > 150:
expan = 150
if k==119: # press 'w'
expan = expan - 10
if expan < 1:
expan = 1
return expan
This is to scale the roi. This means we magnify the roi a bit.
def magnifyROI(k, scl):
if k==109: #press 'm'
scl = scl + 1
if scl>3:
scl = 3
if k==110: #press 'n'
scl = scl - 1
if scl < 1:
scl = 1
return scl
Now we run the things in a non-ending loop.
while(1):
We create a copy of the img (the variable which the original image is loaded). Since we load the variable img to the variable Frame_out in each cycle, the changes made by the previous cycle to the Frame_out is eliminated.
Frame_out = img.copy()
k = cv2.waitKey(5) # this function will wait for 5 mili seconds and detects which key you have pressed. This ‘k’ value is passed to the functions.
Following four lines update the global variables by using the relevant function.
hori = horiMove(k, hori)
vert = vertMove(k,vert)
expan = expanROI (k,expan)
scl = magnifyROI(k, scl)
Now, let’s draw a rectangle around the roi. So, the roi area will be clear.
cv2.rectangle(Frame_out,(hori,10+vert),((hori+20+expan),30+expan+vert),(255,0,0),0)
This will increase the roi area.
roi = Frame_out[10+vert:30+expan+vert,hori:hori+20+expan]
This will scale the roi.
roi_edited = cv2.resize(roi,None,fx=scl, fy=scl, interpolation = cv2.INTER_CUBIC)
And now, let’s see what has happened to the original image and the roi.
cv2.imshow('Frame1',Frame_out)
cv2.imshow('Frame2',roi_edited)
if k==27: # Esc key to stop
break
cv2.destroyAllWindows()
That’s all.
Download the python code here.