Problem faced in opencv‘s project

文章讨论了在使用OpenCV进行图像处理时遇到的问题,包括返回值丢失、数据丢失和类型不匹配。作者发现`cv2.destroyAllWindows()`可能导致数据丢失,并通过科学方法定位问题。最后,通过初始化和正确处理数据类型解决了这些问题。
摘要由CSDN通过智能技术生成

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. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值