<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python OpenCV像素操作

          共 5152字,需瀏覽 11分鐘

           ·

          2021-05-05 10:25

          點(diǎn)擊上方小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂

          重磅干貨,第一時(shí)間送達(dá)

          本文轉(zhuǎn)自:opencv學(xué)堂


          Python OpenCV像素操作

          環(huán)境聲明 : Python3.6 + OpenCV3.3 + PyCharm IDE

          首先要引入OpenCV和Numpy支持,添加代碼如下:


          1. import cv2 as cv;

          2. import numpy as np;

          讀寫像素


          對RGB圖像來說,在Python中第一個(gè)維度表示高度、第二個(gè)維度表示寬度、第三個(gè)維度是通道數(shù)目,可以通過下面的代碼獲取圖像三個(gè)維度的大小

          1. print(image.shape)

          2. print(image.size)

          3. print(image.dtype)

          循環(huán)讀取圖像方法一:


          直接從圖像中讀取,缺點(diǎn)是每次都需要訪問imread之后的Mat對象,進(jìn)行native操作,速度是個(gè)問題, 代碼實(shí)現(xiàn)如下:

          1. height = image.shape[0]

          2. width = image.shape[1]

          3. channels = image.shape[2]

          4. print(image.shape)

          5. for row in range(height):

          6.    for col in range(width):

          7.        for c in range(channels):

          8.            level = image[row, col, c]

          9.            pv = level + 30

          10.            image[row, col, c] = (255 if pv>255 else pv)

          循環(huán)讀取圖像方法二:


          首先通過Numpy把像素?cái)?shù)據(jù)讀到內(nèi)存中,在內(nèi)存中進(jìn)行高效循環(huán)訪問每個(gè)像素,修改之后,在賦值回去即可,代碼如下:

          1. # read once

          2. pixel_data = np.array(image, dtype = np.uint8);

          3. # loop pixel by pixel

          4. for row in range(height):

          5.    for col in range(width):

          6.        for c in range(channels):

          7.            level = pixel_data[row, col, c]

          8.            pixel_data[row, col, c] = 255 - level

          9. # write once

          10. image[ : : ] = pixel_data

          案例演示 在Python語言中完成圖像的屬性讀取、像素讀取與操作、實(shí)現(xiàn)了圖像的顏色取反、亮度提升、灰度化、梯度化、操作。首先看一下效果:

          完整的Python代碼實(shí)現(xiàn)如下:

          1. import cv2 as cv;

          2. import numpy as np;

          3. def inverse(image):

          4.    print("read and write pixel by pixel")

          5.    print(image.shape)

          6.    print(image.size)

          7.    print(image.dtype)

          8.    height = image.shape[0]

          9.    width = image.shape[1]

          10.    channels = image.shape[2]

          11.    # read once

          12.    pixel_data = np.array(image, dtype = np.uint8);

          13.    # loop pixel by pixel

          14.    for row in range(height):

          15.        for col in range(width):

          16.            for c in range(channels):

          17.                level = pixel_data[row, col, c]

          18.                pixel_data[row, col, c] = 255 - level

          19.    # write once

          20.    image[ : : ] = pixel_data

          21.    cv.imshow("inverse image", image)

          22. def brightness(image):

          23.    print("read and write pixel by pixel")

          24.    height = image.shape[0]

          25.    width = image.shape[1]

          26.    channels = image.shape[2]

          27.    print(image.shape)

          28.    for row in range(height):

          29.        for col in range(width):

          30.            for c in range(channels):

          31.                level = image[row, col, c]

          32.                pv = level + 30

          33.                image[row, col, c] = (255 if pv>255 else pv)

          34.    cv.imshow("inverse image", image);

          35. def to_gray(image):

          36.    print("RGB to Gray Image")

          37.    height = image.shape[0]

          38.    width = image.shape[1]

          39.    channels = image.shape[2]

          40.    print("channels : ", channels);

          41.    print(image.shape)

          42.    for row in range(height):

          43.        for col in range(width):

          44.            blue = image[row, col, 0]

          45.            green = image[row, col, 1];

          46.            red = image[row, col, 2]

          47.            gray = (0.2989*red + 0.5870*green + 0.1140*blue);

          48.            image[row, col, 0] = gray;

          49.            image[row, col, 1] = gray;

          50.            image[row, col, 2] = gray;

          51.    cv.imshow("gray image", image)

          52. def gradient_image(image):

          53.    gx = cv.Sobel(image, cv.CV_32F, 1, 0)

          54.    gy = cv.Sobel(image, cv.CV_32F, 0, 1)

          55.    dst = cv.addWeighted(gx, 0.5, gy, 0.5, 50)

          56.    sobel_abs = np.absolute(dst)

          57.    sobel_8u = np.uint8(sobel_abs)

          58.    cv.imshow("gradient image", sobel_8u)

          59. def clam(pv):

          60.    if pv > 255:

          61.        return 255

          62.    if pv < 0:

          63.        return 0;

          64.    return pv;

          65. print("Image Pixel Operation Demo")

          66. src = cv.imread("D:/vcprojects/images/demo.png")

          67. cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)

          68. cv.imshow("input image", src)

          69. gradient_image(src)

          70. cv.waitKey(0)

          71. cv.destroyAllWindows()




          下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程
          在「小白學(xué)視覺」公眾號(hào)后臺(tái)回復(fù):擴(kuò)展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴(kuò)展模塊教程中文版,涵蓋擴(kuò)展模塊安裝、SFM算法、立體視覺、目標(biāo)跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實(shí)戰(zhàn)項(xiàng)目52講
          小白學(xué)視覺公眾號(hào)后臺(tái)回復(fù):Python視覺實(shí)戰(zhàn)項(xiàng)目,即可下載包括圖像分割、口罩檢測、車道線檢測、車輛計(jì)數(shù)、添加眼線、車牌識(shí)別、字符識(shí)別、情緒檢測、文本內(nèi)容提取、面部識(shí)別等31個(gè)視覺實(shí)戰(zhàn)項(xiàng)目,助力快速學(xué)校計(jì)算機(jī)視覺。

          下載3:OpenCV實(shí)戰(zhàn)項(xiàng)目20講
          小白學(xué)視覺公眾號(hào)后臺(tái)回復(fù):OpenCV實(shí)戰(zhàn)項(xiàng)目20講即可下載含有20個(gè)基于OpenCV實(shí)現(xiàn)20個(gè)實(shí)戰(zhàn)項(xiàng)目,實(shí)現(xiàn)OpenCV學(xué)習(xí)進(jìn)階。

          交流群


          歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器自動(dòng)駕駛、計(jì)算攝影、檢測、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會(huì)逐漸細(xì)分),請掃描下面微信號(hào)加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會(huì)根據(jù)研究方向邀請進(jìn)入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會(huì)請出群,謝謝理解~


          瀏覽 56
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  超碰在线观看中文字幕版 | 青草娱乐视频在线观看 | 黄色免费性爱视频 | 神马午夜福利视频 | 黄色一a电影 |