当我希望出现索引时如何使用numpy.hist [英] How to use numpy.hist when i want the occurence of the indices

查看:82
本文介绍了当我希望出现索引时如何使用numpy.hist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组图像,我想为每个图像的色相值创建一个直方图.因此,我创建了一个长度为180的数组.如果色相值在图像中,则在每个单元格中加1. 最后,我得到了每个色相值都出现的数组,但是当我使用numpy.hist时,y轴是色相值,x轴是色相值.但是我反过来想要它.

这是我的代码:

path = 'path'
sub_path = 'subpath'

sumHueOcc = np.zeros((180, 1), dtype=int) 

print("sumHue Shape")
print(sumHueOcc.shape)

for item in dirs:
    fullpath = os.path.join(path,item)
    pathos = os.path.join(sub_path,item)
    if os.path.isfile(fullpath):
        img = np.array(Image.open(fullpath))

        f, e = os.path.splitext(pathos)

        imgHSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

        print("Img shape")
        print(img.shape)

        # want to work with hue only
        h, s, v = cv2.split(imgHSV)

        # the hue values in one large array
        Z = h.reshape((-1, 1))
        # convert to np.float32
        Z = np.uint32(Z)

        # add 1 for each hue value in the image
        for z in Z:
            sumHueOcc[z] = sumHueOcc[z] + 1

        plt.figure(figsize=(9, 8))
        plt.subplot(311)  # Hue Picture 1
        plt.subplots_adjust(hspace=.5)
        plt.title("Hue Picture 1")
        plt.hist(np.ndarray.flatten(h), bins=180)
        plt.subplot(312)  # Hue Picture 2
        plt.subplots_adjust(hspace=.5)
        plt.title("Hue Picture 2")
        plt.hist(np.ndarray.flatten(Z), bins=180)
        plt.subplot(313)  # Hue Picture 2
        plt.subplots_adjust(hspace=.5)
        plt.title("Sum Occ")
        plt.hist(np.ndarray.flatten(sumHueOcc), bins=180)
        plt.show()

#First Hue Sum
plt.figure(figsize=(9,8))
plt.title("Sum Hue Occ")
plt.hist(np.ndarray.flatten(sumHueOcc), bins=180)
plt.show()

这里是Berriels代码,从半色调"更改为全色调":

print(glob.glob('path with my 4 images'))

# list of paths to the images
image_fname_list = glob.glob('path with my 4 images')

# var to accumulate the histograms
total_hue_hist = np.zeros((359,))

