overmind 2024-01-14 18:15 采纳率: 92.9%
浏览 2
已结题

opencv中的输入黑白场、输出黑白场,中间调的控制。

  • 我想做出ps中的色阶效果

img


import cv2
import numpy as np

cv2.namedWindow('levels', cv2.WINDOW_NORMAL)
width = 400
height= 300
image = np.ones((height,width),dtype=np.uint8)
for num in range(width):
    image[:,num ] =  0 + num / (width-1)  * 255

def linear(pos,wpilv,bpilv,wpolv,bpolv):
    return bpolv + (pos - bpilv) * (wpolv - bpolv) / (wpilv - bpilv)

def bpil(value):
    wpilv = cv2.getTrackbarPos('White Point Input Level', 'levels')
    bpilv = cv2.getTrackbarPos('Black Point Input Level', 'levels')
    height,width = image.shape[0:2]
    for y in range(height):
        for x in range(width):
            v = image[y,x]
            if v <= bpilv:
                image[y,x] = 0
            elif v >= wpilv:
                image[y,x] = 255
            else:
                image[y,x] = linear(v,wpilv,bpilv,255,0)

def wpil(value):
    wpilv = cv2.getTrackbarPos('White Point Input Level', 'levels')
    bpilv = cv2.getTrackbarPos('Black Point Input Level', 'levels')
    height,width = image.shape[0:2]
    for y in range(height):
        for x in range(width):
            v = image[y,x]
            if v <= bpilv:
                image[y,x] = 0
            elif v >= wpilv:
                image[y,x] = 255
            else:
                image[y,x] = linear(v,wpilv,bpilv,255,0)

cv2.createTrackbar('Black Point Input Level', 'levels', 0, 255, bpil)
cv2.createTrackbar('White Point Input Level', 'levels', 0, 255, wpil)
cv2.setTrackbarPos('White Point Input Level', 'levels', 255)
while True:
    cv2.imshow('levels', image)
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

img

  • 输入黑白场 处理成功
  • 然后我准备处理输出的黑白场和中间调
import cv2
import numpy as np

cv2.namedWindow('levels', cv2.WINDOW_NORMAL)
width = 400
height= 300
image = np.ones((height,width),dtype=np.uint8)
for num in range(width):
    image[:,num ] =  0 + num / (width-1)  * 255

def linear(pos,wpilv,bpilv,wpolv,bpolv):
    return bpolv + (pos - bpilv) * (wpolv - bpolv) / (wpilv - bpilv)

def wpil(value):
    process()

def bpil(value):
    process()

def wpol(value):
    process()

def bpol(value):
    process()

def process():
    wpilv = cv2.getTrackbarPos('White Point Input Level', 'levels')
    bpilv = cv2.getTrackbarPos('Black Point Input Level', 'levels')
    wpolv = cv2.getTrackbarPos('White Point Output Level', 'levels')
    bpolv = cv2.getTrackbarPos('Black Point Output Level', 'levels')
    height,width = image.shape[0:2]
    for y in range(height):
        for x in range(width):
            v = image[y,x]
            if v <= bpilv:
                image[y,x] = bpolv
            elif v >= wpilv:
                image[y,x] = wpolv
            else:
                image[y,x] = linear(v,wpilv,bpilv,wpolv,bpolv)

cv2.createTrackbar('Black Point Input Level', 'levels', 0, 255, bpil)
cv2.createTrackbar('White Point Input Level', 'levels', 0, 255, wpil)
cv2.createTrackbar('Black Point Output Level', 'levels', 0, 255,bpol)
cv2.createTrackbar('White Point Output Level', 'levels', 0, 255,wpil)
cv2.setTrackbarPos('White Point Input Level', 'levels', 255)
cv2.setTrackbarPos('White Point Output Level', 'levels', 255)
while True:
    cv2.imshow('levels', image)
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break
cv2.destroyAllWidows()
  • 我在原来基础上修改了代码
  • 结果全黑了

img

  • 如何解决输入黑白场的问题
  • 并且最终增加滑块 控制中间调
  • 写回答

7条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-01-14 18:21
    关注

    引自免费微信小程序:皆我百晓生

    根据你提供的问题,你想要在OpenCV中实现对黑白场的输入和输出进行控制,并调整图像的亮度范围。下面是几个关键点来解决你的问题:

    1. 输入黑白场控制:在图1和图2中,你通过使用Trackbar来控制黑白场的输入级别。你可以创建两个Trackbar用于控制黑点和白点的输入级别。在回调函数中,你将获取Trackbar的当前位置,并根据这些值重新计算图像的亮度范围。

    2. 输出黑白场控制:在图3中,你还要控制图像的输出黑白场。你可以使用两个额外的Trackbar来控制黑点和白点的输出级别。与输入级别一样,在回调函数中,你将获取输出黑白场的当前位置,并相应地更新图像的亮度范围。

    3. 图像亮度范围调整:在图1和图2中,你使用了一个名为linear()的函数来计算图像中每个像素的亮度值,该函数将图像像素从输入级别映射到输出级别。你可以在回调函数中使用这个函数来调整图像的亮度范围。

    根据以上信息,我将提供一个整合了你提供的代码并做了一些修改的例子,以实现输入黑白场和输出黑白场的控制。请注意查看代码中的注释以便理解每个部分的作用和原理。

    import cv2
    import numpy as np
    
    cv2.namedWindow('levels', cv2.WINDOW_NORMAL)
    width = 400
    height = 300
    image = np.ones((height, width), dtype=np.uint8)
    for num in range(width):
        image[:, num] = 0 + num / (width - 1) * 255
    
    def linear(pos, wpilv, bpilv, wpolv, bpolv):
        return bpolv + (pos - bpilv) * (wpolv - bpolv) / (wpilv - bpilv)
    
    def process(value):
        wpilv = cv2.getTrackbarPos('White Point Input Level', 'levels')
        bpilv = cv2.getTrackbarPos('Black Point Input Level', 'levels')
        wpolv = cv2.getTrackbarPos('White Point Output Level', 'levels')
        bpolv = cv2.getTrackbarPos('Black Point Output Level', 'levels')
        height, width = image.shape[0:2]
        for y in range(height):
            for x in range(width):
                v = image[y, x]
                if v <= bpilv:
                    image[y, x] = bpolv
                elif v >= wpilv:
                    image[y, x] = wpolv
                else:
                    image[y, x] = linear(v, wpilv, bpilv, wpolv, bpolv)
    
    def on_input_level_change(value):
        process(value)
    
    def on_output_level_change(value):
        process(value)
    
    cv2.createTrackbar('Black Point Input Level', 'levels', 0, 255, on_input_level_change)
    cv2.createTrackbar('White Point Input Level', 'levels', 0, 255, on_input_level_change)
    cv2.createTrackbar('Black Point Output Level', 'levels', 0, 255, on_output_level_change)
    cv2.createTrackbar('White Point Output Level', 'levels', 0, 255, on_output_level_change)
    
    # 设置初始值
    cv2.setTrackbarPos('Black Point Input Level', 'levels', 0)
    cv2.setTrackbarPos('White Point Input Level', 'levels', 255)
    cv2.setTrackbarPos('Black Point Output Level', 'levels', 0)
    cv2.setTrackbarPos('White Point Output Level', 'levels', 255)
    
    while True:
        cv2.imshow('levels', image)
        key = cv2.waitKey(1)
        if key & 0xFF == ord('q'):
            break
    
    cv2.destroyAllWindows()
    

    希望这可以解决你的问题!如果还有其他问题,请随时提问。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来