利用easygui实现一个简易Opencv的小系统
import numpy as np
from matplotlib import pyplot as plt
import torch
import cv2 as cv
import easygui as g
import sys
def winname(name):
return name.encode('gbk').decode(errors='ignore')
def fake_color_image(image_gray):
r = 0
g=0
b=0
height = image_gray.shape
weight = image_gray.shape
out_color_image = np.zeros((height,weight,3),np.uint8)
for i in range(height):
for j in range(weight):
val = image_gray
if (val <128):
r = 0
elif (val <192):
r=255/64*(val-128)
else:
r = 255
if (val < 64):
g = 255 / 64 * val
elif (val < 192):
g = 255
else:
g = -255 / 63 * (val - 192) + 255
if (val < 64):
b = 255
elif (val < 128):
b = -255 / 63 * (val - 192) + 255
else:
b = 0
out_color_image = b
out_color_image = g
out_color_image = r
cv.imshow("fake color image",out_color_image)
def image_gray1(img_gray):
img_gray1 = img_gray.copy()
img_gray1 = (img_gray[:, :, 0] + img_gray[:, :, 1] + img_gray[:, :, 2]) / 3# 第一种方式为对各个通道求均值
img_gray1 = img_gray1.astype(np.uint8)# 猪油数组类型为unit类型时,才会认为是一张图片
cv.imshow("gray image 1",img_gray1)
cv.waitKey(1000)
def image_gray2(img_gray):
img_gray2 = img_gray.copy()
img_gray2 = img_gray[:, :, 0] * 0.11 + img_gray[:, :, 1] * 0.59 + img_gray[:, :, 2] * 0.3
img_gray2 = img_gray2.astype(np.uint8)# GRAY=0.3*R+0.59*G+0.11*B:
cv.imshow("gray image 2",img_gray2)
cv.waitKey(10)
plt.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
plt.rcParams['axes.unicode_minus']=False # 正常显示负号
a=''
filename = ''
img =np.ones((3,3),dtype=np.uint8)
msg="请输入您想要完成的任务(建议您第一步先打开图片)"
title='第一次作业'
choice=('打开图片','退出')
a=g.buttonbox(msg=msg,title=title,choices=choice)
if a == '打开图片':
filename = g.fileopenbox(msg="请打开一个jpg文件")
img = cv.imread(filename)
msg1 = "选择您想要实现的功能"
title1 = '第一次作业'
choice1 = ('灰度化1', '灰度化2', '伪彩色', '直方图', '显示图片', '退出')
q=1
while q:
b=g.buttonbox(msg=msg1,title=title1,choices=choice1)
#while b!='退出':
if b == '灰度化1':
image_gray1(img)
elif b == '灰度化2':
image_gray2(img)
elif b == '显示图片':
cv.imshow("original image", img)
cv.waitKey(1000)
elif b == '伪彩色':
img_gray = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
img_gray = img_gray.astype(np.uint8) #GRAY=0.3*R+0.59*G+0.11*B:
fake_color_image(img_gray)#显示伪彩色图
elif b == '直方图':
img_gray = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
img_gray = img_gray.astype(np.uint8)
pix = []
height = img_gray.shape
weight = img_gray.shape
for i in range(height):
for j in range(weight):
pix.append(int(img_gray))
# pix1=[]
# for i in range(256):
# number = pix.count(i)
# pix1.append(number)
pix = np.array(pix)
# data:必选参数,绘图数据
# bins:直方图的长条形数目,可选项,默认为10
# normed:是否将得到的直方图向量归一化,可选项,默认为0,代表不归一化,显示频数。normed=1,表示归一化,显示频率。
# facecolor:长条形的颜色
# edgecolor:长条形边框的颜色
# alpha:透明度
plt.hist(pix,bins=256,range=) #绘制直方图(第二种方式的)
plt.xlabel( " gray number " )
plt.ylabel( " number " )
plt.title("灰度直方图")
plt.show()
else:
q=0
cv.waitKey(0)
放到代码框里咯?{:10_254:}那样方便阅读 import numpy as np
from matplotlib import pyplot as plt
import torch
import cv2 as cv
import easygui as g
import sys
def winname(name):
return name.encode('gbk').decode(errors='ignore')
def fake_color_image(image_gray):
r = 0
g=0
b=0
height = image_gray.shape
weight = image_gray.shape
out_color_image = np.zeros((height,weight,3),np.uint8)
for i in range(height):
for j in range(weight):
val = image_gray
if (val <128):
r = 0
elif (val <192):
r=255/64*(val-128)
else:
r = 255
if (val < 64):
g = 255 / 64 * val
elif (val < 192):
g = 255
else:
g = -255 / 63 * (val - 192) + 255
if (val < 64):
b = 255
elif (val < 128):
b = -255 / 63 * (val - 192) + 255
else:
b = 0
out_color_image = b
out_color_image = g
out_color_image = r
cv.imshow("fake color image",out_color_image)
def image_gray1(img_gray):
img_gray1 = img_gray.copy()
width = img_gray1.shape
height = img_gray1.shape
for i in range(height):
for j in range(width):
img_gray1= (int(img_gray) + int(img_gray) + int(img_gray))/ 3#强行int类型,实现了灰度化
# img_gray1 = (img_gray[:, :, 0] + img_gray[:, :, 1] + img_gray[:, :, 2]) / 3# 第一种方式为对各个通道求均值
img_gray1 = img_gray1.astype(np.uint8)# 猪油数组类型为unit类型时,才会认为是一张图片
cv.imshow("gray image 1",img_gray1)
cv.waitKey(1000)
def image_gray2(img_gray):
img_gray2 = img_gray.copy()
img_gray2 = img_gray[:, :, 0] * 0.11 + img_gray[:, :, 1] * 0.59 + img_gray[:, :, 2] * 0.3
img_gray2 = img_gray2.astype(np.uint8)# GRAY=0.3*R+0.59*G+0.11*B:
cv.imshow("gray image 2",img_gray2)
cv.waitKey(10)
plt.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
plt.rcParams['axes.unicode_minus']=False # 正常显示负号
a=''
filename = ''
img =np.ones((3,3),dtype=np.uint8)
msg="请输入您想要完成的任务(建议您第一步先打开图片)"
title='第一次作业'
choice=('打开图片','退出')
a=g.buttonbox(msg=msg,title=title,choices=choice)
if a == '打开图片':
filename = g.fileopenbox(msg="请打开一个jpg文件")
img = cv.imread(filename)
msg1 = "选择您想要实现的功能"
title1 = '第一次作业'
choice1 = ('灰度化1', '灰度化2', '伪彩色', '直方图', '显示图片', '退出')
q=1
while q:
b=g.buttonbox(msg=msg1,title=title1,choices=choice1)
#while b!='退出':
if b == '灰度化1':
image_gray1(img)
elif b == '灰度化2':
image_gray2(img)
elif b == '显示图片':
cv.imshow("original image", img)
cv.waitKey(1000)
elif b == '伪彩色':
img_gray = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
img_gray = img_gray.astype(np.uint8) #GRAY=0.3*R+0.59*G+0.11*B:
fake_color_image(img_gray)#显示伪彩色图
elif b == '直方图':
img_gray = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
img_gray = img_gray.astype(np.uint8)
pix = []
height = img_gray.shape
weight = img_gray.shape
for i in range(height):
for j in range(weight):
pix.append(int(img_gray))
pix1=[]
for i in range(256):
number = pix.count(i)
pix1.append(number)
# pix = np.array(pix)
pix1 = np.array(pix1)
# data:必选参数,绘图数据
# bins:直方图的长条形数目,可选项,默认为10
# normed:是否将得到的直方图向量归一化,可选项,默认为0,代表不归一化,显示频数。normed=1,表示归一化,显示频率。
# facecolor:长条形的颜色
# edgecolor:长条形边框的颜色
# alpha:透明度
# plt.hist(pix,bins=256,range=) #绘制直方图(第二种方式的)
plt.plot(pix1) #利用plot直接根据每个像素点的个数直接绘制直方图
plt.xlabel( " gray number " )
plt.ylabel( " number " )
plt.title("灰度直方图")
plt.show()
else:
q=0
cv.waitKey(0) 是在抱歉,第一次投稿,有很多不清楚的地方还请见谅
页:
[1]