暂无图片
暂无图片
1
暂无图片
暂无图片
1
暂无图片

读入图像并且绘制图像的直方图

原创 kayla 2023-03-26
297

# 读入图像并绘制图像直方图
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image #导入PIL库
import numpy as np

# 图像中引入中文字体
plt.rc("font",family='MicroSoft Yahei')

"""自己编写函数绘制直方图"""
def my_imhist(img):
"""
根据图像灰度值绘制直方图

Parameters
-----------
h:height of image
w:width of image
hist:用于储存统计图像每个像素点的灰度值个数
img:图像的灰度值

"""
# 获取图像的高与宽
h, w = img.shape
# 16位图像,拥有0-65535灰度值,首先各灰度频数都置为0
hist = [0] * 65536
#将图像第(i,j)个像素点对应的灰度值做计数储存在hist中
for i in range(h):
for j in range(w):
hist[img[i, j]] += 1
return hist

#读入图像
img = Image.open(r"axon01.tif")
#获取图像灰度值
# imgGray = img.convert('L')
img = np.asarray(img)
myhist=my_imhist(img)
plt.figure(figsize=(5,1))
plt.bar(range(65536), myhist, width=1)
plt.xlim(0,6000)
plt.title("自己编写my_imhist函数绘制的直方图")

"""调用函数hist绘制直方图"""
img = img.reshape(-1) #将图像展开成一个一维的numpy数组
plt.figure(figsize=(5,1))
plt.hist(img,128) #128
plt.title("调用hist绘制直方图")
plt.xlim(0,6000)
plt.show

# 读入图像并绘制图像直方图
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image #导入PIL库
import numpy as np

# 图像中引入中文字体
plt.rc("font",family='MicroSoft Yahei')

"""自己编写函数绘制直方图"""
def my_imhist(img):
    """
    根据图像灰度值绘制直方图
    
    Parameters
    -----------
    h:height of image
    w:width of image
    hist:用于储存统计图像每个像素点的灰度值个数
    img:图像的灰度值
    
    """
    # 获取图像的高与宽
    h, w = img.shape   
    # 16位图像,拥有0-65535灰度值,首先各灰度频数都置为0
    hist = [0] * 65536  
    #将图像第(i,j)个像素点对应的灰度值做计数储存在hist中
    for i in range(h):
        for j in range(w):
            hist[img[i, j]] += 1
    return hist

#读入图像
img = Image.open(r"axon01.tif")
#获取图像灰度值
# imgGray = img.convert('L')
img = np.asarray(img)
myhist=my_imhist(img)
plt.figure(figsize=(5,1))
plt.bar(range(65536), myhist, width=1)
plt.xlim(0,6000)
plt.title("自己编写my_imhist函数绘制的直方图")

"""调用函数hist绘制直方图"""
img = img.reshape(-1)  #将图像展开成一个一维的numpy数组
plt.figure(figsize=(5,1))
plt.hist(img,128) #128
plt.title("调用hist绘制直方图")
plt.xlim(0,6000)
plt.show
复制

# 读入图像并绘制图像直方图
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image #导入PIL库
import numpy as np

# 图像中引入中文字体
plt.rc("font",family='MicroSoft Yahei')

"""自己编写函数绘制直方图"""
def my_imhist(img):
"""
根据图像灰度值绘制直方图

Parameters
-----------
h:height of image
w:width of image
hist:用于储存统计图像每个像素点的灰度值个数
img:图像的灰度值

"""
# 获取图像的高与宽
h, w = img.shape
# 16位图像,拥有0-65535灰度值,首先各灰度频数都置为0
hist = [0] * 65536
#将图像第(i,j)个像素点对应的灰度值做计数储存在hist中
for i in range(h):
for j in range(w):
hist[img[i, j]] += 1
return hist

#读入图像
img = Image.open(r"axon01.tif")
#获取图像灰度值
# imgGray = img.convert('L')
img = np.asarray(img)
myhist=my_imhist(img)
plt.figure(figsize=(5,1))
plt.bar(range(65536), myhist, width=1)
plt.xlim(0,6000)
plt.title("自己编写my_imhist函数绘制的直方图")

"""调用函数hist绘制直方图"""
img = img.reshape(-1) #将图像展开成一个一维的numpy数组
plt.figure(figsize=(5,1))
plt.hist(img,128) #128
plt.title("调用hist绘制直方图")
plt.xlim(0,6000)
plt.show

# 读入图像并绘制图像直方图
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image #导入PIL库
import numpy as np

# 图像中引入中文字体
plt.rc("font",family='MicroSoft Yahei')

"""自己编写函数绘制直方图"""
def my_imhist(img):
    """
    根据图像灰度值绘制直方图
    
    Parameters
    -----------
    h:height of image
    w:width of image
    hist:用于储存统计图像每个像素点的灰度值个数
    img:图像的灰度值
    
    """
    # 获取图像的高与宽
    h, w = img.shape   
    # 16位图像,拥有0-65535灰度值,首先各灰度频数都置为0
    hist = [0] * 65536  
    #将图像第(i,j)个像素点对应的灰度值做计数储存在hist中
    for i in range(h):
        for j in range(w):
            hist[img[i, j]] += 1
    return hist

#读入图像
img = Image.open(r"axon01.tif")
#获取图像灰度值
# imgGray = img.convert('L')
img = np.asarray(img)
myhist=my_imhist(img)
plt.figure(figsize=(5,1))
plt.bar(range(65536), myhist, width=1)
plt.xlim(0,6000)
plt.title("自己编写my_imhist函数绘制的直方图")

"""调用函数hist绘制直方图"""
img = img.reshape(-1)  #将图像展开成一个一维的numpy数组
plt.figure(figsize=(5,1))
plt.hist(img,128) #128
plt.title("调用hist绘制直方图")
plt.xlim(0,6000)
plt.show
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

Zixin Huo
暂无图片
1年前
评论
暂无图片 0
# 读入图像并绘制图像直方图 import matplotlib.pyplot as plt import numpy as np from PIL import Image #导入PIL库 import numpy as np
1年前
暂无图片 点赞
评论