久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

老外講解什么是直方圖以及如何用opencv繪制直方圖(譯文)

 新用戶0118F7lQ 2022-07-02 發(fā)布于湖北

1. 目標(biāo)

  • Find histograms, using both OpenCV and Numpy functions 使用 OpenCV 和 Numpy 函數(shù)查找直方圖

  • Plot histograms, using OpenCV and Matplotlib functions 使用 OpenCV 和 Matplotlib 函數(shù)繪制直方圖

  • You will see these functions : 你會(huì)看到這些函數(shù):cv.calcHist() cv.calcHist (), np.histogram() Np.histogram ()etc. 等等

2. 理論

So what is histogram ? You can consider histogram as a graph or plot, which gives you an overall idea about the intensity distribution of an image. It is a plot with pixel values (ranging from 0 to 255, not always) in X-axis and corresponding number of pixels in the image on Y-axis.

那么什么是直方圖呢,?您可以將直方圖看作一個(gè)圖形或圖表,它可以讓您對(duì)圖像的強(qiáng)度分布有一個(gè)全面的了解,。它是一個(gè)以 x 軸為單位的像素值(范圍從0到255,,并非總是如此)和 y 軸上圖像中相應(yīng)的像素?cái)?shù)的繪圖。

It is just another way of understanding the image. By looking at the histogram of an image, you get intuition about contrast, brightness, intensity distribution etc of that image. Almost all image processing tools today, provides features on histogram. Below is an image from Cambridge in Color website, and I recommend you to visit the site for more details.

這只是理解圖像的另一種方式,。通過查看圖像的直方圖,,你可以直觀地了解圖像的對(duì)比度,、亮度、強(qiáng)度分布等等?,F(xiàn)在幾乎所有的圖像處理工具都提供了直方圖特征,。下面是一張來(lái)自 Cambridge in Color 網(wǎng)站的圖片,我建議你訪問該網(wǎng)站了解更多細(xì)節(jié),。

圖片

You can see the image and its histogram. (Remember, this histogram is drawn for grayscale image, not color image). Left region of histogram shows the amount of darker pixels in image and right region shows the amount of brighter pixels. From the histogram, you can see dark region is more than brighter region, and amount of midtones (pixel values in mid-range, say around 127) are very less.

你可以看到圖像和它的直方圖,。(請(qǐng)記住,此直方圖是為灰度圖像繪制的,,而不是為彩色圖像繪制的),。直方圖的左側(cè)區(qū)域顯示圖像中較暗的像素?cái)?shù)量,右側(cè)區(qū)域顯示較亮的像素?cái)?shù)量,。從直方圖上,,你可以看到黑色區(qū)域比亮色區(qū)域多,而中間色調(diào)的數(shù)量(像素值在中間范圍,,比如127左右)非常少,。

3. 查找直方圖

Now we have an idea on what is histogram, we can look into how to find this. Both OpenCV and Numpy come with in-built function for this. Before using those functions, we need to understand some terminologies related with histograms.

現(xiàn)在我們知道什么是直方圖了,我們可以研究如何找到它,。OpenCV 和 Numpy 都為此提供了內(nèi)置功能,。在使用這些函數(shù)之前,我們需要了解一些與直方圖相關(guān)的術(shù)語(yǔ),。

BINS:The above histogram shows the number of pixels for every pixel value, ie from 0 to 255. ie you need 256 values to show the above histogram. But consider, what if you need not find the number of pixels for all pixel values separately, but number of pixels in a interval of pixel values? say for example, you need to find the number of pixels lying between 0 to 15, then 16 to 31, …, 240 to 255. You will need only 16 values to represent the histogram. And that is what is shown in example given in OpenCV Tutorials on histograms.

上面的直方圖顯示了每個(gè)像素值的像素?cái)?shù),,即從0到255。即你需要256個(gè)值來(lái)顯示上面的直方圖,。但是,,考慮一下,如果您不需要為所有像素值單獨(dú)找到像素?cái)?shù),,而是需要以像素值間隔找到像素?cái)?shù)呢,?例如,你需要找到0到15之間的像素?cái)?shù),,然后是16到31,,… ,240到255,。你只需要16個(gè)值來(lái)表示直方圖,。這就是在 OpenCV 的直方圖教程中展示的例子。

Sowhat you do is simply split the whole histogram to 16 sub-parts and value of each sub-part is the sum of all pixel count in it. This each sub-part is called 'BIN'. In first case, number of bins were 256 (one for each pixel) while in second case, it is only 16. BINS is represented by the term histSizein OpenCV docs.

