1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Python OpenCV像素操作

        共 5152字,需瀏覽 11分鐘

         ·

        2021-05-05 10:25

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

        重磅干貨,第一時間送達(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中第一個維度表示高度、第二個維度表示寬度、第三個維度是通道數(shù)目,可以通過下面的代碼獲取圖像三個維度的大小

        1. print(image.shape)

        2. print(image.size)

        3. print(image.dtype)

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


        直接從圖像中讀取,缺點是每次都需要訪問imread之后的Mat對象,進行native操作,速度是個問題, 代碼實現(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把像素數(shù)據(jù)讀到內(nèi)存中,在內(nèi)存中進行高效循環(huán)訪問每個像素,修改之后,在賦值回去即可,代碼如下:

        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語言中完成圖像的屬性讀取、像素讀取與操作、實現(xiàn)了圖像的顏色取反、亮度提升、灰度化、梯度化、操作。首先看一下效果:

        完整的Python代碼實現(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擴展模塊中文版教程
        在「小白學(xué)視覺」公眾號后臺回復(fù):擴展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴展模塊教程中文版,涵蓋擴展模塊安裝、SFM算法、立體視覺、目標(biāo)跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

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

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

        交流群


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


        瀏覽 56
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            天天躁日日躁狠狠 | 欧美成人三级网站 | 艳妇合欢交换小说全文阅读 | 韩国一级淫片免费看 | 性爱午夜视频 | www玖玖| 国产手机在线观看 | 国产精品一区成人精品果冻传媒 | 男女免费看片 | 天天射天天看 |