失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > -11-30 OpenCV人工智能图像处理学习笔记 第3章 计算机视觉加强之几何变换 warpAffine

-11-30 OpenCV人工智能图像处理学习笔记 第3章 计算机视觉加强之几何变换 warpAffine

时间:2019-08-05 20:21:10

相关推荐

-11-30 OpenCV人工智能图像处理学习笔记 第3章 计算机视觉加强之几何变换 warpAffine

目录

主要内容

图片缩放

图片剪切

图片位移

图片镜像

再看图片缩放

图片仿射变换

再看图片旋转

主要内容

图片缩放

图片放在上一级文件夹则:

img = cv2.imread('../image0.jpg',1)

# 1 load 2 info 3 resize 4 checkimport cv2img = cv2.imread('image0.jpg',1)imgInfo = img.shapeprint(imgInfo)height = imgInfo[0]width = imgInfo[1]mode = imgInfo[2]# 1 放大 缩小 2 等比例 非 2:3 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

# newX = x*(src 行/目标 行) newX = 1*(10/5) = 2

# newY = y*(src 列/目标 列) newY = 2*(20/10)= 4

# 12.3 = 12

# 双线性插值

# A1 = 20% 上+80%下 A2

# B1 = 30% 左+70%右 B2

# 1 最终点 = A1 30% + A2 70%

# 2 最终点 = B1 20% + B2 80%

#实质:矩阵运算

# 1 info 2 空白模版 3 xyimport cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]dstHeight = int(height/2)dstWidth = int(width/2)dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)#0-255for 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('dst',dstImage)cv2.waitKey(0)# 1 opencv API resize 2 算法原理 3 源码

i表示行 ,多少行就是多高 height y

j多少列,宽 width x

图片剪切

#100 -》200 x#100-》300 yimport cv2img = cv2.imread('image0.jpg',1)imgInfo = img.shapedst = img[100:200,100:300]cv2.imshow('image',dst)cv2.waitKey(0)

图片位移

import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]####matShift = np.float32([[1,0,100],[0,1,200]])# 2*3dst = cv2.warpAffine(img,matShift,(height,width))#1 data 2 mat 3 info# 移位 矩阵cv2.imshow('dst',dst)cv2.waitKey(0)

# [1,0,100], [0,1,200] 2*2 2*1

# [[1,0], [0,1]] 2*2 A

# [[100], [200]] 2*1 B

# xy C

# A*C+B = [[1*x+0*y],[0*x+1*y]]+[[100],[200]] = [[x+100],[y+200]]

#(10,20)->(110,120)

import cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapedst = np.zeros(img.shape,np.uint8)height = imgInfo[0]width = imgInfo[1]for i in range(0,height-50):for j in range(0,width-100):dst[i+50,j+100]=img[i,j]cv2.imshow('image',dst)cv2.waitKey(0)

OpenCV中图像旋转(warpAffine)

#include <opencv2/opencv.hpp>using namespace cv;void copyPixel(const Mat& src, Mat& dst){for (size_t i = 0; i < src.rows; i++){for (size_t j = 0; j < src.cols; j++){dst.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, j)[0]; //蓝色通道dst.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, j)[1]; //绿色通道dst.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, j)[2]; //红是通道}}}int main(){Mat src = imread("NWPULB.jpg", IMREAD_COLOR);int width = src.cols / 6;int height = src.rows / 6;resize(src, src, Size(width, height));Mat image = Mat(Size(2*width, height), src.type());namedWindow("image", 1);//imshow("images", src);if (src.empty()){std::cout << "Could not open or find the image!\n" << std::endl;return EXIT_FAILURE;}Mat imageWarp = Mat(Size(src.cols, src.rows), src.type());Mat warp = (Mat_<float>(2, 3) << 1, 0, src.cols, 0, 1, -50);//cv::Mat t_mat = cv::Mat::zeros(2, 3, CV_32FC1);//t_mat.at<float>(0, 0) = 1;//t_mat.at<float>(0, 2) = 20; //水平平移量//t_mat.at<float>(1, 1) = 1;//t_mat.at<float>(1, 2) = 10; //竖直平移量//int len = std::max(src.cols, src.rows);//指定旋转中心cv::Point2f center( src.cols/2, src.rows/2);//获取旋转矩阵(2x3矩阵)cv::Mat rot_mat = cv::getRotationMatrix2D(center, -90, 1.0);//先完成图片的90度旋转以原图的中心为旋转点warpAffine(src, imageWarp, rot_mat, Size(src.cols, src.rows));//再将图片向右平移src.cols,并向上平行50warpAffine(imageWarp, imageWarp, warp, Size(2 * src.cols, src.rows));copyPixel(src, imageWarp);imshow("image", imageWarp);waitKey();destroyWindow("image");return 0;}

图片镜像

import cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]deep = imgInfo[2]newImgInfo = (height*2,width,deep)dst = np.zeros(newImgInfo,np.uint8)#uint8 for i in range(0,height):for j in range(0,width):dst[i,j] = img[i,j]#x y = 2*h - y -1dst[height*2-i-1,j] = img[i,j]for i in range(0,width):dst[height,i] = (0,0,255)#BGRcv2.imshow('dst',dst)cv2.waitKey(0)

再看图片缩放

#[[A1 A2 B1],[A3 A4 B2]]# [[A1 A2],[A3 A4]] [[B1],[B2]]# newX = A1*x + A2*y+B1# newY = A3*x +A4*y+B2# x->x*0.5 y->y*0.5# newX = 0.5*ximport cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]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)

图片仿射变换

import cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#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,100]])#组合matAffine = cv2.getAffineTransform(matSrc,matDst)# mat 1 src 2 dstdst = cv2.warpAffine(img,matAffine,(width,height))cv2.imshow('dst',dst)cv2.waitKey(0)

再看图片旋转

import cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]# 2*3 matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,1)# mat rotate 1 center 2 angle 3 scale#100*100 25 dst = cv2.warpAffine(img,matRotate,(height,width))cv2.imshow('dst',dst)cv2.waitKey(0)

如果觉得《-11-30 OpenCV人工智能图像处理学习笔记 第3章 计算机视觉加强之几何变换 warpAffine》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。