我正在尝试创建一个过滤器,它会为所有照片创建一个棕褐色外观。但我在过滤后的图像中得到了一些蓝色和青色的斑点。我认为这可能是溢出但我试图解释它并没有给我我想要的影响。我不确定什么是错的。原始链接位于https://www.dropbox.com/s/pd0esc8tef3smae/Lampard.jpg,修改后变为https://www.dropbox.com/s/f9n9lrhoi1qqs53/Lampard_blue.jpg
def filter3(path, cv2):
cv2.destroyAllWindows()
cv2.namedWindow("Filter 3")
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
newImg = img
(i,j,k) = img.shape
for x in xrange(i):
for y in xrange(j):
b = img[x,y,0]
g = img[x,y,1]
r = img[x,y,2]
if(k == 4):
newImg[x,y,0] = np.uint16(b * 0.272 + g * 0.534 + r * 0.131)
newImg[x,y,1] = np.uint8(b * .349 + g * 0.686 + r * 0.168)
newImg[x,y,2] = np.uint8(b * 0.393 + g * 0.769 + r * 0.189)
# newB = np.uint16(b * 0.272 + g * 0.534 + r * 0.131)
# newG = np.uint16(b * .349 + g * 0.686 + r * 0.168)
# newR = np.uint16(b * 0.393 + g * 0.769 + r * 0.189)
# if(newB > 255):
# newImg[x,y,0] = np.uint8(255)
# else:
# newImg[x,y,0] = np.uint8(newB)
# if(newG > 255):
# newImg[x,y,1] = np.uint8(255)
# else:
# newImg[x,y,1] = np.uint8(newB)
# if(newR > 255):
# newImg[x,y,2] = np.uint8(255)
# else:
# newImg[x,y,2] = np.uint8(newB)
newImg[x,y,3] = img[x,y,3]
else:
newImg[x,y,0] = np.uint16(b * 0.272 + g * 0.534 + r * 0.131)
newImg[x,y,1] = np.uint8(b * .349 + g * 0.686 + r * 0.168)
newImg[x,y,2] = np.uint8(b * 0.393 + g * 0.769 + r * 0.189)
# if(newB > 255):
# newImg[x,y,0] = np.uint8(0)
# else:
# newImg[x,y,0] = np.uint8(newB)
cv2.imshow("Filter 3", newImg)
return newImg
答案 0 :(得分:0)
我不知道python但你可以参考下面的C ++代码对我来说很好。
Mat src=imread("Lampard.jpg",1);
Mat SepiaKernel = (Mat_<float>(4,4) << 0.189, 0.769, 0.393, 0,\
0.168, 0.686, 0.349, 0,\
0.131, 0.534, 0.272, 0,\
0.000, 0.000, 0.000, 1);
Mat dst;
transform(src, dst, SepiaKernel);
imshow("src",src);
imshow("sepia",dst);
waitKey();
<强>结果: - 强>
答案 1 :(得分:0)
在我看来,问题在于类型。 Python认为在操作之后你想要整数。每个通道有8位。经过计算,当你得到值256(100000001)时,python将只读取前8位(00000001)。因为你有一点点奇怪的形象。 在我的情况下,使用此代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('lena.jpg')
newImage = cv2.imread('lena.jpg')
i, j, k = img.shape
for x in xrange(i):
for y in xrange(j):
R = img[x,y,2] * 0.393 + img[x,y,1] * 0.769 + img[x,y,0] * 0.189
G = img[x,y,2] * 0.349 + img[x,y,1] * 0.686 + img[x,y,0] * 0.168
B = img[x,y,2] * 0.272 + img[x,y,1] * 0.534 + img[x,y,0] * 0.131
if R > 255:
newImage[x,y,2] = 255
else:
newImage[x,y,2] = R
if G > 255:
newImage[x,y,1] = 255
else:
newImage[x,y,1] = G
if B > 255:
newImage[x,y,0] = 255
else:
newImage[x,y,0] = B
cv2.imshow('image',newImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
也许它不是很好的代码,但我不熟悉Python。
答案 2 :(得分:0)
您可以在应用棕褐色滤镜之前将图像阵列转换为浮点型。同样,使用cv2.transform
比遍历所有像素要快得多。可以使用此代码
import cv2
import numpy as np
img = cv2.imread('messi.jpg')
original = img.copy()
img = np.array(img, dtype = np.float64)
img = cv2.transform(img, np.matrix([[ 0.393, 0.769, 0.189],
[ 0.349, 0.686, 0.168],
[ 0.272, 0.534, 0.131]]))
img[np.where(img>255)] = 255
img = np.array(img, dtype = np.uint8)
cv2.imshow("original", original)
cv2.imshow("Output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()