Commit 7505ae83 authored by Liu Wenzhuo's avatar Liu Wenzhuo
Browse files

first change

parent 022e3355
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
img = cv2.imread('piece5.jpeg',0)
#cv2.imshow('image',img)
img=cv2.blur(img,(10,10))
edges = cv2.Canny(img,10,20)
#cv2.imshow('Canny',edges)
#cv2.waitKey(0)
#cv2.destroyALLWindows()
[m,n]=img.shape
Gx=np.zeros((m-1,n-1))
Gy=np.zeros((m-1,n-1))
G=np.zeros((m-1,n-1))
theta=np.zeros((m-1,n-1))
#calcul le gradient
for i in range(m-1):
for j in range(n-1):
Gx[i,j]=img[i,j+1]-img[i,j]
Gy[i,j]=img[i+1,j]-img[i,j]
G[i,j]=math.sqrt(Gy[i,j]*Gy[i,j]+Gx[i,j]*Gx[i,j])
theta[i,j]=math.atan(Gy[i,j]/(Gx[i,j]+0.0001))#direction de gradient
if (G[i,j]<3):
img[i,j]=0
#cv2.imshow('gra',img)
#supression des non-max on trouve img2
#division en 4 directions:
img2=img.copy()
for i in range(1,m-1):
for j in range(1,n-1):
if theta[i,j]>=3/8*math.pi:
theta[i,j]=0
if (img[i,j]<=img[i+1,j] or img[i,j]<=img[i-1,j]):
img2[i,j]=0
elif theta[i,j]>=1/8*math.pi:
theta[i,j]=1
if (img[i,j]<=img[i+1,j+1] or img[i,j]<=img[i-1,j-1]):
img2[i,j]=0
elif theta[i,j]>=-1/8*math.pi:
theta[i,j]=2
if (img[i,j]<=img[i,j+1] or img[i,j]<=img[i,j-1]):
img2[i,j]=0
else:
theta[i,j]=3
if (img[i,j]<=img[i+1,j-1] or img[i,j]<=img[i-1,j+1]):
img2[i,j]=0
#cv2.imshow('image',img2)
#seuillage de contour:
a=30
b=100
img3=img2.copy()
img4=img2.copy()
for i in range(0,m-1):
for j in range(0,n-1):
if (G[i,j]<10):
img3[i,j]=0
#print (G[i,j])
if (G[i,j]<20):
img4[i,j]=0
def findline(img3,img4,m,n,flag):
m1=m+1
n1=n+1
while (m!=m1 or n!=n1):
flagg=0
for i in range (1,4):
if (flagg==1):
break
for j in range(1,4):
if (img3[m-2+i,n-2+j]!=0):
img4[m-2+i,n-2+j]=img3[m-2+i,n-2+j]
m1=m-2+i
n1=n-2+j
flag[m,n]=1
flagg=1
m=m1
n=n1
[a,b]=img3.shape
flag=np.zeros((a,b))
for i in range (2,a-1):
for j in range (2,a-1):
if (img4[i,j]!=0 and flag[i,j]==0):
findline(img3,img4,i,j,flag)
for i in range (a):
for j in range (b):
if (img4[i,j]!=0):
img4[i,j]=255
cv2.circle(edges,(113,114),108,(255,255,255),3)
cv2.imshow('canny',edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
def find_circle(img,a,b):
[m,n]=img.shape
C_x = 0
C_y = 0
R = range(a,b,2)
img5 = img.copy()
test = np.zeros((m,n,len(R)))
for r in range(0,len(R)):
for i in range(0,m):
for j in range(0,n):
if img5[i,j] != 0:
#print(1)
for theta in range(0,360):
C_x = i + int(R[r]*math.sin((theta/180)*math.pi))
C_y = j + int(R[r]*math.cos((theta/180)*math.pi))
if (C_x<m and C_y<n):
test[C_x,C_y,r] += 1
test1 = test.copy()
compte1 = []
compte2 = []
compte3 = []
big = 0
for c in range(0,3):
big = np.amax(test1)
for i in range(0,m):
for j in range(0,n):
for r in range(0,len(R)):
if test1[i,j,r] == big:
compte1.append(i)
compte2.append(j)
compte3.append(R[r])
test1[i,j,r] = 0
Rt=np.amax(compte3)
for (i in range 3):
if (compte3[i]==R):
xt=compte1[i]
yt=compte2[i]
#print(compte)
#x=np.argmax(test)
#cv2.circle(img4,(120,120),120)
#hough transform"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment