即使已安装模块cv2,也未找到

时间:2020-06-16 04:49:18

标签: python opencv image-processing

该程序是使用Python和OpenCV的瑕疵去除程序。我收到一个错误消息,说它不能导入OpenCV。这是代码:

def selectedBlemish(x,y,r):
      global i
      crop_img= source[y:(y+2*r),x:(x+2*r)]
      #i=i+1
      #cv2.imwrite("blemish"+str(i)+".jpg",crop_img)
      return identifyBestPatch(x,y,r)
def identifyBestpatch(x,y,r):
      #nearby patches in all 8 directions
      patches={ }

      key1tUp= appendDictionary(x+2*r,y)
      patches['Key1']=(x+2*r,y,key1tUp[0],key1tup[1])

      key2tUp= appendDictionary(x+2*r,y+r)
      patches['Key2']=(x+2*r,y+r,key2tUp[0],key2tup[1])

      key3tUp= appendDictionary(x-2*r,y)
      patches['Key3']=(x-2*r,y,key3tUp[0],key3tup[1])

      key4tUp= appendDictionary(x-2*r,y-r)
      patches['Key3']=(x-2*r,y-r,key4tUp[0],key4tup[1])

      key5tUp= appendDictionary(x,y+2*r)
      patches['Key5']=(x,y+2*r,key5tUp[0],key5tup[1])

      key6tUp= appendDictionary(x+r,y+2*r)
      patches['Key6']=(x+r,y+2*r,key6tUp[0],key6tup[1])

      key7tUp= appendDictionary(x,y-2*r)
      patches['Key7']=(x,y-2*r,key7tUp[0],key7tup[1])

      key8tUp= appendDictionary(x-r,y-2*r)
      patches['Key8']=(x-r,y-2*r,key8tUp[0],key8tup[1])

      #printing(patches)

      findlowx={}
      findlowy={}

      for key, (x,y,gx,gy) in patches.items():
            findlowx[key]=gx

      for key, (x,y,gx,gy) in patches.items():
            findlowy[key]=gy

      y_key_min=min(findlowy.keys(),key=(lambda k: findlowy[k]))
      x_key_min=min(findlowx.keys(),key=(lambda k: findlowx[k]))

      if x_key_min == y_key_min:
            return patches[x_key_min][0], patches[x_key_min][1]

      else:
            #print("return x & y conflict,can take help from fft")

            return patches[x_key_min][0], patches[x_key_min][1]

def appendDictionary(x,y):
      crop_img=source[y:(y+2*r),x:(x+2*r)]
      gradient_x,gradient_y = sobelfilter(crop_img)
      return gradient_x, gradient_y

def sobelfilter(crop_img):
      #along x-direction
      sobelx64f = cv2.Sobel(crop_img,cv2.CV_64F,1,0,ksize=3)
      abs_xsobel64f=np.absolute(sobelx64f)
      sobel_x8u=np.uint8(abs_xsobel64f)
      gradient_x=np.mean(sobel_x8u)

      #along y direction
      sobelx64f = cv2.Sobel(crop_img,cv2.CV_64F,0,1,ksize=3)
      abs_ysobel64f=np.absolute(sobely64f)
      sobel_y8u=np.uint8(abs_ysobel64f)
      gradient_y=np.mean(sobel_y8u)

      return gradient_x,gradient_y

def blemishRemoval(action, x, y, flags, userdata):
      #referencing the global variables
      globalr, source
      #Action when LMB Pressed
      if action==cv2.EVENT_LBUTTONDOWN:
            #MARKING THE CENTER
            blemishLocation=(x,y)
            #printing(blemishLocation)
            newX,newY = selectedBlemish(x,y,r)
            newPatch = source[newY:(newY+2*r), newX:(newX+2*r)]
            cv2.imwrite("newpatch.jpg",newPatch)
            #create mask for new patch
            mask=255*np.ones(newPatch.shape,newPatch.dtype)
            source=cv2.seamlessClone(newPatch, source, mask, blemishLocation, cv2.NORMAL_CLONE)
            cv2.imshow("Blemish Removal App", source)
            #Action to be taken when LMB released
      elif action==cv2.EVENT_LBUTTONUP:
                  cv2.imshow("blemish Removal App", source)



if __name__== '__main__':
      import cv2
      import numpy as np

      #lists to store the points
      r = 15
      i=0
      source=cv2.imread("Raya.jpg",1)
      #,making a duplicate to work upon
      duplicate = source.copy()
      cv2.namedWindow("Blemish Remover")
      #cv2.resizeWindow("Blemish Remover app", 900,900)
      print("using a patch ofradius 15:")
      cv2.setMouseCallback("Blemish Remover app", blemishRemoval)
      k=0
      #Loop till character is pressed
      while k!=27 :
          cv2.imshow("Blemish Removal", source)
          k=cv2.waitKey(10)& 0xFF
          #other way of cloning
          if k==99:
              source=duplicate.copy()
              cv2.destroyAllWindows()

0 个答案:

没有答案