将四角填充不同颜色

该博客主要展示了如何使用OpenCV库对图像进行处理,包括按指定尺寸缩放图像,沿不同轴翻转,进行平移和旋转操作。此外,还涉及到了图像的文字标注和平面变换,如水平和垂直拼接,以及创建黑白图案。这些技术广泛应用于计算机视觉和图像分析领域。
摘要由CSDN通过智能技术生成
import cv2
img = cv2.imread("3.1.jpeg")
#图像按指定宽高进行缩放
#按宽100高400的大小进行缩小
dst1 = cv2.resize(img,(100,400))
dst2 = cv2.resize(img,(400,400))
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.waitKey()
cv2.destroyAllWindows()

#图像按照指定比例进行缩放
dst3 = cv2.resize(img,None,fx=1 / 3,fy=1 / 2)
dst4 = cv2.resize(img,None,fx=2,fy=2)
cv2.imshow("dst3",dst3)
cv2.imshow("dst4",dst4)
cv2.waitKey()
cv2.destroyAllWindows()

#参数为0沿x轴翻转,参数正数沿y轴翻转,参数为负数同时沿x轴y轴翻转
dst1 = cv2.flip(img,0)
dst2 = cv2.flip(img,1)
dst3 = cv2.flip(img,-1)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.imshow("dst3",dst3)
cv2.waitKey()
cv2.destroyAllWindows()

#图像向右下方平移
import numpy as np
#读取图像行数
rows = len(img)
#读取图像列数
cols = len(img[0])
#横坐标向右移动50像素,纵坐标向下移动100像素
#M = [[1,0,水平移动的距离],[0,1,竖直移动的距离]]      距离为正数向下向右,负数向上向左
M = np.float32([[1,0,50],[0,1,100]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()

#让图像逆时针旋转
#图像的中心点
center = (rows/2,cols/2)
#以图像为中心,逆时针旋转30°,缩放0.8倍
M = cv2.getRotationMatrix2D(center,30,0.8)
#按照M进行仿射
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("dst",dst)
cv2.waitKey()
cv2.destroyAllWindows()


dst1 = cv2.resize(img,(400,400))
dst2 = cv2.resize(img,(400,200))
dst3 = cv2.resize(img,(400,200))
fontStyle = cv2.FONT_HERSHEY_TRIPLEX
#参数依次为:图像,文字,文字左下角坐标,字体样式,大小,颜色
cv2.putText(dst2,"WQ",(20,90),fontStyle,1,(0,0,255))
cv2.putText(dst3,"20040420",(20,90),fontStyle,1,(0,0,255))

dst4 = cv2.resize(img,(200,200))
dst5 = cv2.resize(img,(200,200))
dst5 = cv2.flip(dst5,1)
dst8 = cv2.resize(img,(200,200))
dst8 = cv2.flip(dst8,0)
dst9 = cv2.resize(img,(200,200))
dst9 = cv2.flip(dst9,-1)

dst6 = cv2.resize(img,(200,200))
rows = len(dst6)
#读取图像列数
cols = len(dst6[0])
#横坐标向右移动50像素,纵坐标向下移动100像素
#M = [[1,0,水平移动的距离],[0,1,竖直移动的距离]]      距离为正数向下向右,负数向上向左
M = np.float32([[1,0,-100],[0,1,-50]])
dst6 = cv2.warpAffine(dst6,M,(cols,rows))

black_white = cv2.resize(img,(200,400))
for i in range(0,20):
    for j in range(0,200):
        black_white[i,j] = [255,255,255]
for i in range(40,60):
    for j in range(0,200):
        black_white[i,j] = [0,0,0]
for i in range(80,100):
    for j in range(0,200):
        black_white[i,j] = [255,255,255]
for i in range(120,140):
    for j in range(0,200):
        black_white[i,j] = [0,0,0]
for i in range(140,160):
    for j in range(0,200):
        black_white[i,j] = [255,255,255]

dst10 = cv2.resize(img,(200,200))
center = (rows/2,cols/2)
M = cv2.getRotationMatrix2D(center,30,0.5)
dst10 = cv2.warpAffine(dst10,M,(cols,rows))

img_h = np.hstack((dst1,dst1))  #水平拼接
img_v = np.vstack((dst2,dst2))  #竖直拼接

img2 = np.vstack((dst2,dst3))
img2 = np.hstack((dst1,img2))
img3 = np.hstack((dst4,dst5,dst6))
img4 = np.hstack((dst8,dst9,dst10))
img3 = np.vstack((img3,img4))
img3 = np.hstack((img3,black_white))
img3 = np.vstack((img2,img3))

cv2.imshow("20040420",img3)
cv2.waitKey()
cv2.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值