江南才子 发表于 2021-6-24 10:22:13

基于python的空域变换

主要应用
[*]去除叠加性噪声
[*]生成图像叠加效果
import cv2 as cv
img1 = cv.imread("1.jpg")
img2 = cv.imread("2.jpg")
print(img1.shape,img2.shape)
img1=cv.resize(img1,(img2.shape,img2.shape))
image=cv.addWeighted(img1,0.6,img2,0.4,0.0,)

cv.imshow('img1',image)

cv.waitKey(0)
cv.destroyAllWindows()
  减法运算


  “主要运用”

[*]显示两幅图像的差异,检测同一场景两幅图像之间的变化,如:视频中镜头边界的检测
[*]去除不需要的叠加性图案
[*]图像分割:如分割运动的车辆,减法去掉静止部分,剩余的是运动元素和噪声
import cv2 as cv

img1=cv.imread('5.png')
img2=cv.imread('6.png')
#img1=cv.imread('LinuxLogo.jpg')
#img2=cv.imread('WindowsLogo.jpg')
dst=cv.add(img1,img2)
dst1=cv.subtract(img1,img2)

cv.imshow('dst',dst1)
cv.imshow('dst1',dst)

cv.waitKey(0)
cv.destroyAllWindows()


  乘法运算


主要应用
图像的局部显示,如:用二值蒙板图像与原图像做乘法 import cv2 as cv

img1=cv.imread('5.png')
img2=cv.imread('6.png')
dst=img1*img2
cv.imshow('181360152',dst)

cv.waitKey(0)
cv.destroyAllWindows()

  逻辑运算



[*]非运算主要运用:图像求反,g(x,y)=255-f(x,y)
[*]与运算主要用于:两个图像相交子集,提取感兴趣子图像,g(x,y)=f(x,y)^h(x,y)
import cv2 as cv

img1=cv.imread('LinuxLogo.jpg')
img2=cv.imread('WindowsLogo.jpg')
and_img=cv.bitwise_and(img1,img2)
or_img=cv.bitwise_or(img1,img2)
not_img=cv.bitwise_not(img1)
xor_img=cv.bitwise_xor(img1,img2)

cv.imshow('181360152',and_img)
cv.imshow('181360152zhang',or_img)
cv.imshow('181360152yang',not_img)
cv.imshow('181360152-',xor_img)

cv.waitKey(0)
cv.destroyAllWindows()
  缩放


import cv2
import math
import numpy as np

class Img:
    def __init__(self,image,rows,cols,center=):

      self<span class="token punctuation">.</span>src<span class="token operator">=</span>image <span class="token comment">#原始图像</span>
      self<span class="token punctuation">.</span>rows<span class="token operator">=</span>rows <span class="token comment">#原始图像的行</span>
      self<span class="token punctuation">.</span>cols<span class="token operator">=</span>cols <span class="token comment">#原始图像的列</span>
      self<span class="token punctuation">.</span>center<span class="token operator">=</span>center <span class="token comment">#旋转中心,默认是</span>    def Move(self,delta_x,delta_y):      #平移
      <span class="token comment">#delta_x&gt;0左移,delta_x&lt;0右移</span>
      <span class="token comment">#delta_y&gt;0上移,delta_y&lt;0下移</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>delta_x<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>delta_y<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Zoom(self,factor):               #缩放
      <span class="token comment">#factor&gt;1表示缩小;factor&lt;1表示放大</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Horizontal(self):                #水平镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Vertically(self):                #垂直镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>rows<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Rotate(self,beta):               #旋转
      <span class="token comment">#beta&gt;0表示逆时针旋转;beta&lt;0表示顺时针旋转</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">-</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>    <span class="token number">0</span><span class="token punctuation">,</span>            <span class="token number">0</span><span class="token punctuation">,</span>         <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Process(self):
      self<span class="token punctuation">.</span>dst<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">:</span>
                src_pos<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>i<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>j<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">[</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z<span class="token punctuation">]</span><span class="token operator">=</span>np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>self<span class="token punctuation">.</span>transform<span class="token punctuation">,</span>src_pos<span class="token punctuation">)</span>
                x<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                y<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
                <span class="token keyword">if</span> x<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>rows <span class="token operator">or</span> y<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>cols <span class="token operator">or</span> x<span class="token operator">&lt;</span><span class="token number">0</span> <span class="token operator">or</span> y<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">255</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>self<span class="token punctuation">.</span>src<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span>if __name__=='__main__':    src=cv2.imread('123.jpg',0)    rows = src.shape    cols = src.shape  cv2.imshow('src', src)
img=Img(src,rows,cols,)    img.Zoom(0.5) #缩放    img.Process()    cv2.imshow('dst', img.dst)  cv2.waitKey(0)
  平移


import cv2
import math
import numpy as np

