python-将openCV查找表(LUT)应用于图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-将openCV查找表(LUT)应用于图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1943字,纯文字阅读大概需要3分钟。
内容图文
![python-将openCV查找表(LUT)应用于图像](/upload/InfoBanner/zyjiaocheng/684/6994cdadd1d84dada4dbf0263b4496fe.jpg)
我想在图像上应用自定义颜色查找表(LUT).
我查看了openCV LUT Api,但似乎无法正确理解.
#!/bin/bash
# -*- coding: utf-8 -*-
import os
import numpy as np
import cv2
b_max = 230
g_max = 220
r_max = 250
identity = np.arange(256, dtype = np.dtype('uint8'))
b_channel = np.arange(b_max, dtype = np.dtype('uint8'))
b_channel = np.append(b_channel, (256-b_max)*[b_max])
g_channel = np.arange(g_max, dtype = np.dtype('uint8'))
g_channel = np.append(g_channel, (256-g_max)*[g_max])
r_channel = np.arange(r_max, dtype = np.dtype('uint8'))
r_channel = np.append(r_channel, (256-r_max)*[r_max])
if 256 != b_channel.size or 256 != g_channel.size or 256 != r_channel.size:
print "size of arrays don't match!"
lut = np.dstack((identity, identity, b_channel, g_channel, r_channel))
# Load the image
img = cv2.imread('input.jpg',cv2.IMREAD_COLOR)
dstImage = cv2.LUT(img, lut)
cv2.imwrite('output.jpg', dstImage)
我收到以下错误:
OpenCV Error: Assertion failed ((lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == CV_8U || depth == CV_8S)) in cv::LUT, file C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\core\src\convert.cpp, line 5690
Traceback (most recent call last):
File "ApplyCLUT.py", line 33, in <module>
dstImage = cv2.LUT(img, lut)
cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\core\src\convert.cpp:5690: error: (-215) (lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == CV_8U || depth == CV_8S) in function cv::LUT
编辑:
更改为此行:
lut = np.dstack( (b_channel, g_channel, r_channel) )
它会工作!
解决方法:
以下是构建LUT的示例:
identity = np.arange(256, dtype = np.dtype('uint8'))
zeros = np.zeros(256, np.dtype('uint8'))
lut = np.dstack((identity, identity, zeros))
请注意,将红色通道归零的方法非常简单:
img[:,:,2] = 0
同样调用LUT函数应该如下所示:
dstImage = cv2.LUT(img, lut)
将大于b_max的蓝色值设置为b_max:
b_max = 20
img[img[:,:,0] > b_max, 0] = b_max;
内容总结
以上是互联网集市为您收集整理的python-将openCV查找表(LUT)应用于图像全部内容,希望文章能够帮你解决python-将openCV查找表(LUT)应用于图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。