网站开发技术要学什么,扬中如何优化网站,wordpress 附件插件下载失败,网络管理系统的每个节点都包含一组与管理有关的软件实验2 几何变换与变形
实验2-1#xff1a;图像缩放
实验要求#xff1a;1#xff09;实现一个图像缩放函数#xff0c;可以对输入图像进行任意倍数的缩放#xff1b;
2#xff09;采用双线性插值进行重采样#xff1b;
3#xff09;X,Y方向的缩放倍数参函数参数的形…实验2 几何变换与变形
实验2-1图像缩放
实验要求1实现一个图像缩放函数可以对输入图像进行任意倍数的缩放
2采用双线性插值进行重采样
3X,Y方向的缩放倍数参函数参数的形式传入
4可以只考虑输入图像为3通道8位深度的情况
5不能调用图像处理库的缩放函数来完成 参考函数void Scale(const MyImage input, MyImage output, double sx, double sy);
对于实验一先利用缩放倍数求得缩放后图像的width和height。然后对缩放后图像中的每个像素点(x,y)利用如下公式
xx/sx yy/sy 其中sx和sy分别为x和y轴上图像的缩放倍数求得对应于缩放前图像中的(x,y)满足f(x,y)f(x,y)
由于x和y不一定为整数值因而需要用双线性插值法求得f(x,y)
具体公式如下图 此外对于缩放图像的左部和上部会出现溢出问题因此需要进行边界处理。
而右下部不会出现该问题
具体代码实现如下
#图像缩放
#input_img为输入图像output_img为输出图像,x,y为缩放倍数
import math
import numpy as np
import cv2input_img cv2.imread(img.png)
cv2.namedWindow(input Image, cv2.WINDOW_AUTOSIZE)
cv2.imshow(input Image,input_img)
# 等待按键用来展示图片
# 释放窗口x 0.5
y 1
width,height,dimensioninput_img.shape #原图像的行和列数和维度
new_widthround(width*x) #缩放后图像的行数取整
new_heightround(height*y)
output_imgnp.zeros((new_width,new_height,dimension),dtypenp.uint8)#定义矩阵
for i in range(new_width) :for j in range(new_height):tempxmath.floor(i/x) #(i,j)还原为原图像的坐标值的整数部分tempymath.floor(j/y)if tempx 0 or tempy 0 or tempx width-1 or tempy height-1:output_img[0,j,:] input_img[0,tempy,:] #处理缩放后图像在上和左可能出现边界溢出问题output_img[i,0,:] input_img[tempx,0,:] #右下边界不会出现溢出问题#对其余像素进行处理else:# 计算原图像坐标减去新图像坐标的小数部分a i / x - tempxb j / y - tempyoutput_img[i,j,:]input_img[tempx,tempy,:]*(1-a)*(1-b) (1-a)*b*input_img[tempx,tempy1,:] a*(1-b)*input_img[tempx1,tempy,:] a*b*input_img[tempx1,tempy1,:]cv2.imshow(Scaled Image, output_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行结果 实验2-2图像变形 实验2-2根据实验提供的公式逐步求出所需变量即可。
具体代码实现如下
#图像变形
import math
import numpy as np
import cv2input_img cv2.imread(god.png)
cv2.namedWindow(input Image, cv2.WINDOW_AUTOSIZE)
cv2.imshow(input Image,input_img)width,height,dimensioninput_img.shape #原图像的行和列数和维度
output_imgnp.zeros((width,height,dimension),dtypenp.uint8)#定义矩阵
for i in range(width):for j in range(height):#中心归一化tempx(i-0.5*width)/(0.5*width)tempy(j-0.5*height)/(0.5*height)#计算r和θrmath.sqrt(math.pow(tempx,2)math.pow(tempy,2))xitamath.pow((1-r),2)if r1:xtempxytempyelse:xmath.cos(xita)*tempx-math.sin(xita)*tempyymath.sin(xita)*tempxmath.cos(xita)*tempy#上述x和y是中心归一化之后的坐标值根据公式反推图像的x和y值#必须使用(uint16()函数进行处理坐标将其转化成无符号16位的int类型否则坐标索引会出错#uint8保存的数据是0-255. uint16 保存的数据是0-65535.old_xint((x 1)*0.5*width)old_yint((y 1)*0.5*height)output_img[i,j,:]input_img[old_x,old_y,:]
cv2.imshow(Reshaped Image, output_img)
cv2.waitKey(0)
cv2.destroyAllWindows()运行结果