向交通灯添加发光效果Imageprocessing skimage

问题描述

我正在学习图像处理,并遇到了以下代码。它所做的是使交通信号灯图像发红光。我通过不知道new_R变量是如何计算的来理解代码的。尽管很容易解释它对应于R和G标度中的值,其中R的像素值小于40,G的像素值小于10。 我只想知道为什么要添加G层值,并且如果我要发出绿色或橙色光怎么办,在完成new_R时计算new_G或new_O的上限或下限应该是什么。

from sklearn import io as ip
import numpy as np

I = ip.imread('trafficlights.png')

R = np.logical_and(I[:,:,0] > 40,I[:,1] < 10)

new_R = gaussian_filt(255 * R,30)

J = I.copy()
J[:,0] = ip.imadd(new_R,J[:,0])

import matplotlib.pyplot as plt
fig,ax = plt.subplots(1,2)

plt.sca(ax[0])
ip.imshow(I)

plt.sca(ax[1])
ip.imshow(J)
ip.imsave('traffic_redglow.png',J)

如果您想复制,那么这里还有其他功能

def imadd(I,K):
'''
Add to the pixels of I.
- If K is a number,adds K to each pixel value of I.
- If K is an image (NumPy array) of the same size as I,the pixel values of K are added to the pixel values of I.
    
Clips the result to avoid overflow.

Example:
# Add 10 to each pixel of I
J = imadd(I,10)

# Subtract 20 from each pixel of I
J = imadd(I,-20)

# Add images I1 and I2 together
J = imadd(I1,I2)
'''
import numbers
if isinstance(K,numbers.Number):
    J = I.astype('int32')
    J += K
elif isinstance(K,np.ndarray):
    assert K.shape == I.shape,f'Cannot add images with sizes {I.shape} and {K.shape}.'
    J = I.astype('int32') + K.astype('int32')
else:
    raise TypeError('K must be a number or an array.')
    
np.clip(J,255,out=J)
J = J.astype('uint8')
return J

def gaussian_filt(I,sigma,pad=0):
'''
Filters the image I with a Gaussian filter,with standard deviation sigma.
Returns a new filtered image.

Optional argument pad specifies the boundary options,and can take the following values:
- if pad is a number (0-255,default 0),pixels outside the bounds of the image are assigned this value.
- 'reflect': outer pixel values are mirror-reflections of inner pixel values
across the image border.
- 'nearest': outer pixel values are equal to the nearest inner pixel values.
- 'wrap': outer pixel values are computed by assuming that the image is periodic (i.e. tiled).

Example:
# Use Gaussian filter on I with a standard deviation of 0.5,# pad boundary pixels with white
J = gaussian_filt(I,0.5,pad=255)

# Reflect boundary pixel values across the image border
J = gaussian_filt(I,pad='reflect')
'''
import numbers
assert isinstance(pad,numbers.Number) or pad in ['reflect','nearest','wrap'],\
        'Choose a correct value for pad: a number (0-255),''reflect'',''nearest'',or ''wrap''.'

if isinstance(pad,numbers.Number):
    md = 'constant'
    c = pad
else:
    md = pad
    c = 0
    
return gaussian_filter(I,mode=md,cval=c)

the image to glow

glow image

非常感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)