class Img:
    def __init__(self,image,rows,cols,center=):

      self<span class="token punctuation">.</span>src<span class="token operator">=</span>image <span class="token comment">#原始图像</span>
      self<span class="token punctuation">.</span>rows<span class="token operator">=</span>rows <span class="token comment">#原始图像的行</span>
      self<span class="token punctuation">.</span>cols<span class="token operator">=</span>cols <span class="token comment">#原始图像的列</span>
      self<span class="token punctuation">.</span>center<span class="token operator">=</span>center <span class="token comment">#旋转中心,默认是</span>    def Move(self,delta_x,delta_y):      #平移
      <span class="token comment">#delta_x&gt;0左移,delta_x&lt;0右移</span>
      <span class="token comment">#delta_y&gt;0上移,delta_y&lt;0下移</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>delta_x<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>delta_y<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Zoom(self,factor):               #缩放
      <span class="token comment">#factor&gt;1表示缩小;factor&lt;1表示放大</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Horizontal(self):                #水平镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Vertically(self):                #垂直镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>rows<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Rotate(self,beta):               #旋转
      <span class="token comment">#beta&gt;0表示逆时针旋转;beta&lt;0表示顺时针旋转</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">-</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>    <span class="token number">0</span><span class="token punctuation">,</span>            <span class="token number">0</span><span class="token punctuation">,</span>         <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Process(self):
      self<span class="token punctuation">.</span>dst<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">:</span>
                src_pos<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>i<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>j<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">[</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z<span class="token punctuation">]</span><span class="token operator">=</span>np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>self<span class="token punctuation">.</span>transform<span class="token punctuation">,</span>src_pos<span class="token punctuation">)</span>
                x<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                y<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
                <span class="token keyword">if</span> x<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>rows <span class="token operator">or</span> y<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>cols <span class="token operator">or</span> x<span class="token operator">&lt;</span><span class="token number">0</span> <span class="token operator">or</span> y<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">255</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>self<span class="token punctuation">.</span>src<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span>if __name__=='__main__':    src=cv2.imread('123.jpg',0)    rows = src.shape    cols = src.shape  cv2.imshow('src', src)
img=Img(src,rows,cols,)    img.Move(-30, -50)# 平移  img.Process()
cv2.imshow('dst', img.dst)  cv2.waitKey(0)
  旋转


import cv2
import math
import numpy as np

class Img:
    def __init__(self,image,rows,cols,center=):

      self<span class="token punctuation">.</span>src<span class="token operator">=</span>image <span class="token comment">#原始图像</span>
      self<span class="token punctuation">.</span>rows<span class="token operator">=</span>rows <span class="token comment">#原始图像的行</span>
      self<span class="token punctuation">.</span>cols<span class="token operator">=</span>cols <span class="token comment">#原始图像的列</span>
      self<span class="token punctuation">.</span>center<span class="token operator">=</span>center <span class="token comment">#旋转中心,默认是</span>    def Move(self,delta_x,delta_y):      #平移
      <span class="token comment">#delta_x&gt;0左移,delta_x&lt;0右移</span>
      <span class="token comment">#delta_y&gt;0上移,delta_y&lt;0下移</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>delta_x<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>delta_y<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Zoom(self,factor):               #缩放
      <span class="token comment">#factor&gt;1表示缩小;factor&lt;1表示放大</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span>factor<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Horizontal(self):                #水平镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Vertically(self):                #垂直镜像
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>rows<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Rotate(self,beta):               #旋转
      <span class="token comment">#beta&gt;0表示逆时针旋转;beta&lt;0表示顺时针旋转</span>
      self<span class="token punctuation">.</span>transform<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">-</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>math<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>beta<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                 <span class="token punctuation">[</span>    <span class="token number">0</span><span class="token punctuation">,</span>            <span class="token number">0</span><span class="token punctuation">,</span>         <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>    def Process(self):
      self<span class="token punctuation">.</span>dst<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">,</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>np<span class="token punctuation">.</span>uint8<span class="token punctuation">)</span>
      <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>rows<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>cols<span class="token punctuation">)</span><span class="token punctuation">:</span>
                src_pos<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>i<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>j<span class="token operator">-</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token punctuation">[</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>z<span class="token punctuation">]</span><span class="token operator">=</span>np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>self<span class="token punctuation">.</span>transform<span class="token punctuation">,</span>src_pos<span class="token punctuation">)</span>
                x<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
                y<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token operator">+</span>self<span class="token punctuation">.</span>center<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
                <span class="token keyword">if</span> x<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>rows <span class="token operator">or</span> y<span class="token operator">&gt;=</span>self<span class="token punctuation">.</span>cols <span class="token operator">or</span> x<span class="token operator">&lt;</span><span class="token number">0</span> <span class="token operator">or</span> y<span class="token operator">&lt;</span><span class="token number">0</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">255</span>
                <span class="token keyword">else</span><span class="token punctuation">:</span>
                  self<span class="token punctuation">.</span>dst<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>self<span class="token punctuation">.</span>src<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span>if __name__=='__main__':    src=cv2.imread('123.jpg',0)    rows = src.shape    cols = src.shape  cv2.imshow('src', src)
img=Img(src,rows,cols,)    img.Rotate(-math.radians(180)) #旋转    img.Process()    cv2.imshow('dst', img.dst)  cv2.waitKey(0)
  后续
  如果想了解更多物联网、智能家居项目知识,可以关注我的项目实战专栏。
或者关注公众号。

编写不易,感谢支持。
页: [1]
查看完整版本: 基于python的空域变换