python将图像分割成两半

Python010

python将图像分割成两半,第1张

import os

import re

import sys

import time

import random

# add system headers here...

#导入cv模块

import cv2 as cv

#读取图像,支持 bmp、jpg、png、tiff 等常用格式

height = 0

length = 0

key = 0

picPath = "E:\\python3.4.0-amd\\project\\imageProcess\\tamamo.jpg"

if not os.path.exists(picPath):

    print("picture not exists! exit!")

    sys.exit()

srcImage = cv.imread(picPath)

if srcImage is None:

    print("read picture failed! exit!")

    sys.exit()

size = srcImage.shape

height = size[0]

length = size[1]

print("srcImage: height(%u) length(%u)"%(height,length))

#显示原图

#cv.imshow("srcImage",srcImage)

#创建窗口并显示图像

mid = int(length / 2)

leftImage = srcImage[0:height, 0:mid]

cv.namedWindow("leftImage",cv.WINDOW_NORMAL)

cv.resizeWindow("leftImage", mid, height)

cv.imshow("leftImage",leftImage)

rightIamge = srcImage[0:height, mid:length]

cv.namedWindow("rightIamge",cv.WINDOW_NORMAL)

cv.resizeWindow("rightIamge", mid, height)

cv.imshow("rightIamge",rightIamge)

cv.waitKey(0)

#释放窗口

cv.destroyAllWindows()

一、函数说明

在使用python作图时,应用最广的就是matplotlib包,但我们平时使用matplotlib时主要是画一些简单的图表,很少有涉及分段函数。本次针对数值实验中两个较为复杂的函数,使用其构建分段函数图像。

二、图像代码

2.11、函数公式:

y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)

2.12、代码如下:

import numpy as np

import matplotlib.pyplot as plt

def sgn(x):

if x>0:

return 1

elif x<0:

return -1

else:

return 0

t=np.arange(0,1,0.01)

y=[]

for i in t:

y_1=4*np.sin(4*np.pi*i)-sgn(i-0.3)-sgn(0.72-i)

y.append(y_1)

plt.plot(t,y)

plt.xlabel("t")

plt.ylabel("y")

plt.title("Heavsine")

plt.show()

2.13、运行结果如下:

81036331d721706ae12808beb99b9574.png

2.21、函数公式:

479029.html

2.22、代码如下:

import numpy as np

import matplotlib.pyplot as plt

def g(x):

if x>0:

return x

else:

return 0

t=np.arange(0,1,0.01)

y=[]

for i in t:

y_1=g(i*(1-i))*np.sin((2*np.pi*1.05)/(i+0.05))

y.append(y_1)

plt.plot(t,y)

plt.xlabel("t")

plt.ylabel("y")

plt.title("TimeSine")

plt.show()

#初始化一个矩形np.max(marks)+1行,3列,默认值为0

colorTab = np.zeros((np.max(marks)+1,3))

#遍历数组,给每行的3列赋值,就是RGB颜色值,8位的

for i in range(len(colorTab)):

    aa = np.random.uniform(0,255)

    bb = np.random.uniform(0,255)

    cc = np.random.uniform(0,255)

    colorTab[i] = np.array([aa,bb,cc],np.uint8)

#初始化另一个跟img图像形状大小一样的图像,一副黑色图像

bgrImage = np.zeros(img.shape,np.uint8)

#遍历marks形状的行列

for i in range(marks.shape[0]):

    for j in range(marks.shape[1]):

        index = marks[i][j]

        #判断是不是区域与区域之间的分界,如果是边界(-1),则使用白色显示

        if index == -1:

            bgrImage[i][j] = np.array([255,255,255]) #像素点设置位白色

        else:

            bgrImage[i][j] = colorTab[index]    #像素点设置位上边随机生成的颜色值

#显示处理后的图像图像

cv2.imshow('After ColorFill',bgrImage)

#总结,先生成一个跟marks相同数量的row*col的一张颜色表,然后创建一个跟marks相同大小的一副黑色图像

#最后对黑色图像画出白色边界和内部随机彩色像素值