所以你要做的就是把整個(gè)直方圖分成16個(gè)子部分,,每個(gè)子部分的值就是其中所有像素?cái)?shù)的總和,。這個(gè)每個(gè)子部分稱為“ BIN”。在第一種情況下,,垃圾箱的數(shù)量是256個(gè)(每個(gè)像素一個(gè)) ,,而在第二種情況下,,垃圾箱的數(shù)量只有16個(gè)。在 OpenCV 文檔中,,術(shù)語(yǔ) histSize 表示 bin,。

DIMS: It is the number of parameters for which we collect the data. In this case, we collect data regarding only one thing, intensity value. So here it is 1.

DIMS: 它是我們收集數(shù)據(jù)的參數(shù)數(shù)目。在這種情況下,,我們只收集關(guān)于一件事情的數(shù)據(jù),,強(qiáng)度值。這里是1,。

RANGE: It is the range of intensity values you want to measure. Normally, it is [0,256], ie all intensity values.

范圍: 這是你想測(cè)量的強(qiáng)度值的范圍,。通常,它是[0,256] ,,即所有強(qiáng)度值,。

3.1. OpenCV 中的直方圖計(jì)算

So now we use cv.calcHist()function to find the histogram. Let's familiarize with the function and its parameters :

現(xiàn)在我們使用cv.calcHist ()函數(shù)來(lái)查找直方圖:

cv.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

  1. images: it is the source image of type uint8 or float32. it should be given in square brackets, ie, '[img]'. 圖片: 它是 uint8或 float32類型的源圖片。應(yīng)該用方括號(hào)表示,,例如”[ img ]”

  2. channels : it is also given in square brackets. It is the index of channel for which we calculate histogram. For example, if input is grayscale image, its value is [0]. For color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively. 也可以用方括號(hào)來(lái)表示,。它是我們計(jì)算直方圖的信道指標(biāo)。例如,,如果輸入是灰度圖像,,它的值是[0]。對(duì)于彩色圖像,,可以通過[0]、[1]或[2]分別計(jì)算藍(lán)色,、綠色或紅色通道的直方圖

  3. mask : mask image. To find histogram of full image, it is given as 'None'. But if you want to find histogram of particular region of image, you have to create a mask image for that and give it as mask. (I will show an example later.) 面具: 面具圖像,。為了查找完整圖像的直方圖,給出了“無(wú)”的表示,。但是,,如果你想找到圖像的特定區(qū)域的直方圖,你必須創(chuàng)建一個(gè)蒙版圖像,,并給它作為蒙版,。(我稍后將展示一個(gè)例子)

  4. histSize: this represents our BIN count. Need to be given in square brackets. For full scale, we pass [256]. histSize: 這表示我們的 BIN 計(jì)數(shù)。需要在方括號(hào)中給出,。對(duì)于完整規(guī)模,,我們通過[256]

  5. ranges: this is our RANGE. Normally, it is [0,256]. 范圍: 這是我們的范圍。通常,,它是[0,256]

So let's start with a sample image. Simply load an image in grayscale mode and find its full histogram.

讓我們從一個(gè)樣本圖像開始,,簡(jiǎn)單地加載一個(gè)圖像在灰度模式下,找到它的完整直方圖,。

img = cv.imread('home.jpg',0)

hist = cv.calcHist([img],[0],None,[256],[0,256])

hist is a 256x1 array, each value corresponds to number of pixels in that image with its corresponding pixel value.

Hist 是一個(gè)256x1的數(shù)組,,每個(gè)值對(duì)應(yīng)于該圖像中的像素?cái)?shù)及其對(duì)應(yīng)的像素值

2. Numpy 的直方圖計(jì)算

Numpy also provides you a function, np.histogram(). So instead of calcHist() function, you can try below line :

Numpy 還提供了一個(gè)函數(shù) np.histogram ():

hist,bins = np.histogram(img.ravel(),256,[0,256])

hist is same as we calculated before. But bins will have 257 elements, because Numpy calculates bins as 0-0.99, 1-1.99, 2-2.99 etc. So final range would be 255-255.99. To represent that, they also add 256 at end of bins. But we don't need that 256. Upto 255 is sufficient.

歷史和我們之前計(jì)算的一樣,。但是箱子將有257個(gè)元素,因?yàn)?Numpy 將箱子計(jì)算為0-0.99,、1-1.99,、2-2.99等。所以最終的范圍是255-255.99,。為了表示這一點(diǎn),,他們還在垃圾箱的末尾加了256個(gè)。但我們不需要那256,。高達(dá)255就足夠了,。

See also 參見

Numpy has another function, 還有另一個(gè)功能,np.bincount() Np.bincount ()which is much faster than (around 10X) np.histogram(). So for one-dimensional histograms, you can better try that. Don't forget to set minlength = 256 in np.bincount. For example, hist = np.bincount(img.ravel(),minlength=256) 這比(大約10X) np.histogram ()快得多。所以對(duì)于一維直方圖,,你可以嘗試一下,。不要忘記將 minlength = 256設(shè)置為 np.bincount。例如,,hist = np.bincount (img.ravel () ,,minlength = 256)

