毕设学习之opencv——几何变换

本文介绍了使用OpenCV进行图像操作的基本技巧,包括等比例缩放、区域剪切、像素位移以及像素级别的读写和处理。通过实例展示了如何调整图片大小、裁剪指定区域以及应用矩阵变换,是Python图像处理入门的好资料。
摘要由CSDN通过智能技术生成

opencv

opencv像素的读取和像素的写入:

import cv2
img = cv2.imread('xx.jpg',1) #相对路径,读取彩色图片
(b,g,r)=img[100][100] #RGB图像在opencv读取的顺序是BGR
print(b,g,r)  #打印坐标在(100,100)的像素值
#10 10 ->110 110
for i in range(1,100):
    img[10+i,10+i]=(255,0,0)
cv2.imshow('image',img)#第一参数,图像的名称,第二参数,要写入的图像
cv2.waitKey(0) #cv2.waitKey(1000)   1000ms
cv2.destroyAllWindows()

运行结果:在这里插入图片描述
图片的缩放:

等比例缩放:
#load 2info 3resize 4check
import cv2
print("------------图片的缩放---------------")

img = cv2.imread("xx.jpg",1)
cv2.imshow("Image0",img)
imgInfo = img.shape 
#矩阵的shape属性可以打印矩阵的各种信息,img就是一个矩阵
print(imgInfo)
height = imgInfo[0] #h
width =  imgInfo[1] #w
mode = imgInfo[2] #图片的颜色组成方式 rgb
#1放大 缩小 2等比例 非等比例

#等比例缩放
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
dst = cv2.resize(img,(dstHeight,dstWidth))
#第一个参数,需要缩放的图片,缩放后图片的HW
cv2.imshow("Image1",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

双线性变换不理解!!!

图片的剪切

import cv2

print("-----------图片剪切-----------")
img = cv2.imread("xx.jpg",1)
imgInfo = img.shape
print(imgInfo)
dst = img[50:200,50:240] #目标图像
cv2.imshow("Image",img)
cv2.imshow("DstImage",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述
图片的位移

api的方法:

import cv2
import numpy as np
print("--------------图片移位------------------")
img = cv2.imread("xx.jpg",1)
cv2.imshow("Image",img)

imgInfo = img.shape  #获取图像信息
height = imgInfo[0]
widht = imgInfo[1]
###

matShift = np.float32([[1,0,100],[0,1,200]])#等价于沿着x轴右移100,y移200
dst = cv2.warpAffine(img,matShift,(height,widht))
cv2.imshow("Dstage",dst)

cv2.waitKey(0)

在这里插入图片描述

像素处理的方法:

import cv2
import numpy as np
"""
图像处理:先行后列
(0,0)----------------------x
-
-
-
-             
-
y
"""

img = cv2.imread("xx.jpg",1)
cv2.imshow("Image",img)
imgInfo = img.shape
dst = np.zeros(img.shape,np.uint8)
height = imgInfo[0]
width = imgInfo[1]

for i in range(0,height-200):
    for j in range(0,width-100):
        dst[i+200][j+100]=img[i][j] #原来(0,0)处的像素现在在(200,100)
cv2.imshow("Dstage",dst)
cv2.waitKey(0)

运行结果:

在这里插入图片描述

图片镜像:

import cv2
import numpy as np

img = cv2.imread("xx.jpg",1)
cv2.imshow("Image",img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
print(imgInfo)
newImgInfo = (height*2,width,deep)
dst = np.zeros(newImgInfo,np.uint8)
for i in range(0,height):
    for j in range(0,width):
        dst[i,j]=img[i,j]#i是高度,j是宽度 上部
        dst[2*height-i-1,j]=img[i,j]#下部分
for i in range(0,width):
    dst[height,i]=(0,0,255)#BGR

cv2.imshow("dst",dst)   
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:
在这里插入图片描述

图片复制:(实现思路和镜像一致)

import cv2
import numpy as np
img =cv2.imread("xx.jpg",1)
cv2.imshow("Img",img)
ImgInfo = img.shape
height = ImgInfo[0]
width = ImgInfo[1]
deep = ImgInfo[2]
print(ImgInfo)
newImgInfo = (height*2,width*2,deep)
dst = np.zeros(newImgInfo,np.uint8)
for i in range(0,height):#高度i 宽度j
    for j in range(0,width):
        dst[i,j]=img[i,j]
        dst[i,width+j]=img[i,j]
        dst[height+i,j]=img[i,j]
        dst[height+i,width+j]=img[i,j]
cv2.imshow("Dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果:
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值