【问题标题】:NameError in the snippet代码段中的 NameError
【发布时间】:2020-08-29 19:52:39
【问题描述】:
#prepare degraded images by introducing quality distortion by resizing images
def prepare_images(factor):
    path = './SRCNN/source'
    #loop through the file in the directory
    for files in os.listdir(path):
        #open the file
        img = cv2.imread(path + '/' + files)

        #find old and new image dimaensions
        h, w, c = img.shape
        new_height = h /factor
        new_width = w / factor

        #resize the image - down 
        img = (cv2.resize(img, (int(new_width), int(new_height)), interpolation=cv2.INTER_LINEAR))

        #resize the image - up
        img = (cv2.resize(img, (w, h), interpolation = INTER_LINEAR))

        # save the image

        print('Saving {}'.format(files))

        cv2.imwrite('images/{}'.format(files), img)




---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-77-6ba532c6503e> in <module>()
----> 1 prepare_images(2)
      2 #os.listdir('./SRCNN/source')

<ipython-input-76-4a7882c5ab03> in prepare_images(factor)
     16 
     17         #resize the image - up
---> 18         img = (cv2.resize(img, (w, h), interpolation = INTER_LINEAR))
     19 
     20         # save the image

NameError: name 'INTER_LINEAR' is not defined

第一部分是函数。第二部分是该函数中的错误。我该如何解决这个问题?我正在 Google Colab 上编写此代码,并在 Jupyter 上尝试过......

【问题讨论】:

    标签: python python-3.x python-2.7 debugging cv2


    【解决方案1】:

    为了使用INTER_LINEAR,您应该专门导入它。 所以你可以添加一个导入:

    from cv2 import INTER_LINEAR
    

    interpolation = INTER_LINEAR 更改为interpolation = cv2.INTER_LINEAR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多