利用opencv以及easygui实现的简易的视觉系统(一) 彩色灰度化 彩色直方图 灰色图像的伪彩色图像

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  #强行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[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)
                # pix = np.array(pix)
                pix1 = np.array(pix1)

                # data:必选参数,绘图数据
                # bins:直方图的长条形数目,可选项,默认为10
                # normed:是否将得到的直方图向量归一化,可选项,默认为0,代表不归一化,显示频数。normed=1,表示归一化,显示频率。
                # facecolor:长条形的颜色
                # edgecolor:长条形边框的颜色
                # alpha:透明度
                # plt.hist(pix,bins=256,range=[0,255])     #绘制直方图(第二种方式的)
                plt.plot(pix1)              #利用plot直接根据每个像素点的个数直接绘制直方图
                plt.xlabel( " gray number " )
                plt.ylabel( " number " )
                plt.title("灰度直方图")
                plt.show()
            else:
                q=0
cv.waitKey(0)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值