Note 注意

OpenCV function is faster than (around 40X) than np.histogram(). So stick with OpenCV function. OpenCV 函數(shù)比 np.histogram ()快(大約40倍) ,所以堅(jiān)持使用 OpenCV 函數(shù)

Now we should plot histograms, but how?

現(xiàn)在我們應(yīng)該繪制直方圖,,但是如何繪制呢,?

4. 繪制直方圖

There are two ways for this,

有兩種方法,

  1. Short Way : use Matplotlib plotting functions Short Way: 使用 Matplotlib 繪圖函數(shù)

  2. Long Way : use OpenCV drawing functions 長(zhǎng)途: 使用 OpenCV 繪圖功能

4.1. 使用 Matplotlib

Matplotlib comes with a histogram plotting function :matplotlib.pyplot.hist()

Matplotlib 附帶一個(gè)直方圖繪制函數(shù):Matplotlib.pyplot.hist ()

It directly finds the histogram and plot it. You need not use calcHist() or np.histogram() function to find the histogram. See the code below:

它直接找到直方圖并繪制它。您不需要使用 calcHist ()或 np.histogram ()函數(shù)來(lái)查找直方圖,。參見下面的代碼:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('home.jpg',0)
plt.hist(img.ravel(),256,[0,256]); plt.show()

圖片

Or you can use normal plot of matplotlib, which would be good for BGR plot. For that, you need to find the histogram data first. Try below code:

或者可以使用 matplotlib 的普通繪圖,,這對(duì) BGR 繪圖很有幫助。為此,,您需要首先找到直方圖數(shù)據(jù),。試試下面的代碼:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('home.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()

圖片

You can deduct from the above graph that, blue has some high value areas in the image (obviously it should be due to the sky)

你可以從上面的圖表中推斷出,藍(lán)色在圖像中有一些高值區(qū)域(顯然這應(yīng)該是由于天空)

5. 代碼

#!usr/bin/env python
# -- coding:utf-8 _-
'''
@Copyright: Jihua Lab 2021.
@File Name : 1301-historgram.py
@Description:

@Create Time : 2021/1/7 0007 15:12
@Author : HaoWANG, Foshan,,China
@Email : [email protected]

@Software : Pycharm
@Version: V1.0

'''

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def plothistdemo(image):
'''
# 統(tǒng)計(jì)并繪制彩色BGR圖像灰度值統(tǒng)計(jì)直方圖
:param image: src img
:return: plt plot
'''
plt.hist(image.ravel( ), 256, [0, 256])
plt.show()

def imagehist(image):
'''
# 彩色圖像RGB三通道的灰度直方圖繪制
:param image: 彩色圖像
:return: RGB三通道的灰度直方圖
'''
color = ('blue', 'green', 'red')
for i, color in enumerate(color):
hist = cv.calcHist([image], [i], None, [256], [0, 256])
plt.plot(hist, color = color)
plt.xlim([0, 256])
plt.show( )

def imagemask_plot(img):
'''
# 使用make切去ROI區(qū)域內(nèi)的圖像統(tǒng)計(jì)并繪制直方圖
:param img: color image
:return: matplotlib plot.show()
'''

# create a mask
mask = np.zeros(img.shape[:2], np.uint8)
mask[50:200, 50:300] = 255
masked_img = cv.bitwise_and(img, img, mask = mask)
# Calculate histogram with mask and without mask
# Check third argument for mask
hist_full = cv.calcHist([img], [0], None, [256], [0, 256])
hist_mask = cv.calcHist([img], [0], mask, [256], [0, 256])

# cvt color space from OpenCV BGR to RGB
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
mask = cv.cvtColor(mask, cv.COLOR_BGR2RGB)
masked_img = cv.cvtColor(masked_img, cv.COLOR_BGR2RGB)

# matplotlib subplot src images and their histogram
plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(mask, 'gray')
plt.subplot(223), plt.imshow(masked_img, 'gray')
plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
plt.xlim([0, 256])
plt.show( )

def main():
print('————-Histogram Plot Python ————-')
src = cv.imread('../pictures/cat.jpg')
cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
cv.imshow('input image', src)
# gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) # color cvt BGR->gray
# plot_hist_demo(gray) # plot the source image histgram
# image_hist(src)
image_mask_plot(src) # Application of Mask
cv.waitKey(0)
cv.destroyAllWindows( ) # destroy all windows

if __name
== '__main
':
main( )

圖片

5.1 BGR三通道灰度直方圖

圖片

5.2 灰度通道直方圖

圖片

圖片

5.3 mask操作

圖片

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多