Problem 1:Loss of value.I take a operation of return value in the end of fuction.But I find a phenomenon,after this operation of return value ,the calculation is NONE.So I try to find where is problem.I use the information web to get other's experiment.One said that I haven't use class to creat Instence object,this action lead to loss of data,and under of the class's fuction called'static way' can't get data.SoI use some time to correct this problem.But finally,I find this no the real problem.So I try to use a scientifical way to locate this problem,I find a funny phenomenon:at the beginning,I run the code can see the value by print way,but when I try to know whether or not the value is returning(because Iater's calculation can't run,we should before this problem to find problem).So I after the return I use print way to show result.But unfortunately,None.Yes,I can't image what lead to it.Until I put this print way in oringinal fuction,but occear a stange thing:the value showing disappear,too.Same code,why?Wait,same code,between the two running,same of this thing is after a code word'cv2.destoryAllwindows()'.What can you think?Sequence.Yes,there are a thing hider my code or lost value,I think that should be this code word 'cv2.destoryAllwindows()',I put this code in ending of the main fuction.Run,yes,it can return value rightly.After this,I find a data that narrate the cv2.destoryAllwindows() have cotrol fuction's folwing,after it's location that all statment will lost efftiveness.So far,the problem is solved.
source code:
import cv2
import numpy as np
image_path='/home/pi/photo/image.jpg'
#
class circle:
def get_coodrinate(image_path):
img=cv2.imread(image_path)
w = 20
h = 5
circle_x=[]
circle_y=[]
circle_size=[]
circle_coordinate=[]
phone_coordinate=[]
phone_coordinate=[]
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 10e1
params.maxArea = 10e3
params.minDistBetweenBlobs = 25
params.filterByConvexity = False
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
minThreshValue = 55
_, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("gray",gray)
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(gray)
#result show
for kp in keypoints:
x=kp.pt[0]
y=kp.pt[1]
size=kp.size
#unify the unit to mm (D means nm become mm)
z=float(input("请输入手机屏幕与接收屏幕之间的距离: "))
D=650/1,000,000
lx=float(input('请输入手机的宽:'))
ly=float(input('请输入手机的长:'))
#purpose to get a error to replace with this param
response=kp.response
circle_data=circle(x,y,size,z,D,lx,ly)
circle_x.append(circle_data.X_location)
circle_y.append(circle_data.Y_location)
circle_size.append(circle_data.radius)
circle_coordinate.append(circle_data.X_location,circle_data.Y_location)
circle_z = circle_data.z #carmer between receive's distance
circle_D = circle_data.D #wavelight of incident light
circle_lx = circle_data.lx
circle_ly = circle_data.ly
print('x:',circle_x)
print('y:',circle_y)
print('radius:',circle_size)
print('z:',circle_z)
print('D:',circle_D)
print('lx:',circle_ly)
print('ly:',circle_lx)
print(len(keypoints))
print("circle_coordinate:",circle_coordinate)
# opencv
im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# plt
# im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)
cv2.imshow('findCorners', color_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return circle_coordinate,circle_z,circle_D,circle_lx,circle_ly
def main():
circle.get_original(image_path)
#left_top_p,left_down_p,right_top_p,right_down_p,h1=circle.find_rect(image_path)
#circle.jiaozheng(left_top_p,left_down_p,right_top_p,right_down_p,image_path,h1)
circle_z,circle_D,circle_lx,circle_ly,circle_coordinate=circle.get_coodrinate(image_path)
circle_pointsdistance=[]
z_scores = []
######Z-scires algorithm and mean or std way
for i in circle_coordinate:
circle_pointsdistance.append(circle.find_proximatepoints(circle_coordinate[i],circle_coordinate))
mean = np.mean(circle_pointsdistance)
std = np.std(circle_pointsdistance)
for i in circle_pointsdistance:
z_score = (circle_pointsdistance[i] - mean) / std
z_scores.append(z_score)
threshold = 2
###### judge and get result(bool index)
filtered_data = circle_pointsdistance[np.abs(z_scores) <= threshold]
True_disance=np.mean(filtered_data)
circle.calculate_resolution(circle_z,circle_D,True_disance,circle_lx,circle_ly)
circle.main()
Problem 2:object of type 'float' has no len()
When I use 'for loop' in len(corcle_coordinate),it remind me that have report error.Though I use a float array,but use len() way to calculate its lenth.According to past experience,it should be no problem.But after my carefully check ,I find that is because my return value is error location,it's supposed to be loaction of the array is replace by a float number,so report an error is right.
Problem 3:It can't rightly endow the 'circle_data' the value to 'circle_x','circle_z',and so on .
error:local variable 'circlr_data' referenced begore assignment
source code:
for kp in keypoints:
x=kp.pt[0]
y=kp.pt[1]
size=kp.size
#unify the unit to mm (D means nm become mm)
z=float(input("请输入手机屏幕与接收屏幕之间的距离: "))
D=650/1,000,000
lx=float(input('请输入手机的宽:'))
ly=float(input('请输入手机的长:'))
#purpose to get a error to replace with this param
response=kp.response
circle_data=circle(x,y,size,z,D,lx,ly)
circle_x.append(circle_data.X_location)
circle_y.append(circle_data.Y_location)
circle_size.append(circle_data.radius)
circle_coordinate.append([circle_data.X_location,circle_data.Y_location])
circle_z = circle_data.z #carmer between receive's distance
circle_D = circle_data.D #wavelight of incident light
circle_lx = circle_data.lx
circle_ly = circle_data.ly
That's because the circle_data is range in the for loop,out of the range,its value is destoried.
Solveing:please put the initializing before the for loop,and let its range can be full of the fuction.
Problem4:'int' type can't sequence with 'float' type
error:can‘t multiply sequence by non-int of type ‘numpy.float64‘
That's because I use a int type array to sequence with a float type number,so will offer to this wrong.