首页 > 解决方案 > 试图运行一个python脚本,我主要在我的gpu上使用opencv

问题描述

所以我什么都试过了,我试过在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_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')

非常感谢任何帮助,因为我已经坚持了一周了。

抱歉,如果我的问题没有很好地提出,但我是这个论坛和整个领域的新手;我正在适应!

标签: pythonopencvimage-processingcomputer-visionimage-preprocessing

解决方案


推荐阅读