国产无码免费,人妻口爆,国产V在线,99中文精品7,国产成人无码AA精品一,制度丝袜诱惑av,久久99免费麻辣视频,蜜臀久久99精品久久久久久酒店
        訂閱
        糾錯
        加入自媒體

        OpenCV:直方圖均衡

        2022-12-13 11:43
        磐創AI
        關注

        步驟:

        第 1 步:手動均衡

        第 2 步:通過使用 OpenCV 函數

        什么是圖像直方圖?

        它是圖像強度分布的圖形表示。它量化了所考慮的每個強度值的像素數。

        第 1 步:手動均衡

        %matplotlib inline

        from IPython.display import display, Math, Latex

        import numpy as np

        import matplotlib.pyplot as plt

        from PIL import Image

        img = Image.open('DATA/einstein.jpg')

        plt.imshow(img)

        輸出:


        顯示彩色圖像

        將圖像轉換為 numpy 數組,以便 OpenCV 可以使用:

        img = np.asanyarray(img)

        img.shape

        輸出:

        (2354, 2560, 3)

        將 RGB 轉換為灰度:

        import cv2

        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        img.shape

        輸出:

        (2354, 2560)

        顯示圖像:

        plt.imshow(img, cmap='gray')

        輸出:


        我們現在知道如何處理直方圖了

        img.max()

        輸出:

        255

        img.min()

        輸出:

        0

        img.shape

        輸出:

        (2354, 2560)

        把它展平:

        flat = img.flatten()

        # 1 row 2354 x 2560 = 6.026.240

        flat.shape

        輸出:

        (6026240,)

        顯示直方圖

        plt.hist(flat, bins=50)

        請注意,灰度值在某個值周圍分布很差

        什么是直方圖均衡?

        為了更清楚,從上圖中,你可以看到像素似乎聚集在可用強度范圍的中間。直方圖均衡所做的就是擴大這個范圍。

        # formula for creating the histogram

        display(Math(r'P_x(j) = sum_{i=0}^{j} P_x(i)'))

        # create our own histogram function

        def get_histogram(image, bins):

           # array with size of bins, set to zeros

           histogram = np.zeros(bins)

           # loop through pixels and sum up counts of pixels

           for pixel in image:

               histogram[pixel] += 1

               # return our final result

               return histogram

        hist = get_histogram(flat, 256)

        plt.plot(hist)

        []

        # create our cumulative sum function

        def cumsum(a):

          a = iter(a)

          b = [next(a)]

          for i in a:

           b.append(b[-1] + i)

           return np.array(b)
           

        # execute the fn

        cs = cumsum(hist)


        # display the result

        plt.plot(cs)

        []

        # formula to calculate cumulation sum

        display(Math(r's_k = sum_{j=0}^{k} { rac{n_j}{N}}'))

        # re-normalize cumsum values to be between 0-255

        # numerator & denomenator

        nj = (cs - cs.min()) * 255

        N = cs.max() - cs.min()

        # re-normalize the cdf

        cs = nj / N

        plt.plot(cs)

        []


        Casting:

        # cast it back to uint8 since we can't use floating point values in imagescs =

        cs.astype('uint8')

        plt.plot(cs)

        輸出:

        []

        獲取 CDF:

        # get the value from cumulative sum for every index in flat, and set that as img_new

        img_new = cs[flat]

        # we see a much more evenly distributed histogram

        plt.hist(img_new, bins=50)

        它是如何工作的?

        均衡意味著將一個分布(給定的直方圖)映射到另一個分布(強度值的更廣泛和更均勻的分布),因此強度值分布在整個范圍內。

        # get the value from cumulative sum for every index in flat, and set that as img_new

        img_new = cs[flat]

        # we see a much more evenly distributed histogram

        plt.hist(img_new, bins=50)

        # put array back into original shape since we flattened it

        img_new = np.reshape(img_new, img.shape)

        img_new

        輸出:

        array([[233, 231, 228, ..., 216, 216, 215],

              [233, 230, 228, ..., 215, 215, 214],

              [233, 231, 229, ..., 213, 213, 212],

              ...,

              [115, 107,  96, ..., 180, 187, 194],

              [111, 103,  93, ..., 187, 189, 192],

              [111, 103,  93, ..., 187, 189, 192]], dtype=uint8)

        一探究竟:

        # set up side-by-side image display

        fig = plt.figure()

        fig.set_figheight(15)

        fig.set_figwidth(15)

        fig.add_subplot(1,2,1)

        plt.imshow(img, cmap='gray')

        # display the new image

        fig.add_subplot(1,2,2)

        plt.imshow(img_new, cmap='gray')

        plt.show(block=True)

        使用 OpenCV equalizeHist(img) 方法

        第 2 步:通過使用 OpenCV 函數

        # Reading image via OpenCV and Equalize it right away!

        img = cv2.imread('DATA/einstein.jpg',0)

        equ = cv2.equalizeHist(img)

        準備好!這就是你需要做的!

        fig = plt.figure()

        fig.set_figheight(15)

        fig.set_figwidth(15)

        fig.add_subplot(1,2,1)

        plt.imshow(img, cmap='gray')

        # display the Equalized (equ) image

        fig.add_subplot(1,2,2)

        plt.imshow(equ, cmap='gray')

        plt.show(block=True)

        print("That?s it! Thank you once again!I hope will be helpful.")

        輸出:

        That?s it! Thank you once again!

        I hope will be helpful.

        image.png

               原文標題 : OpenCV:直方圖均衡

        聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權或其他問題,請聯系舉報。

        發表評論

        0條評論,0人參與

        請輸入評論內容...

        請輸入評論/評論長度6~500個字

        您提交的評論過于頻繁,請輸入驗證碼繼續

        暫無評論

        暫無評論

          掃碼關注公眾號
          OFweek人工智能網
          獲取更多精彩內容
          文章糾錯
          x
          *文字標題:
          *糾錯內容:
          聯系郵箱:
          *驗 證 碼:

          粵公網安備 44030502002758號

          主站蜘蛛池模板: 亚洲中文字幕AV在线| 99麻豆| 国产精品集合| 泾川县| 3p露脸在线播放| 炉霍县| 欧韩一区| 久久久久久久久无码| 欧美老熟妇精品| 玛纳斯县| 聚色av| 江陵县| av色综合| 影音先锋久久| 安塞县| 久久99久久99精品免观看吃奶网| 性交大片| 亚洲天堂男人天堂| 国产在线你懂| 内江市| 亚洲十八禁| 欧美福利极品| 长乐市| 国内a片| 97资源超碰| 口爆少妇| 狠狠干影院| 亚洲无码人妻| 欧美A视频| av资源站| 乱伦日屄| 超碰免费人妻| 亚洲avav| 美女秘密91| 羞羞成人一区| 平顶山市| 国模在线视频| 亚州无码熟女| 强行糟蹋人妻HD中文字幕| 亚州脚交| 91视频?-?sebo99|