Commit 7505ae83d476522b26d89af659638a904af7c97b

Authored by Liu Wenzhuo
1 parent 022e3355

first change

Showing 1 changed file with 194 additions and 0 deletions   Show diff stats
temp1.py 0 → 100644
... ... @@ -0,0 +1,194 @@
  1 +# -*- coding: utf-8 -*-
  2 +"""
  3 +Spyder Editor
  4 +
  5 +This is a temporary script file.
  6 +"""
  7 +import cv2
  8 +import numpy as np
  9 +import matplotlib.pyplot as plt
  10 +import math
  11 +
  12 +img = cv2.imread('piece5.jpeg',0)
  13 +#cv2.imshow('image',img)
  14 +img=cv2.blur(img,(10,10))
  15 +
  16 +edges = cv2.Canny(img,10,20)
  17 +#cv2.imshow('Canny',edges)
  18 +#cv2.waitKey(0)
  19 +#cv2.destroyALLWindows()
  20 +
  21 +[m,n]=img.shape
  22 +
  23 +Gx=np.zeros((m-1,n-1))
  24 +Gy=np.zeros((m-1,n-1))
  25 +
  26 +G=np.zeros((m-1,n-1))
  27 +
  28 +theta=np.zeros((m-1,n-1))
  29 +
  30 +#calcul le gradient
  31 +for i in range(m-1):
  32 + for j in range(n-1):
  33 + Gx[i,j]=img[i,j+1]-img[i,j]
  34 + Gy[i,j]=img[i+1,j]-img[i,j]
  35 + G[i,j]=math.sqrt(Gy[i,j]*Gy[i,j]+Gx[i,j]*Gx[i,j])
  36 + theta[i,j]=math.atan(Gy[i,j]/(Gx[i,j]+0.0001))#direction de gradient
  37 + if (G[i,j]<3):
  38 + img[i,j]=0
  39 +#cv2.imshow('gra',img)
  40 +
  41 +#supression des non-max on trouve img2
  42 +#division en 4 directions:
  43 +img2=img.copy()
  44 +for i in range(1,m-1):
  45 + for j in range(1,n-1):
  46 + if theta[i,j]>=3/8*math.pi:
  47 + theta[i,j]=0
  48 + if (img[i,j]<=img[i+1,j] or img[i,j]<=img[i-1,j]):
  49 + img2[i,j]=0
  50 + elif theta[i,j]>=1/8*math.pi:
  51 + theta[i,j]=1
  52 + if (img[i,j]<=img[i+1,j+1] or img[i,j]<=img[i-1,j-1]):
  53 + img2[i,j]=0
  54 + elif theta[i,j]>=-1/8*math.pi:
  55 + theta[i,j]=2
  56 + if (img[i,j]<=img[i,j+1] or img[i,j]<=img[i,j-1]):
  57 + img2[i,j]=0
  58 + else:
  59 + theta[i,j]=3
  60 + if (img[i,j]<=img[i+1,j-1] or img[i,j]<=img[i-1,j+1]):
  61 + img2[i,j]=0
  62 +#cv2.imshow('image',img2)
  63 +
  64 +#seuillage de contour:
  65 +a=30
  66 +b=100
  67 +img3=img2.copy()
  68 +img4=img2.copy()
  69 +for i in range(0,m-1):
  70 + for j in range(0,n-1):
  71 + if (G[i,j]<10):
  72 + img3[i,j]=0
  73 + #print (G[i,j])
  74 + if (G[i,j]<20):
  75 + img4[i,j]=0
  76 +
  77 +def findline(img3,img4,m,n,flag):
  78 + m1=m+1
  79 + n1=n+1
  80 + while (m!=m1 or n!=n1):
  81 + flagg=0
  82 + for i in range (1,4):
  83 + if (flagg==1):
  84 + break
  85 + for j in range(1,4):
  86 + if (img3[m-2+i,n-2+j]!=0):
  87 + img4[m-2+i,n-2+j]=img3[m-2+i,n-2+j]
  88 + m1=m-2+i
  89 + n1=n-2+j
  90 + flag[m,n]=1
  91 + flagg=1
  92 + m=m1
  93 + n=n1
  94 +
  95 +[a,b]=img3.shape
  96 +flag=np.zeros((a,b))
  97 +for i in range (2,a-1):
  98 + for j in range (2,a-1):
  99 + if (img4[i,j]!=0 and flag[i,j]==0):
  100 + findline(img3,img4,i,j,flag)
  101 +
  102 +
  103 +for i in range (a):
  104 + for j in range (b):
  105 + if (img4[i,j]!=0):
  106 + img4[i,j]=255
  107 +
  108 +
  109 +
  110 +cv2.circle(edges,(113,114),108,(255,255,255),3)
  111 +cv2.imshow('canny',edges)
  112 +
  113 +
  114 +
  115 +cv2.waitKey(0)
  116 +cv2.destroyAllWindows()
  117 +
  118 +def find_circle(img,a,b):
  119 +
  120 + [m,n]=img.shape
  121 +
  122 +
  123 + C_x = 0
  124 + C_y = 0
  125 + R = range(a,b,2)
  126 + img5 = img.copy()
  127 + test = np.zeros((m,n,len(R)))
  128 + for r in range(0,len(R)):
  129 + for i in range(0,m):
  130 + for j in range(0,n):
  131 + if img5[i,j] != 0:
  132 + #print(1)
  133 + for theta in range(0,360):
  134 + C_x = i + int(R[r]*math.sin((theta/180)*math.pi))
  135 + C_y = j + int(R[r]*math.cos((theta/180)*math.pi))
  136 + if (C_x<m and C_y<n):
  137 + test[C_x,C_y,r] += 1
  138 + test1 = test.copy()
  139 + compte1 = []
  140 + compte2 = []
  141 + compte3 = []
  142 + big = 0
  143 + for c in range(0,3):
  144 + big = np.amax(test1)
  145 + for i in range(0,m):
  146 + for j in range(0,n):
  147 + for r in range(0,len(R)):
  148 + if test1[i,j,r] == big:
  149 + compte1.append(i)
  150 + compte2.append(j)
  151 + compte3.append(R[r])
  152 + test1[i,j,r] = 0
  153 + Rt=np.amax(compte3)
  154 + for (i in range 3):
  155 + if (compte3[i]==R):
  156 + xt=compte1[i]
  157 + yt=compte2[i]
  158 +
  159 +#print(compte)
  160 +
  161 +
  162 +#x=np.argmax(test)
  163 +
  164 +
  165 +#cv2.circle(img4,(120,120),120)
  166 +
  167 +#hough transform"""
  168 +
  169 +
  170 +
  171 +
  172 +
  173 +
  174 +
  175 +
  176 +
  177 +
  178 +
  179 +
  180 +
  181 +
  182 +
  183 +
  184 +
  185 +
  186 +
  187 +
  188 +
  189 +
  190 +
  191 +
  192 +
  193 +
  194 +
... ...