• 图片算法学习---欧式算法


    算法的基本原理;将当前像素与领接的下部和右部像素进行比较,如果相似,则将当前的像素设置为白色,否则设置为黑色

    如果两个像素点的欧式距离小于某个常数的阈值,则认为相似

    import cv2

    import numpy as np

    fn="xx.jpg"

    def get_EuclideanDistance(x,y):

      myx=np.array(x)

      myy=np.array(y)

      return np.sqrt(np.sum(myx-myy)*(myx-myy))

    if __name__=='__main__':

      print('loading %s...'%fn)

        print('working')

       myimg1=cv2.imread(fn)

      w=myimg1.shape[1]

      h=myimg1.shape[0]

      sz1=w

      sz2=h

      #创建空白图像

      myimg2=np.zeros((sz2,sz1,3),np.uint8)

      #对比产生线条

       black=np.array([0,0,0])

       white=np.array([255,255,255])

       centecolor=np.array([125,125,125])

       for y in range(0,sz2-1):

         for x in range(0,sz1-1):

           mydown=myimg1[y+1,x,:]

          myright=myimg1[y,x+1,:]

          myhere=myimg1[y,x,:]

          lmyhere=myhere

           lmyright=myright

          lmydowm=mydown

          if any(get_EuclideanDistance(lmyhere,lmydown)>16) and any(get_EuclideanDistance(lmyhere,lmyright)>16):

              myimg2[y,x,:]=black

          elif any(get_EuclideanDistance(lmyhere,lmydown)<=16) and any(get_EclidDistance(lmyhere,lmright)<=16):

              myimg2[y,x,:]=white

          else:

            myimg2[y,x,:]=centercolor

           print('.')

          cv2.namedWindow('img2')

          cv2.imshow('img2',myimg2)

          cv2.waitKey()

          cv2.destroyAllWindows()

    运行出现的Bug:

     ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    修改前

    :# if get_EuclideanDistance(lmyhere, lmydown) > 16 and get_EuclideanDistance(lmyhere, lmyright) > 16:
    # myimg2[y, x, :] = black
    # elif get_EuclideanDistance(lmyhere, lmydown) <= 16 and get_EuclideanDistance(lmyhere, lmyright) <= 16:
    # myimg2[y, x, :] = white
    # else:()i
    # myimg2[y, x, :] = centercolor

    修改后:

    if any(get_EuclideanDistance(lmyhere, lmydown) > 16) and any(get_EuclideanDistance(lmyhere, lmyright) > 16):
    myimg2[y, x, :] = black
    elif any(get_EuclideanDistance(lmyhere, lmydown) <= 16) and any(get_EuclideanDistance(lmyhere, lmyright) <= 16):
    myimg2[y, x, :] = white
    else:
    myimg1[y, x, :] = centercolor

    测试图片:

     

     

  • 相关阅读:
    2020学习 04 python 爬取某政府网站信件
    2020学习03
    2020学习02
    阅读笔记--《一线架构师实践指南》--Pre-architecture阶段
    重构“信息领域热词分析”,实现六种质量属性战术
    pycharm错误:Error updating package list: connect timed out解决
    python连接mysql数据库——编码问题
    阅读笔记--《大型网站技术架构》—02架构
    python查询MySQL数据库的表以及所有字段
    python连接mysql数据库——版本问题
  • 原文地址:https://www.cnblogs.com/who-am-i/p/10497010.html
Copyright © 2020-2023  润新知