亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python3+OpenCV2實現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射)

 更新時間:2019年05月13日 11:12:16   作者:我是小螞蟻  
這篇文章主要介紹了Python3+OpenCV2實現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

總結(jié)一下最近看的關(guān)于opencv圖像幾何變換的一些筆記.

這是原圖:

1.平移

import cv2
import numpy as np

img = cv2.imread("image0.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]

dst = np.zeros(imgInfo, np.uint8)

for i in range( height ):
  for j in range( width - 100 ):
    dst[i, j + 100] = img[i, j]

cv2.imshow('image', dst)
cv2.waitKey(0)

demo很簡單,就是將圖像向右平移了100個像素.如圖:

2.鏡像

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]
deep = imgInfo[2]

dst = np.zeros([height*2, width, deep], np.uint8)

for i in range( height ):
  for j in range( width ):
    dst[i,j] = img[i,j]
    dst[height*2-i-1,j] = img[i,j]

for i in range(width):
  dst[height, i] = (0, 0, 255)
cv2.imshow('image', dst)
cv2.waitKey(0)

demo生成一個如下效果:

3.縮放

import cv2
img = cv2.imread("image0.jpg", 1)
imgInfo = img.shape
print( imgInfo )
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]

# 1 放大 縮小 2 等比例 非等比例
dstHeight = int(height * 0.5)
dstWeight = int(width * 0.5)

# 最近鄰域插值 雙線性插值 像素關(guān)系重采樣 立方插值
dst = cv2.resize(img, (dstWeight,dstHeight))
print(dst.shape)
cv2.imshow('image', dst)
cv2.waitKey(0)

使用resize直接進(jìn)行縮放操作,同時還可以使用鄰域插值法進(jìn)行縮放,代碼如下:

# 1 info 2 空白模板 3 重新計算x, y
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)
for i in range( dstHeight ):
  for j in range(dstWidth):
    iNew = i * ( height * 1.0 / dstHeight )
    jNew = j * ( width * 1.0 / dstWidth )

    dstImage[i,j] = img[int(iNew),int(jNew)]

cv2.imshow('image', dstImage)
cv2.waitKey(0)

4.旋轉(zhuǎn)

import cv2

img = cv2.imread('image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]

# 定義一個旋轉(zhuǎn)矩陣
matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 0.7) # mat rotate 1 center 2 angle 3 縮放系數(shù)

dst = cv2.warpAffine(img, matRotate, (height, width))

cv2.imshow('image',dst)
cv2.waitKey(0)

旋轉(zhuǎn)需要先定義一個旋轉(zhuǎn)矩陣,cv2.getRotationMatrix2D(),參數(shù)1:需要旋轉(zhuǎn)的中心點.參數(shù)2:需要旋轉(zhuǎn)的角度.參數(shù)三:需要縮放的比例.效果如下圖:

5.仿射

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]
deep = imgInfo[2]
# src 3 -> dst 3 (左上角, 左下角,右上角)
matSrc = np.float32([[0,0],[0,height-1],[width-1, 0]]) # 需要注意的是 行列 和 坐標(biāo) 是不一致的
matDst = np.float32([[50,50],[100, height-50],[width-200,100]])

matAffine = cv2.getAffineTransform(matSrc,matDst) #mat 1 src 2 dst 形成組合矩陣
dst = cv2.warpAffine(img, matAffine,(height, width))
cv2.imshow('image',dst)
cv2.waitKey(0)

需要確定圖像矩陣的三個點坐標(biāo),及(左上角, 左下角,右上角).定義兩個矩陣,matSrc 為原圖的三個點坐標(biāo),matDst為進(jìn)行仿射的三個點坐標(biāo),通過cv2.getAffineTransform()形成組合矩陣.效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中itertools模塊zip_longest函數(shù)詳解

    python中itertools模塊zip_longest函數(shù)詳解

    itertools模塊包含創(chuàng)建高效迭代器的函數(shù),這些函數(shù)的返回值不是list,而是iterator(可迭代對象),可以用各種方式對數(shù)據(jù)執(zhí)行循環(huán)操作,今天我們來詳細(xì)探討下zip_longest函數(shù)
    2018-06-06
  • Python常見庫matplotlib學(xué)習(xí)筆記之畫圖中各個模塊的含義及修改方法

    Python常見庫matplotlib學(xué)習(xí)筆記之畫圖中各個模塊的含義及修改方法

    matplotlib是python最著名的繪圖庫,它提供了一整套和matlab相似的命令A(yù)PI,十分適合交互式地進(jìn)行制圖,下面這篇文章主要給大家介紹了關(guān)于Python常見庫matplotlib學(xué)習(xí)筆記之畫圖中各個模塊的含義及修改方法的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Python?Pandas讀取csv/tsv文件(read_csv,read_table)的區(qū)別

    Python?Pandas讀取csv/tsv文件(read_csv,read_table)的區(qū)別

    這篇文章主要給大家介紹了關(guān)于Python?Pandas讀取csv/tsv文件(read_csv,read_table)區(qū)別的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Pandas具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • 樹莓派使用USB攝像頭和motion實現(xiàn)監(jiān)控

    樹莓派使用USB攝像頭和motion實現(xiàn)監(jiān)控

    這篇文章主要為大家詳細(xì)介紹了樹莓派使用USB攝像頭和motion實現(xiàn)監(jiān)控,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • django 數(shù)據(jù)庫 get_or_create函數(shù)返回值是tuple的問題

    django 數(shù)據(jù)庫 get_or_create函數(shù)返回值是tuple的問題

    這篇文章主要介紹了django 數(shù)據(jù)庫 get_or_create函數(shù)返回值是tuple的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 跟老齊學(xué)Python之用Python計算

    跟老齊學(xué)Python之用Python計算

    做為零基礎(chǔ)學(xué)習(xí)Python,也就從計算小學(xué)數(shù)學(xué)題目開始吧。因為從這里開始,數(shù)學(xué)的基礎(chǔ)知識列為肯定過關(guān)了。
    2014-09-09
  • pytorch如何保存訓(xùn)練模型參數(shù)并實現(xiàn)繼續(xù)訓(xùn)練

    pytorch如何保存訓(xùn)練模型參數(shù)并實現(xiàn)繼續(xù)訓(xùn)練

    這篇文章主要介紹了pytorch如何保存訓(xùn)練模型參數(shù)并實現(xiàn)繼續(xù)訓(xùn)練問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • PyQt5實現(xiàn)簡單的計算器

    PyQt5實現(xiàn)簡單的計算器

    這篇文章主要為大家詳細(xì)介紹了PyQt5實現(xiàn)簡單的計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 使用Python如何測試InnoDB與MyISAM的讀寫性能

    使用Python如何測試InnoDB與MyISAM的讀寫性能

    網(wǎng)上有很多評論myisam和innodb讀寫性能對比,所以下面這篇文章主要給大家介紹了關(guān)于使用Python如何測試InnoDB與MyISAM讀寫性能的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • python 中的list和array的不同之處及轉(zhuǎn)換問題

    python 中的list和array的不同之處及轉(zhuǎn)換問題

    python中的list是python的內(nèi)置數(shù)據(jù)類型,list中的數(shù)據(jù)類不必相同的,而array的中的類型必須全部相同。這篇文章給大家介紹了python 中的list和array的不同之處及轉(zhuǎn)換問題,需要的朋友參考下吧
    2018-03-03

最新評論