for image_fname in image_fname_list:
    # load image
    img = cv2.imread(image_fname)
    # convert from BGR to HSV
    img = np.float32(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
    # get the Hue channel
    #hue = img_hsv[:, :, 0]
    hue, sat, val = cv2.split(img_hsv)
    # show histogram
    hist, bin_edges = np.histogram(hue, bins=range(360))
    total_hue_hist += hist

plt.bar(list(range(359)), hist)
plt.show()

总和必须与色相图片1和2相同

第4张图片

第二张4张图片

第4张图片

后4张图片

我的结果必须正确

贝里尔结果

解决方案

您可以这样做:

 import cv2
import numpy as np
import matplotlib.pyplot as plt

# load image
img = cv2.imread('lenna.png')
# convert from BGR to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# get the Hue channel
hue = img_hsv[:, :, 0]
# show histogram
hist, bin_edges = np.histogram(hue, bins=range(180))
plt.bar(bin_edges[:-1], hist)
plt.show()
 

如果您不需要直方图值,则可以通过以下方式进行操作:

 import cv2
import matplotlib.pyplot as plt

# load image
img = cv2.imread('lenna.png')
# convert from BGR to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# get the Hue channel
hue = img_hsv[:, :, 0]
# show histogram
plt.hist(hue.flatten(), bins=range(180))
plt.show()
 

输入(lenna.png):

输出:


如果您有多张图片,则可以执行以下操作:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# list of paths to the images
image_fname_list = ['lenna.png', 'other_image.png', ...]

# var to accumulate the histograms
total_hue_hist = np.zeros((179,))

for image_fname in image_fname_list:
    # load image
    img = cv2.imread(image_fname)
    # convert from BGR to HSV
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # get the Hue channel
    hue = img_hsv[:, :, 0]
    # show histogram
    hist, bin_edges = np.histogram(hue, bins=range(180))
    total_hue_hist += hist

plt.bar(list(range(179)), hist)
plt.show()

I have a set of images and i want to create a histogram over the hue values from each image. Therfore i created an array with length 180. In every cell i add 1 if the hue value is in the image. In the end i have the array with the occurence of each hue value, but when i use numpy.hist, the y-axis are the hue values and the x-axis are the occurence. But i want it the other way round.

Here is my code:

path = 'path'
sub_path = 'subpath'

sumHueOcc = np.zeros((180, 1), dtype=int) 

print("sumHue Shape")
print(sumHueOcc.shape)

for item in dirs:
    fullpath = os.path.join(path,item)
    pathos = os.path.join(sub_path,item)
    if os.path.isfile(fullpath):
        img = np.array(Image.open(fullpath))

        f, e = os.path.splitext(pathos)

        imgHSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

        print("Img shape")
        print(img.shape)

        # want to work with hue only
        h, s, v = cv2.split(imgHSV)

        # the hue values in one large array
        Z = h.reshape((-1, 1))
        # convert to np.float32
        Z = np.uint32(Z)

        # add 1 for each hue value in the image
        for z in Z:
            sumHueOcc[z] = sumHueOcc[z] + 1

        plt.figure(figsize=(9, 8))
        plt.subplot(311)  # Hue Picture 1
        plt.subplots_adjust(hspace=.5)
        plt.title("Hue Picture 1")
        plt.hist(np.ndarray.flatten(h), bins=180)
        plt.subplot(312)  # Hue Picture 2
        plt.subplots_adjust(hspace=.5)
        plt.title("Hue Picture 2")
        plt.hist(np.ndarray.flatten(Z), bins=180)
        plt.subplot(313)  # Hue Picture 2
        plt.subplots_adjust(hspace=.5)
        plt.title("Sum Occ")
        plt.hist(np.ndarray.flatten(sumHueOcc), bins=180)
        plt.show()

#First Hue Sum
plt.figure(figsize=(9,8))
plt.title("Sum Hue Occ")
plt.hist(np.ndarray.flatten(sumHueOcc), bins=180)
plt.show()

Here is Berriels Code with the change from Half Hue to Full Hue:

print(glob.glob('path with my 4 images'))

# list of paths to the images
image_fname_list = glob.glob('path with my 4 images')

# var to accumulate the histograms
total_hue_hist = np.zeros((359,))

for image_fname in image_fname_list:
    # load image
    img = cv2.imread(image_fname)
    # convert from BGR to HSV
    img = np.float32(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
    # get the Hue channel
    #hue = img_hsv[:, :, 0]
    hue, sat, val = cv2.split(img_hsv)
    # show histogram
    hist, bin_edges = np.histogram(hue, bins=range(360))
    total_hue_hist += hist

plt.bar(list(range(359)), hist)
plt.show()

Sum Occ has to be the same as Hue Picture 1 and 2

First of 4 Pictures

Second of 4 Pictures

Third of 4 Pictures

Last of 4 Pictures

My result, which has to be correct

Berriels result

解决方案

You can do this way:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# load image
img = cv2.imread('lenna.png')
# convert from BGR to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# get the Hue channel
hue = img_hsv[:, :, 0]
# show histogram
hist, bin_edges = np.histogram(hue, bins=range(180))
plt.bar(bin_edges[:-1], hist)
plt.show()

If you don't need to histogram values, you can do this way:

import cv2
import matplotlib.pyplot as plt

# load image
img = cv2.imread('lenna.png')
# convert from BGR to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# get the Hue channel
hue = img_hsv[:, :, 0]
# show histogram
plt.hist(hue.flatten(), bins=range(180))
plt.show()

Input (lenna.png):

Output:


If you have multiple images, you can do something like this:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# list of paths to the images
image_fname_list = ['lenna.png', 'other_image.png', ...]

# var to accumulate the histograms
total_hue_hist = np.zeros((179,))

for image_fname in image_fname_list:
    # load image
    img = cv2.imread(image_fname)
    # convert from BGR to HSV
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # get the Hue channel
    hue = img_hsv[:, :, 0]
    # show histogram
    hist, bin_edges = np.histogram(hue, bins=range(180))
    total_hue_hist += hist

plt.bar(list(range(179)), hist)
plt.show()

这篇关于当我希望出现索引时如何使用numpy.hist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