尝试运行python脚本,其中我主要在gpu上使用opencv
所以我试过了所有的方法,我试过在ubuntu上,在windows上,试过了所有的在线解决方案,但我没有得到它;我错过了一些东西 我有这个代码,我试图在我的gpu上运行它,我主要使用windows;如果需要,我还可以访问ubuntu 16.04和18.04尝试运行python脚本,其中我主要在gpu上使用opencv,python,opencv,image-processing,computer-vision,image-preprocessing,Python,Opencv,Image Processing,Computer Vision,Image Preprocessing,所以我试过了所有的方法,我试过在ubuntu上,在windows上,试过了所有的在线解决方案,但我没有得到它;我错过了一些东西 我有这个代码,我试图在我的gpu上运行它,我主要使用windows;如果需要,我还可以访问ubuntu 16.04和18.04 import cv2 def shadow_removal (path) : or_img = cv2.imread(path) y_cb_cr_img1 = cv2.cvtColor(or_img, cv2.COLOR_B
import cv2
def shadow_removal (path) :
or_img = cv2.imread(path)
y_cb_cr_img1 = cv2.cvtColor(or_img, cv2.COLOR_BGR2YCrCb)
# covert the BGR image to an YCbCr image
y_cb_cr_img = cv2.cvtColor(or_img, cv2.COLOR_BGR2YCrCb)
# copy the image to create a binary mask later
binary_mask = np.copy(y_cb_cr_img)
# get mean value of the pixels in Y plane
y_mean = np.mean(cv2.split(y_cb_cr_img)[0])
# get standard deviation of channel in Y plane
y_std = np.std(cv2.split(y_cb_cr_img)[0])
# classify pixels as shadow and non-shadow pixels
for i in range(y_cb_cr_img.shape[0]):
for j in range(y_cb_cr_img.shape[1]):
if y_cb_cr_img[i, j, 0] < y_mean - (y_std / 3):
# paint it white (shadow)
binary_mask[i, j] = [255, 255, 255]
else:
# paint it black (non-shadow)
binary_mask[i, j] = [0, 0, 0]
# Using morphological operation
# The misclassified pixels are
# removed using dilation followed by erosion.
kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(binary_mask, kernel, iterations=3)
# sum of pixel intensities in the lit areas
spi_la = 0
# sum of pixel intensities in the shadow
spi_s = 0
# number of pixels in the lit areas
n_la = 0
# number of pixels in the shadow
n_s = 0
# get sum of pixel intensities in the lit areas
# and sum of pixel intensities in the shadow
for i in range(y_cb_cr_img.shape[0]):
for j in range(y_cb_cr_img.shape[1]):
if erosion[i, j, 0] == 0 and erosion[i, j, 1] == 0 and erosion[i, j, 2] == 0:
spi_la = spi_la + y_cb_cr_img[i, j, 0]
n_la += 1
else:
spi_s = spi_s + y_cb_cr_img[i, j, 0]
n_s += 1
# get the average pixel intensities in the lit areas
average_ld = spi_la / n_la
# get the average pixel intensities in the shadow
average_le = spi_s / n_s
# difference of the pixel intensities in the shadow and lit areas
i_diff = average_ld - average_le
# get the ratio between average shadow pixels and average lit pixels
ratio_as_al = average_ld / average_le
# added these difference
for i in range(y_cb_cr_img.shape[0]):
for j in range(y_cb_cr_img.shape[1]):
if erosion[i, j, 0] == 255 and erosion[i, j, 1] == 255 and erosion[i, j, 2] == 255:
y_cb_cr_img[i, j] = [y_cb_cr_img[i, j, 0] + i_diff, y_cb_cr_img[i, j, 1] + ratio_as_al,
y_cb_cr_img[i, j, 2] + ratio_as_al]
else:
y_cb_cr_img[i, j] = y_cb_cr_img1[i,j]
# # # covert the YCbCr image to the BGR image
final_image = cv2.cvtColor(y_cb_cr_img, cv2.COLOR_YCR_CB2RGB)
#dilation = cv2.dilate(final_image,kernel,iterations = 1)
cv2.imwrite('im4.png', dilation)
# blur = cv2.GaussianBlur(final_image,(5,5),cv2.BORDER_DEFAULT)
return final_image
if __name__== "__main__":
#shadow_removal(cv2.Umat('im1.png'))
shadow_removal('im1.png')
导入cv2
def阴影_移除(路径):
或_img=cv2.imread(路径)
y_cb_cr_img1=cv2.cvt颜色(或_img,cv2.COLOR_BGR2YCrCb)
#将BGR映像转换为YCbCr映像
y_cb_cr_img=cv2.cvt颜色(或_img,cv2.COLOR_BGR2YCrCb)
#稍后复制图像以创建二进制掩码
二进制掩码=np.copy(y\u cb\u cr\u img)
#获取Y平面上像素的平均值
y_平均值=np.平均值(cv2.分割(y_cb_cr_img)[0])
#获得Y平面上通道的标准偏差
y_std=np.std(cv2.split(y_cb_cr_img)[0])
#将像素分为阴影像素和非阴影像素
对于范围内的i(y_cb_cr_img.shape[0]):
对于范围内的j(y_cb_cr_img.shape[1]):
如果y_cb_cr_img[i,j,0]
非常感谢您的帮助,因为我已经坚持了一个星期了
对不起,如果我的问题问得不好,但我是这个论坛和整个领域的新手;我快发疯了 在numba中使用cuda目标进行矢量化,或在numba cuda.jit目标中。这里采用的方法不是如何在GPU上运行这样的代码。如果你想学习如何正确使用numba,有很多在线资源。在GPU上使用OpenCV最直接的方法是直接使用OpenCV(它具有GPU支持功能),而不是通过numba.Hi!谢谢你的回复!可以使用numba运行我的代码吗?我读过一些东西,比如说没有办法使用cuda运行opencv脚本;其他人谈到使用UMat,其他人谈到openCL;我真的迷路了;请你能给我一点指导…还有一件事,numba部分是我忘记删除的部分,我发布的尾声不应该在gpu上运行。我想知道如何在GPU上运行这段代码。上面有很多问题,因此提供了如何将OpenCV与CUDA结合使用的示例。是一个例子,这是一个使用C++而不是Python的例子;在numba矢量化和cuda目标或numba cuda.jit目标中使用cpp会容易得多。这里采用的方法不是如何在GPU上运行这样的代码。如果你想学习如何正确使用numba,有很多在线资源。在GPU上使用OpenCV最直接的方法是直接使用OpenCV(它具有GPU支持功能),而不是通过numba.Hi!谢谢你的回复!可以使用numba运行我的代码吗?我读过一些东西,比如说没有办法使用cuda运行opencv脚本;其他人谈到使用UMat,其他人谈到openCL;我真的迷路了;请你能给我一点指导…还有一件事,numba部分是我忘记删除的部分,我发布的尾声不应该在gpu上运行。我想知道如何在GPU上运行这段代码。上面有很多问题,因此提供了如何将OpenCV与CUDA结合使用的示例。是一个例子,这是一个使用C++而不是Python的例子;使用cpp更容易解决这个问题