opencv-python处理图片的一些列操作之几何变换

一般的变换放大缩小(都是由插值算法得到的,但是都会有损失,目前的超分辨率网络,应该是最好的图像算法,到时候会讲一下超分辨率网络)
直接进入opencv函数(具体插值算法可百度,简单的很)

resize改变图片大小

例子

import cv2
image=cv2.imread("/home/dfy/Pictures/Camera_photo/Camera_photo/sss.jpg")


image1=cv2.resize(image,(1300,1200))
cv2.imshow("",image1)
cv2.waitKey(0)


if __name__ == '__main__':
    print()

calcHist函数

hist=cv2.calcHist(images="",channels="",mask="",hist="",histSize="",accumulate="")
images输入图像
channels 输入图像的通道
mask掩模图计算全部图的时候直接为None如果计算部分图像需要配相应的mask
histSize灰度级的个数
range像素值范围
例子

import cv2
from matplotlib import pyplot as plt

def main():
    image = cv2.imread("/home/dfy/Pictures/Camera_photo/Camera_photo/page2.jpg")
    chans=cv2.split(image)
    colors=("b","g","r")

    plt.figure()#创建画布
    plt.title("tu")
    plt.xlabel("bin")
    plt.ylabel("#of pixes")
    for (chan,color) in zip(chans,colors):
        hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
        plt.plot(hist,color=color)
        plt.xlim([0,256])
    plt.show()
    input()


if __name__ == '__main__':
    main()

使用几何变换实现哈哈镜子特效
输人图像f(x, y),宽高分别为Width和Height,设置图像中心坐标Center (cx, xy)为缩放中心点,图像上任意一点到中心点的相对坐标x-Cx, ty=y- -cY。 哈哈镜效果分为图像拉伸放大和图像缩小。
对于图像拉伸放大,设置图像变换的半径为radius,哈哈镜变换后的图像为p(x, y)。

x = ( t x / 2 ) × ( s q r t ( t x × t x + t y × t y ) / r a d i u s ) + c x x= (tx/2)\times (sqrt(tx \times tx+ty \times ty)/radius) +cx x=(tx/2)×(sqrt(tx×tx+ty×ty)/radius)+cx
y = ( t y / 2 ) × ( s q r t ( t x × t x + t y × t y ) / r a d i u s ) + c y y= (ty/2) \times (sqrt(tx \times tx+ty \times ty)/radius) +cy y=(ty/2)×(sqrt(tx×tx+ty×ty)/radius)+cy

对于图像缩小,设置图像变换的半径为radius,哈哈镜变换后的图像为p(x, y)。

x = c o s ( a t a n 2 ( t y , t x ) ) 12 ( s q r t ( t x x t x + t y x t y ) + c x x= {cos(atan2(ty, tx))}{12}{(sqrt(txxtx +tyxty) +cx} x=cos(atan2(ty,tx))12(sqrt(txxtx+tyxty)+cx
y = s i n ( a t a n 2 ( t y , b x ) ) 12 ( s q r t ( t x x t x + t y x y ) + c y y= sin(atan2(ty, bx))12 (sqrt(txxtx+tyxy) +cy y=sin(atan2(ty,bx))12(sqrt(txxtx+tyxy)+cy
例子1


#可以自己调整中心点
import cv2
import math


def maxframe():
    frame = cv2.imread("/home/dfy/PycharmProjects/GAN-TTS-master/sss.jpg")
    height, width, n = frame.shape
    center_x = width / 2
    center_y = height / 2

    randius = 400  # 直径
    real_randius = int(randius / 2)  # 半径
    new_data = frame.copy()
    for i in range(width):
        for j in range(height):
            tx = i - center_x
            ty = j - center_y
            distance = tx ** 2 + tx ** 2
            # 为了保证选择的像素是图片上的像素
            if distance < randius ** 2:
                new_x = tx / 2
                new_y = ty / 2
                # 图片的每个像素的坐标按照原来distance 之后的distance(real_randius**2)占比放大即可
                new_x = int(new_x * math.sqrt(distance) / real_randius + center_x)
                new_y = int(new_y * math.sqrt(distance) / real_randius + center_y)
                # 当不超过new_data 的边界时候就可赋值
                if new_x < width and new_y < height:
                    new_data[j][i][0] = frame[new_y][new_x][0]
                    new_data[j][i][1] = frame[new_y][new_x][1]
                    new_data[j][i][2] = frame[new_y][new_x][2]
    cv2.imshow("", new_data)
    cv2.waitKey(0)



def MinFrame():
    frame = cv2.imread("/home/dfy/PycharmProjects/GAN-TTS-master/sss.jpg")
    height,width,n=frame.shape
    center_x=width/2
    center_y=height/2
    new_data=frame.copy()
    for i in range(width):
        for j in range(height):
            tx=i-center_x
            ty=j-center_y
            theta=math.atan2(ty,tx)
            radius=math.sqrt(tx**2+ty**2)
            new_x=int(center_x+math.sqrt(radius)*20*math.cos(theta))
            new_y=int(center_y+math.sqrt(radius)*20*math.sin(theta))
            if new_x<0 and new_x>width:
                new_x=0
            elif new_y<0 and new_y>height:
                new_y=0
            else:
                new_data[j][i][0] = frame[new_y][new_x][0]
                new_data[j][i][1] = frame[new_y][new_x][1]
                new_data[j][i][2] = frame[new_y][new_x][2]
    cv2.imshow("", new_data)
    cv2.waitKey(0)

if __name__ == '__main__':
    # maxframe()
    MinFrame()

你可能感兴趣的:(opencv-python)