Image ROI and mask mask and image geometric transformation
Region of interest ROI:
⚫ROI—(region of interest)—area of interest
⚫Generally a rectangular area
⚫It can determine the focus of analysis, reduce processing time, and improve accuracy
⚫Definition method:
use the Rect starting point and ending point range
img= cv2 .imread('1 .jpg')
cv2 .imshow('src', img)
roi = img[250:483,211:706] #---y1:y2,x1:x2
cv2.imshow('ROI', roi)
Note the order of ROI parameters y1, y2, x1, x2
Image Geometric Transformation
Image scaling—cv2.resize()
⚫ src: 输入图像
⚫ dst: 输出图像
⚫ dsize: Size类型,指定输出图像大小,如果它等于0,由下式计算:
dsize = Size(round(fx*src.cols), round(fy*src.rows))
⚫ fx: 沿水平方向的缩放系数,默认值0,等于0时,由下式计算:
(double)dsize.width/src.cols
⚫ fy: 沿垂直方向的缩放系数,默认值0,等于0时,由下式计算:
(double)dsize.height/src.rows
⚫ interpolation: 用于指定插值方式,默认为cv2.INTER_LINEAR (线性插值)
INTER NEAREST #Nearest neighbor interpolation
INTER LINEAR #Linear interpolation (default value) (large image use – fast) INTER AREA #Area interpolation (small image recommended) INTER
CUBIC #Cubic spline interpolation (enlarged image use – slow)
INTER LANCZOS4 # Lanczos interpolation
Example of use
exl:
dst=cv2.resize(img1,None,fx=2,fy=2,interpolation=cv2.INTER LINEAR)
ex2:
dst=cv2.resize(img1,(320,320) ,interpolation=cv2.INTER AREA)
image panning
Principle introduction
Example of use
#---平移函数1(原图,X方向位移,Y方向位移)
#---不改变原图大小,会丢失信息
def imgTranslate (img,xoffset,yoffset):
rows = img.shape[0]
cols = img.shape[1]
dst = np.zeros ((rows,cols ,3) ,np.uint8)
for i in range(0,rows) :
for j in range(0,cols) :
x=j+ xOffset
y=i+ yOffset
if(x>=0 and y>=0 and x<cols and y<rows) :
dst[y,x] = img[i,j]
return dst
#---平移函数2(原图,x方向位移,Y方向位移)#---改变原图大小,不丢失信息
def imgTranslate2 (img,xoffset,yoffset):
rows = img.shape[0]+abs(yoffset)
cols = img.shape[1]+abs(xoffset)
dst = np.zeros ((rows ,cols ,3) ,np.uint8)
for i in range (0,img.shape[0]) :
for j in range (0 ,img.shape[1]) :
x= j + xOffset
y= i + yOffset
if(x>=0 and y>=0 and x<cols and y<rows) :
dst[y,x] = img[i,j]
return dst
Simple and fast translation method
To translate is to change the position of an object. If you want to move in the (X, y) direction, and the distance to move is (tx, ty), you can build the movement matrix in the following way:
You can build this matrix using a Numpy array (data type is np.float32), and then pass it to the function cv2.warpAffine()
exl:
#读取图像1
img1=cv2 .imread('lena.jpg')
cv2.imshow('srcl',img1)
rows,cols,channel = img1.shape #获取图像属性
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2 .warpAffine (img1,M,(cols ,rows))
image rotation
Graphical introduction
⚫OpenCV does not provide a function to directly rotate the image
⚫Image rotation may cause loss of image information
⚫Image rotation can be achieved with affine transformation
⚫Mainly used functions: cv2.getRotationMatrix2D() cv2.warpAffine()
Rotation matrix M:
The parameters correspond to: rotation center, rotation angle, and zoom factor after rotation.
use case
img1=cv2.imread('lena.jpg') #读取图像1
cv2 .imshow('src1', img1)
rows,cols,channel = img1.shape #获取图像属性
M = cv2.getRotationMatrix2D ((cols/2,rows/2),45,1)
dst = cv2.warpAffine(img1,M,(cols,rows)) #仿射变换
cv2.imshow('dst',dst) #结果显示
cv2 .waitKey(0)
cv2.destroyAllwindows ()
Transpose and Mirror
⚫Functions used cv2.transpose(), cv2.flip()
⚫Can achieve transposition and mirror transformation, and 90°, 180° rotation
flipCode = 0, vertical flip (flip along the X axis);
flipCode > 0, horizontal flip (flip along the Y axis);
flipCode < 0, horizontal and vertical flip (180°central symmetry);
Remapping—cv2.remap():
cv2.remap() function
Remapping refers to converting a pixel at a position in an image to a specified position in another image through a mapping relationship. For the input original image f(x, y), the target image g(x, y), and the mapping relationship is T, the following formula is satisfied: g(x, y) = T(f(x, y))
⚫map1
: Represents the coordinates or x coordinates of (x, y) points, CV_16SC2, CV_32FC1, CV_32FC2 types
⚫map2: represents the y coordinates of (x, y) points, if map1 is (x, y), map2 can choose not to use, it can be CV_16UC1 , CV_32FC1
⚫interpolation: Indicates the interpolation method
⚫borderMode: Indicates the boundary interpolation type
⚫borderValue: Indicates the interpolation value
use case
xMap = np.zeros (img1 . shape [:2],np .float32)
yMap = np.zeros (img1 . shape[:2] ,np .float32)
rows = img1 .shape[0]
cols = img1 .shape[1]
for i in range(0,rows) :
for j in range(0,cols):
xMap .itemset((i,j) ,j)
yMap.itemset((i,j) ,i+5*math.sin (j/10.0))
dst = cv2.remap (img1,xMap,yMap,interpolation=cv2.INTER LINEAR