天天看点

7、OpenCV翻转及复制

Python:​

​cv.​

​​

​Flip​

​​(src, dst=None, flipMode=0) → None​​¶​​

Parameters:
  • src– input array.
  • dst– output array of the same size and type as​

    ​src​

    ​.
  • flipCode– a flag to specify how to flip the array; 0 means flipping around the x-axis and positive value (for example, 1) means flipping around y-axis. Negative value (for example, -1) means flipping around both axes (see the discussion below for the formulas).

The function ​

​flip​

​ flips the array in one of three different ways (row and column indices are 0-based):

7、OpenCV翻转及复制
# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = cv2.imread('1.jpg')
img=cv2.resize(img,(3,3))
print 'img=',img
rimg = img.copy()
fimg = img.copy()
rimg = cv2.flip(img,1)
print 'rimg=',rimg
fimg = cv2.flip(img,0)
print 'fimg=',fimg
cv2.imshow('Original',img)
cv2.imshow('Vertical',rimg)
cv2.imshow('Horizontal',fimg)
cv2.imwrite('flip-vertical.png',rimg)
cv2.imwrite('flip-horizontal.png',fimg)
#src 是原始图像;
#dst 是和原始图像大小,类型相同的目标图像;
#flipCode 是旋转类型,0代表x轴旋转,任意正数代表y轴旋转,任意负数代表x和y轴同时旋转。
#简单一句话总结>0的数 进行矩阵列互换 =0 行变换  =负数 行列互换
k=cv2.waitKey(0)&0xff  
print k
#等待按键按下,很类似单片机的按键等待操作;在手册中没有找到。。。。
if k==27:  #27==ESC
  cv2.destroyAllWindows()#销毁窗口
elif k==ord('s'):
  cv2.imwrite('mypicture.png',img)##保存新的名字图片
  cv2.destroyAllWindows()      

测试图片随机设置就行;

img= [
      [[  0   0    0]  [255 255 255]  [160 146 150]]
      [[  8   9    7]  [164 185 186]  [155 139 150]]
      [[ 46  56   57]  [249 255 253]  [ 33  37  33]]
      ]
rimg= [
      [[160 146 150]  [255 255 255]   [  0   0   0]] 
      [[155 139  150]  [164 185 186]  [  8   9   7]]
      [[ 33  37   33]  [249 255 253]  [ 46  56  57]]
      ]
fimg= [
      [[ 46  56  57]  [249 255 253]  [ 33  37  33]]
      [[  8   9    7]  [164 185 186]  [155 139 150]]
      [[  0   0    0]  [255 255 255]  [160 146 150]]
      ]      
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img=cv2.imread('1.jpg',0)
rows,cols=img.shape
# 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
M=cv2.getRotationMatrix2D((cols/2,rows/2),90,0.6)
# 第三个参数是输出图像的尺寸中心
dst=cv2.warpAffine(img,M,(cols,rows))
while(1):
    cv2.imshow('img',dst)
    if cv2.waitKey(1)&0xFF==27:
        break
cv2.destroyAllWindows()      

继续阅读