And*_*tre 3 python rgb opencv raspberry-pi
我正在一个项目中,我需要从图像中减去RGB值。在示例中,我想从RED中减去BLUE通道,因此RED获得减法的差值。
我有图像的下一个属性:
Dimension:1456x2592,bpp:3
我正在使用的图像为我提供了以下数组:
[[[ 63 58 60]
[ 63 58 60]
[ 64 59 61]
...,
[155 155 161]
[155 155 161]
[155 155 161]]
[[ 58 53 55]
[ 60 55 57]
[ 62 57 59]
...,
[157 157 163]
[157 157 163]
[158 158 164]]
Run Code Online (Sandbox Code Playgroud)
我知道这些是图像中的值(RGB),所以现在我继续执行代码(我基于此代码)
import cv2
import numpy as np
from PIL import Image
# read image into matrix.
m = cv2.imread("ITESO.jpeg")
# get image properties.
h,w,bpp = np.shape(m)
# iterate over the entire image.
# BLUE = 0, GREEN = 1, RED = 2.
for py in range(0,h):
for px in range(0,w):
#m[py][px][2] = 2
n = m[py][px][2] //n takes the value of RED
Y = [n, 0, 0] //I create an array with [RED, 0, 0]
m, Y = np.array(m), np.array(Y)
m = np.absolute(m - Y) //Get the matriz with the substraction
y = 1
x = 1
print (m)
print (m[x][y])
#display image
#cv2.imshow('matrix', m)
#cv2.waitKey(0)
cv2.imwrite('new.jpeg',m)
img = Image.open('new.jpeg')
img.show()
img = Image.open('new.jpeg').convert('L')
img.save('new_gray_scale.jpg')
img.show()
Run Code Online (Sandbox Code Playgroud)
当我打印J矩阵时,它给出以下数组:
B,G,R
蓝色=蓝色-红色
[[[ 3 58 60]
[ 3 58 60]
[ 4 59 61]
...,
[ 95 155 161]
[ 95 155 161]
[ 95 155 161]]
[[ 2 53 55]
[ 0 55 57]
[ 2 57 59]
...,
[ 97 157 163]
[ 97 157 163]
[ 98 158 164]]
Run Code Online (Sandbox Code Playgroud)
但是我无法打开新图像,如果将一个RGB通道设置为一个值,它将显示图像。我使用以下几行:
import cv2
import numpy as np
# read image into matrix.
m = cv2.imread("python.png")
# get image properties.
h,w,bpp = np.shape(m)
# iterate over the entire image.
for py in range(0,h):
for px in range(0,w):
m[py][px][0] = 0 //setting channel Blue to values of 0
# display image
cv2.imshow('matrix', m)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
如何互相减去RGB通道?
PS:在MatLab中,它的工作原理很吸引人,但我无法在python中完成。
请注意,此操作会将dtype矩阵(图像)的值从uint8更改为int32,这可能会导致其他问题。IMO,一种更好的方法(效率更高)是:
import cv2
import numpy as np
img = cv2.imread('image.png').astype(np.float) # BGR, float
img[:, :, 2] = np.absolute(img[:, :, 2] - img[:, :, 0]) # R = |R - B|
img = img.astype(np.uint8) # convert back to uint8
cv2.imwrite('new-image.png', img) # save the image
cv2.imshow('img', img)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2388 次 |
| 最近记录: |