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
[0]
weight
= image_gray
.shape
[1]
out_color_image
= np
.zeros
((height
,weight
,3),np
.uint8
)
for i
in range(height
):
for j
in range(weight
):
val
= image_gray
[i
,j
]
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
[i
,j
,0] = b
out_color_image
[i
,j
,1] = g
out_color_image
[i
,j
,2] = r
cv
.imshow
("fake color image",out_color_image
)
def image_gray1(img_gray
):
img_gray1
= img_gray
.copy
()
width
= img_gray1
.shape
[1]
height
= img_gray1
.shape
[0]
for i
in range(height
):
for j
in range(width
):
img_gray1
[i
,j
]= (int(img_gray
[i
,j
, 0]) + int(img_gray
[i
,j
, 1]) + int(img_gray
[i
,j
, 2]))/ 3
img_gray1
= img_gray1
.astype
(np
.uint8
)
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
)
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
)
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
)
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
[0]
weight
= img_gray
.shape
[1]
for i
in range(height
):
for j
in range(weight
):
pix
.append
(int(img_gray
[i
,j
]))
pix1
=[]
for i
in range(256):
number
= pix
.count
(i
)
pix1
.append
(number
)
pix1
= np
.array
(pix1
)
plt
.plot
(pix1
)
plt
.xlabel
( " gray number " )
plt
.ylabel
( " number " )
plt
.title
("灰度直方图")
plt
.show
()
else:
q
=0
cv
.waitKey
(0)
转载请注明原文地址: https://lol.8miu.com/read-24372.html