当前位置:   article > 正文

opencv画坐标系(便于与世界坐标转化)、检测色块(目标物体)

opencv画坐标系



在这里插入图片描述

① 根据图像尺寸遍历出所有节点;
② 根据行列个数得出边界节点;
③ 画点、两点连线;
④ 将视野中心点特殊标记;
⑤ 实现当色块位于网格节点或者视野中心点时,显示坐标值在图像上;
⑥ 可调整网格间距,10、20或者40等可以被尺寸值整除的数



from collections import  deque  
import numpy as np   
import cv2  
import time  


class Painter(object):
    def __init__(self,img,start_point=(20,20)):
        self.horizons_center = (0,0)
        self.point_list = []
        self.line_list = []
        self.get_point_line(img,start_point)

    def add_text(self,img,text,point,size=0.5,color=(0,0,255)):
        cv2.putText(img,text,point,cv2.FONT_HERSHEY_COMPLEX,size,color,1)
        return img

    def draw_a_circle(self,img,point,radius=3,color=(255,0,0),thickness=2):
        img = self.draw_a_point(img,point,radius,color,thickness)
        return img

    def draw_a_point(self,img,point,radius=3,color=(255,0,0),thickness=-4):
        cv2.circle(img, point, radius, color, thickness)
        return img

    def draw_all_point(self,img):
        for point in self.point_list:
            if point == self.horizons_center:
                img = self.draw_a_point(img,self.horizons_center,6,(0,0,255))
                lable_point = (self.horizons_center[0]-5,self.horizons_center[1]-5)
                img = self.add_text(img,str(self.horizons_center),lable_point)
            else:
                img = self.draw_a_point(img,point)
        return img

    def draw_a_line(self,img,start_point,end_point,color=(0,255,0),thickness=1,lineType=4):
        cv2.line(img, start_point, end_point, color, thickness, lineType)
        return img

    def draw_all_line(self,img):
        for point in self.line_list:
            img = self.draw_a_line(img, point[0], point[1])
        return img

    def get_point_line(self,img,start_point):
        start_value = start_point[0]
        space = start_value
        img_x,img_y = img.shape[1],img.shape[0]
        self.horizons_center = (int(img_x/2),int(img_y/2))
        
        for x_point in range(start_value,img_x,space):
            for y_point in range(start_value,img_y,space):
                self.point_list.append((x_point,y_point))

        num_row = int((img_y-start_value)/space)
        num_column = int((img_x-start_value)/space)
        for j in range(1,num_column+1):
            self.line_list.append(((j*space,start_value),(j*space,(img_y-space))))
        for i in range(1,num_row+1):
            self.line_list.append(((start_value,i*space),((img_x-space),i*space)))


class FindTarget(object):
    def __init__(self):
        self.redLower = np.array([156, 43, 46]) #设定红色阈值,HSV空间 
        self.redUpper = np.array([180, 255, 255])
        self.target_position = (0,0)
        self.target_radius = 0
        cv2.namedWindow("camera",1)
        self.camera = cv2.VideoCapture(0)

    def get_one_flame(self):
        ret,img = self.camera.read()
        return ret,img

    def start_find(self,img):
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)   #转到HSV空间
        mask_red = cv2.inRange(hsv, self.redLower, self.redUpper)  #根据阈值构建掩膜
        mask_red = cv2.erode(mask_red, None, iterations=2)  #腐蚀操作
        mask_red = cv2.dilate(mask_red, None, iterations=2)  #膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点
        cnts_red = cv2.findContours(mask_red.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]  #轮廓检测
        if len(cnts_red) > 0:
            c = max(cnts_red, key=cv2.contourArea) #找到面积最大的轮廓
            (red_x,red_y), self.target_radius = cv2.minEnclosingCircle(c)
            self.target_position = (int(red_x),int(red_y))
            return True,img
        return False,img

    def is_horizons_center(self,center):
        x_low,x_high = center[0]-5,center[0]+5
        y_low,y_high = center[1]-5,center[1]+5
        x_target,y_target = self.target_position[0],self.target_position[1]
        if x_low<x_target<x_high and y_low<y_target<y_high:
            return True
        return False

    def is_target_on_node(self,point_list):
        x_target,y_target = self.target_position[0],self.target_position[1]
        for point in point_list:
            if (point[0]-3)<x_target<(point[0]+3) and (point[1]-3)<y_target<(point[1]+3):
                return True
        return False

    def show_result(self,img):
        cv2.imshow('Frame', img)

    def is_exit(self):
        k = cv2.waitKey(5)&0xFF 
        if k == 27:  
            return True
        return False

    def close(self):
        self.camera.release()  #摄像头释放 
        cv2.destroyAllWindows()#销毁所有窗口 

    
    
if __name__ == '__main__':

    toFind = FindTarget()
    ret,flame = toFind.get_one_flame()
    
    if ret:
        painter = Painter(flame,(40,40))
        while True:
            _,flame = toFind.get_one_flame()
            result,flame = toFind.start_find(flame)
            if result:
                flame = painter.draw_a_circle(flame,toFind.target_position,int(toFind.target_radius),(0,255,255))
                flame = painter.draw_a_point(flame,toFind.target_position,5,(0,255,0))
                flame = painter.add_text(flame,str(toFind.target_position),toFind.target_position)
                if toFind.is_horizons_center(painter.horizons_center):
                    flame = painter.add_text(flame,'arrive center point!',(30,30),1,(0,0,255))
                if toFind.is_target_on_node(painter.point_list):
                    flame = painter.add_text(flame,'YES!',(550,30),1,(0,0,255))                   
                print('目标物体坐标:  ',toFind.target_position)               
            flame = painter.draw_all_point(flame)
            flame = painter.draw_all_line(flame)

            toFind.show_result(flame)
            if toFind.is_exit():
                break
    else:
        print ('No Camera')
        
    toFind.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/119725
推荐阅读
相关标签
  

闽ICP备14008679号