2018.12.4—2018.12.5计算机视觉加强之几何变换

5 篇文章 0 订阅
5 篇文章 0 订阅

12.4

01 图片的缩放


	# 1 load 加载 ,2 info 获取信息 ,3  resize 完成缩放 ,4 check 检查结果
	import cv2 
	img = cv2.imread('image0.jpg',1) #1 : 图片路径  ,2 :1表示彩色图片
	
	imgInfo = img.shape
	print(imgInfo)
	height = imgInfo[0]
	width = imgInfo[1]
	mod = imgInfo[2] # 打印3   表示一个像素点有颜色的三个基本元素组成
	# 1 放大 缩小  ,2等比例  非等比
	dstHeight = int(height*0.5)
	dstWidth = int(width*0.5)
	# 最近临域插值 双线性插值  像素关系重采样  立方差值
	dst = cv2.resize(img,(dstWidth,dstHeight))
	cv2.imshow('image',dst)
	cv2.waitKey(0)
	
	# 最近临域插值 双线性插值  原理
	# 最近临域插值
	# src 10*20 dst 5*10
	# dst <-src
	# (1,2) <- (2,4)
	# dst x 1-> src x 2  newX     (src :原图像      dst:目标 )
	# newX =src X*(src 行/目标 行) newX = 1*(10/5) =2
	# newY =src Y*(src 列/目标 列) newX = 1*(20/10) =4
	# 12.3 = 12
	
	# 双线性插值 
	# A1 = 20% 上 + 80%下 A2
	# B1 = 30% 左 + 78% 右 B2
	# 1 最终点 = A1x30% + A2x70%
	# 2 最终点 = B1x20% + B2x80%

02 图片缩放2

	
	# 1 info获取信息 2 创建空白模版 3 重新计算xy
	import cv2
	import numpy as np
	img = cv2.imread('image0.jpg',1)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	dstHeight = int(height/2)
	dstWidth = int (width/2)
	dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)#uint8 0-255
	for i in range(0,dstHeight):#行
	    for j in range(0,dstWidth):#列
	        iNew = int(i*(height*1.0/dstHeight))
	        jNew = int (j*(width*1.0/dstWidth))
	        dstImage[i,j] = img[iNew,jNew]
	cv2.imshow('img',dstImage) 
	cv2.waitKey(0)
	# 1 opencv 中API的调用 resize 2算法原理 3自己写源码实现

03 图片剪切

	# 100 -> 200 x
	# 100 -> 300 y
	import cv2
	img = cv2.imread('image0.jpg',1)
	imgInfo = img.shape
	dst = img[100:200,100:300] #x:100~200 y:100~300
	cv2.imshow('img',dst)
	cv2.waitKey(0)

04 图片移位

	# 1.通过API移位 2.算法原理  3.通过源代码实现
	import cv2
	import numpy as np
	img = cv2.imread('image0.jpg',1)
	cv2.imshow('src',img)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	####
	matShift = np.float32([[1,0,100],[0,1,200]])# 2*3矩阵
	dst = cv2.warpAffine(img,matShift,(height,width))#1 原图data ,2 mat 移位矩阵 ,3图片的info信息
	# warpAffine实现了移位 和矩阵的运算
	cv2.imshow('img',dst)
	cv2.waitKey(0)
	
	# 2.算法原理
	# [1,0,100],[0,1,200] 2*2 2*1
	# [1,0],[0,1]  2*2
	# [100],[200]  2*1
	# xy C
	# A*C+B = [[1*x+0*y],[0*x+1*y]]+[[100],[200]]
	# =[[x+100],[y+200]]
	
	# (10,20) ->(110,120)

05图片位移2

	import cv2
	import numpy as np
	
	img = cv2.imread('image0.jpg',1)
	cv2.imshow('img',img)
	imgInfo = img.shape
	dst = np.zeros(img.shape,np.uint8) #1:图片的维度  2:图片的数据类型
	height = imgInfo[0]
	width = imgInfo[1]
	for i in range(0,height):
	    for j in range(0,width-100):
	        dst[i,j+100]=img[i,j]
	cv2.imshow('img',dst)
	cv2.waitKey(0)

12.5

06图片镜像


	import cv2
	import numpy as np
	img = cv2.imread('image0.JPG',1)
	cv2.imshow('img',img)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	deep = imgInfo[2]
	# 竖向镜像
	newImgInfo_h = (height*2,width,deep)
	dst_h  = np.zeros(newImgInfo_h,np.uint8)
	for i in range(0,height):
	    for j in range(0,width):
	        dst_h[i,j] = img[i,j]
	        #x   y  = 2*h-y-1
	        dst_h[height*2-i-1,j] = img[i,j]
	for i in range(0,width):
	    dst_h[height,i] = (0,0,255)#BGR
	cv2.imshow('dst_h',dst_h)
	# 横向镜像
	newImgInfo_w = (height,width*2,deep)
	dst_w  = np.zeros(newImgInfo_w,np.uint8)
	for i in range(0,height):
	    for j in range(0,width):
	        dst_w[i,j] = img[i,j]
	        #x   y  = 2*h-y-1
	        dst_w[i,width*2-j-1] = img[i,j]
	for i in range(0,height):
	    dst_w[i,width] = (0,0,255)#BGR
	cv2.imshow('dst_w',dst_w)
	
	cv2.waitKey(0)

07图片缩放

	# [[A1 A2 B1],[A3 A4 B2]]
	#权重 [[A1 A2],[A3 A4]]   偏移 [[B1],[B2]]
	# newX = A1*X + A2*Y + B1
	# newY = A3*X + A4*Y + B2
	
	# [0.5,0,0],[0,0.5,0] 缩小1/2
	#X->X*0.5   Y->Y*0.5
	# newX = 0.5X
	# newY = 0.5Y
	import cv2
	import numpy as np
	img = cv2.imread('image0.JPG',1)
	cv2.imshow('img',img)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	# deep = imgInfo[2]
	matScale = np.float32([[0.5,0,0],[0,0.5,0]])
	dst = cv2.warpAffine(img,matScale,(int(width/2),int(height/2)))
	cv2.imshow("dst",dst)
	cv2.waitKey(0)

08图片的放射变换

	import cv2
	import numpy as np
	img = cv2.imread('image0.JPG',1)
	cv2.imshow('img',img)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	# deep = imgInfo[2]
	# src 3个点 ->dst 3个点(左上角 左下角 右上角)
	matSrc = np.float32([[0,0],[0,height-1],[width-1,0]])
	matDst = np.float32([[50,50],[300,height-200],[width-300,200]])
	# 组合
	matAffine = cv2.getAffineTransform(matSrc,matDst)# mat 1:src   2:dst
	dst = cv2.warpAffine(img,matAffine,(width,height))
	cv2.imshow('dst',dst)
	cv2.waitKey(0)

09图片旋转

	# 09图片旋转
	import cv2
	import numpy as np
	img = cv2.imread('image0.JPG',1)
	cv2.imshow('img',img)
	imgInfo = img.shape
	height = imgInfo[0]
	width = imgInfo[1]
	# 2*3
	matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)# mat   rotate 1:center 图片的中心点  2:angle 旋转的角度 3:sc  缩放的比例
	# 100*100 25
	dst = cv2.warpAffine(img,matRotate,(height,width))  #放射方法
	cv2.imshow('dst',dst)
	cv2.waitKey(0)

总结

经过大概一天的学习,

  1. 总体上来说对OpenCV的图形几何变换操作有更深入的了解
  2. 熟练度欠佳
  3. 继续努力
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值