一点、两点、三点透视投影的python3实现-计算机图形学
结果
总结
- 网上都很容易找到一点、两点透视投影的变换矩阵,唯独三点透视矩阵不好找,偏偏发现一个却是和两点的矩阵一样,阿巴阿巴。。。。也没有办法给人家留言说你贴错图了emmmm,最后只好通读几个PPT总结了一下三点透视变换的矩阵,就是下面几个矩阵的乘积
# 平移到LMN
mtranslation = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [L, M, N, 1]], dtype='float32')
# 沿y轴旋转
mrotatey = np.array([[math.cos(xita), 0, -math.sin(xita), 0], [0, 1, 0, 0],[math.sin(xita), 0, math.cos(xita), 0], [0, 0, 0, 1]],dtype='float32')
# 沿x轴旋转
mrotatex = np.array([[1, 0, 0, 0], [0, math.cos(five), math.sin(five), 0],[0, -math.sin(five), math.cos(five), 0], [0, 0, 0, 1]],dtype='float32')
# 透视
mperspective = np.array([[1, 0, 0, P], [0, 1, 0, Q], [0, 0, 1, R], [0, 0, 0, 1]], dtype='float32')
# 向xoy平面做正投影
mprojectionxy = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]], dtype='float32')
- 然后numpy的矩阵相乘是
np.matmul
,元素相乘是np.multiply
或者*
,第一天用后者算出来的结果整得我人傻了 - 角度表示是先将派分割成180再乘,比如30度是这样表示的
degree = math.pi / 180 * 30
,而不是degree = 30
源代码
后续如果有更新,最新代码都在我的Github仓库
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File : perspectiveone.py
@Time : 2020/12/11 21:45:11
@Author : Kearney
@Version : 0.0.0
@Contact : 191615342@qq.com
@License : GPL 3.0
@Desc : 一点透视、两点透视画布横轴x向右增大,纵轴y向下增大ref: https://blog.csdn.net/mylovestart/article/details/8352105https://wenku.baidu.com/view/ce28831e0508763230121275.htmlhttps://www.cnblogs.com/Penglimei/p/9750390.html
'''
import pygame
import sys
import numpy as np
import math# 长方体的八个点(x, y ,z, 1),其他点数的立体图形需要修改draw的参数
points = [[0, 0, 200, 1], [200, 0, 200, 1], [200, 0, 0, 1], [0, 0, 0, 1],[0, 200, 200, 1], [200, 200, 200, 1], [200, 200, 0, 1],[0, 200, 0, 1]]onepoint = [] # 一点透视后的二维坐标
twopoint = [] # 两点透视后的二维坐标
threepoint = [] # 三点透视后的二维坐标# 一点透视参数
L = 100 # 平移位置L, M, N
M = 100
N = 100
D = 300 # 视距
# 两点透视参数
P = 2
R = 3
xita = math.pi / 180 * 65 # 角度xita
Q = 1.5 # 三点透视参数
five = math.pi / 180 * 80 # 角度five,三点透视参数# 一点透视变换矩阵
transMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1 / D], [L, M, 0, 1 + (N / D)]],dtype='float32')# 一点透视:将三维坐标映射到二维
for e in range(0, len(points)):tmp = np.matmul(np.array(points[e], dtype='float32'), transMatrix)tmp = tmp / tmp[3] # 齐次化onepoint.append(tmp.tolist()[:2])# 两点透视变换矩阵
twoMatrix = np.array([[math.cos(xita), 0, 0,(P * math.cos(xita) - R * math.sin(xita))], [0, 1, 0, 0],[math.sin(xita), 0, 0, (P * math.sin(xita) + R * math.cos(xita))],[(L * math.cos(xita) + N * math.sin(xita)), M, 0,(P * (L * math.cos(xita) + N * math.sin(xita))) +(R * (N * math.cos(xita) + L * math.sin(xita))) + 1]],dtype='float32')
# 两点透视降维变换
for e in range(0, len(points)):tmp = np.matmul(np.array(points[e], dtype='float32'), twoMatrix)tmp = tmp / tmp[3] # 齐次化tmp = tmp * 500 # 非必须:放大,太小看不见tmp[0] += 200 # 非必须:整体向右平移,不然和一位透视挡在一起了twopoint.append(tmp.tolist()[:2])# 三点透视变换矩阵
# 平移到LMN
mtranslation = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [L, M, N, 1]], dtype='float32')
# 沿y轴旋转
mrotatey = np.array([[math.cos(xita), 0, -math.sin(xita), 0], [0, 1, 0, 0],[math.sin(xita), 0, math.cos(xita), 0], [0, 0, 0, 1]],dtype='float32')
# 沿x轴旋转
mrotatex = np.array([[1, 0, 0, 0], [0, math.cos(five), math.sin(five), 0],[0, -math.sin(five), math.cos(five), 0], [0, 0, 0, 1]],dtype='float32')
# 透视
mperspective = np.array([[1, 0, 0, P], [0, 1, 0, Q], [0, 0, 1, R], [0, 0, 0, 1]], dtype='float32')
# 向xoy平面做正投影
mprojectionxy = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]], dtype='float32')
threeMatrix = np.matmul(np.matmul(np.matmul(np.matmul(mtranslation, mrotatey), mrotatex),mperspective), mprojectionxy)# 三点透视降维变换
for e in range(0, len(points)):tmp = np.matmul(np.array(points[e], dtype='float32'), threeMatrix)tmp = tmp / tmp[3] # 齐次化tmp = tmp * 800 # 非必须:放大,太小看不见tmp[0] += 600 # 非必须:整体向右平移,不然和一点透视挡在一起了tmp[1] += 100 # 非必须:整体向下平移,不然和一点透视挡在一起了threepoint.append(tmp.tolist()[:2])LINECOLOR = (29, 244, 255) # 颜色设置
BACKYCOLOR = (255, 212, 238)
LINEWIDTH = 3 # 线宽
pygame.init()
screen = pygame.display.set_mode((920, 640))while True:screen.fill(BACKYCOLOR)# 一点透视的图pygame.draw.lines(screen, LINECOLOR, False, onepoint, LINEWIDTH) # 懒人划线pygame.draw.line(screen, BACKYCOLOR, onepoint[3], onepoint[4],LINEWIDTH) # 遮瑕# 补全剩下的线条pygame.draw.line(screen, LINECOLOR, onepoint[0], onepoint[3], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[4], onepoint[7], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[0], onepoint[1], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[0], onepoint[1], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[0], onepoint[4], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[3], onepoint[7], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[2], onepoint[6], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, onepoint[1], onepoint[5], LINEWIDTH)# 两点透视的图pygame.draw.lines(screen, LINECOLOR, False, twopoint, LINEWIDTH)pygame.draw.line(screen, BACKYCOLOR, twopoint[3], twopoint[4], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[0], twopoint[3], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[4], twopoint[7], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[0], twopoint[1], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[0], twopoint[1], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[0], twopoint[4], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[3], twopoint[7], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[2], twopoint[6], LINEWIDTH)pygame.draw.line(screen, LINECOLOR, twopoint[1], twopoint[5], LINEWIDTH)pygame.draw.lines(screen, LINECOLOR, False, threepoint, LINEWIDTH)pygame.draw.line(screen, BACKYCOLOR, threepoint[3], threepoint[4],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[0], threepoint[3],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[4], threepoint[7],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[0], threepoint[1],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[0], threepoint[1],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[0], threepoint[4],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[3], threepoint[7],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[2], threepoint[6],LINEWIDTH)pygame.draw.line(screen, LINECOLOR, threepoint[1], threepoint[5],LINEWIDTH)for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit(0)pygame.display.update()
一点、两点、三点透视投影的python3实现-计算机图形学相关推荐
- 点积和叉积在计算机图形学的应用
点积和叉积在计算机图形学中,是最为基础且重要的概念,初学者弄清它的概念的应用,是很重要的.最后一节,是为了加强理解记录,如果不看也是可以的,大家选择观看,有兴趣可以去看原视频,结合我的笔记. 前置知识 ...
- 一点、两点、三点透视投影
在图形学的书上看到一点.两点.三点透视投影,有一些公式推理,但都是把模型放在某个特定地方推导投影矩阵,实践中我发现,用一般的透视投影,把相机放到特定地方,就可以实现一点.两点.三点透视投影.这样就不需 ...
- 计算机图形学基础之一点两点三点透视图
嘿嘿,如果你想应付一下这个计算机图形学的作业,那么这篇文章在合适不过了. 想看懂代码,那么只需要明白齐次坐标在各种变换下的变化即可. 对了,别忘记在java工程中补上一张"backgroun ...
- 计算机图形学 学习笔记(八):三维图形变换:三维几何变换,投影变换(平行/ 透视 投影)
接上文 计算机图形学 学习笔记(七):二维图形变换:平移,比例,旋转,坐标变换等 通过三维图形变换,可由简单图形得到复杂图形,三维图形变化则分为三维几何变换和投影变换. 6.1 三维图形几何变换 三维 ...
- 计算机图形学-3D观察与图像渲染流水线-投影全解析
本文目标: 理清OpenGL在3D观察的整个流程. 清楚各个专业术语的含义. 对坐标系变换的数学有所掌握. 1 三维观察与观察流程 1.1 三维观察与照相观察的对比 三维观察过程与使用照相机拍摄照片类 ...
- 计算机图形学三维投影程序,三维计算机图形学
<三维计算机图形学>是科学出版社出版.本书共分17章,内容包括IDE环境与编程基础.基本语法与数据类型.运算操作.流程控制.面向对象程序设计.用户界面编程.文件系统管理.图形图像多媒体编程 ...
- 【计算机图形学基础】投影矩阵
最近在重温计算机图形学的基础知识,期望能做到温故知新,加深对其的理解,以便能从容应对工作中各种情况. 小弟水平有限,若有不正确之处,欢迎大家批评指正. 相关文章链接: [计算机图形学基础]线性 ...
- 《每天一点学习笔记》-初学python3~3
<每天一点学习笔记>-初学python3 这几天乱遭事有点多,没来得及写,就不写第几天了,综合整理一下,简单记录这几天学的内容吧.简单来说我看了一些入门的基础知识,有的是视频,也有别人写的 ...
- 计算机图形学作业一:旋转与投影
计算机图形学 作业一:旋转与投影 文章目录 计算机图形学 作业一:旋转与投影 1.题目概述: 2.知识储备: 2.1 MVP变换 2.2罗德里格斯旋转公式 3.解答过程: 4.总结思考: 1.题目概述 ...
最新文章
- 详细说明Spring--AOP
- 手把手教你 MongoDB 的安装与详细使用(一)
- java自定义监听器例子_Spring 实现自定义监听器案例
- python 东哥 with open_python 连接redis cluster
- 【Jmeter篇】Jmeter踩坑记(一)
- Python《回车桌面图片》
- 华为P30 Pro外观无悬念:双曲面水滴屏 屏占比超高
- count(1),count(*),count(rowid)
- (2) 怎么学习IFC (Industry Foundation Class)
- 用python实现单词本功能
- 《暗时间》读书笔记与读后感
- centOS安装vnc远程桌面
- python实现——40W告警日志分析(上)
- PHP在线横幅广告店标LOGO制作网站源码
- 嵌入式了解 以及学习路线
- bat批处理与adb命令的配合
- 一起来DIY一个人工智能实验室吧
- c语言 爱心 (koi奋斗中~~)
- 倒计时1天 | 大势智慧2022新品发布会全面而来!
- 路何去何从-第三方Oracle服务商
热门文章
- 超级计算机的等级,亿亿次级别运算曙光初现-超级计算机,千万亿次,TeraScale,万万亿次,ExaScale ——快科技(驱动之家旗下媒体)--科技改变未来...
- [面向对象与多线程综合实验]数据挖掘系统
- c语言编程一张纸对折,关于一张纸的对折问题
- SpringMVC04:数据处理及跳转
- (基础)详解Python实现图像分割增强的两种方法
- 算法很美:01背包问题(动态规划、贪心)
- 全志F133(D1s)芯片 如何在Tina下进行显示旋转?
- freeswitch的呼叫中心模块——mod_callcenter
- 持居住证能落户?子女还能在沪高考
- Jeesite Login 登录 分析