欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python OpenCV對圖像像素進(jìn)行操作

 更新時(shí)間:2022年01月10日 14:42:17   作者:Hong_Youth  
大家好,本篇文章主要講的是Python OpenCV對圖像像素進(jìn)行操作,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

遍歷并修改圖像像素值

在使用opencv處理圖像時(shí),有時(shí)需要對圖像的每個(gè)像素點(diǎn)進(jìn)行處理,比如取反、修改值等操作,就需要通過h和w遍歷像素。
依然以下圖為例:

OpenCV-Python對圖像像素進(jìn)行操作_python

具體代碼:

import cv2 as cv
import numpy as np


def image_pixel(image_path: str):
    img = cv.imread(image_path, cv.IMREAD_COLOR)
    cv.imshow('input', img)

    h, w, c = img.shape
    # 遍歷像素點(diǎn),修改圖像b,g,r值
    for row in range(h):
        for col in range(w):
            b, g, r = img[row, col]
            # img[row, col] = (255 - b, 255 - g, 255 - r)
            # img[row, col] = (255 - b, g, r)
            # img[row, col] = (255 - b, g, 255 - r)
            img[row, col] = (0, g, r)

    cv.imshow('result', img)
    cv.imwrite('images/result.jpg', img)
    cv.waitKey(0)
    cv.destroyAllWindows()

結(jié)果展示:

OpenCV-Python對圖像像素進(jìn)行操作_python_02

圖像的加減乘除運(yùn)算

圖像的加減運(yùn)算可以調(diào)整圖片的亮度和對比度。圖像的加減運(yùn)算可以調(diào)整圖像的亮度;圖像的乘除運(yùn)算可以調(diào)整圖像的對比度。

具體代碼:

# -*-coding:utf-8-*-
"""
File Name: image_pixel_operation.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2 as cv
import numpy as np


def math_pixel(image_path: str):
    img = cv.imread(image_path, cv.IMREAD_COLOR)
    cv.imshow('input', img)
    h, w, c = img.shape

    blank = np.zeros_like(img)
    blank[:, :] = (2, 2, 2)  # 所有像素值設(shè)為50

    # 改變圖像亮度
    mask = cv.add(img, blank)  # 圖像加操作。圖像形狀一樣就可以相加,像素值類型不一樣不影響, 人為的增加了亮度
    mask = cv.subtract(img, blank)  # 圖像減操作。人為的降低了亮度

    # 改變圖像對比度
    # result = cv.divide(img, blank)  # 圖像除操作
    result = cv.multiply(img, blank)  # 圖像乘操作

    cv.imshow('blank', blank)
    cv.imshow('mask', mask)
    cv.imshow('contrast', result)
    cv.waitKey(0)
    cv.destroyAllWindows()

效果展示:

OpenCV-Python對圖像像素進(jìn)行操作_ide_03

 到此這篇關(guān)于Python OpenCV對圖像像素進(jìn)行操作的文章就介紹到這了,更多相關(guān)Python OpenCV圖像像素操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論