首页 > 解决方案 > 使用openCV和python绘制缩放网格系统

问题描述

我想做的是在图像上绘制一个网格,顶部有较小的正方形,底部有较大的正方形。我不知道如何做到这一点。我想出了如何使用 open cv 在图像上绘制网格,但不知道如何微调它以获得我想要的结果。

我将使用它来查找检测到的对象的边界框点在哪里。我需要一个较小的边界框在顶部,一个较大的边界框靠近图像的底部。

这就是我想要做的: 在此处输入图像描述

但我无法弄清楚如何在代码中获得曲线。这是我到目前为止所拥有的: 在此处输入图像描述

这是我正在使用的代码。

'''
#This method draws simple grid overthe image based on the passed step
#The pxstep controls the size of the grid
'''
def drawBasicGrid(image, pxstep, midX, midY):
    x = pxstep
    y = pxstep
    #Draw all x lines
    while x < img.shape[1]:
        cv2.line(img, (x, 0), (x, img.shape[0]), color=(255, 0, 255), thickness=1)
        x += pxstep
    
    while y < img.shape[0]:
        cv2.line(img, (0, y), (img.shape[1], y), color=(255, 0, 255),thickness=1)
        y += pxstep
    

这绘制了基本网格。这将创建我用于检测检测到对象的边界点的边界框。

def makeBoundingBox(h,w, step):
    #BBox is 100*100 square or step which is defied
    y = 0
    bbox = []
    while y < h:
        #print("Y value", y)
        x=0
        while x < w:
            #print("X Value", x)
            bbox.append([(x,y), (x+step, y+step)])
            x += step
        y += step
    
    return bbox

这就是我使用它的方式。

#Prepare Images
img = cv2.imread(image)
#img = imutils.resize(img, width=300)
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
(H,W) = img.shape[:2]
#Create bounding boxes for the image
'''
#Creates the boxes for people detection 
#It will look for which bounding boxes the person is in and then 
#Draw a box around their feet
'''
#People Detection
blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)),0.007843,(300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
'''
#Get the center point of the image
'''
midY = H//2
midX = W//2
print("Middle Y pixel", midY)
#Draw center line
cv2.line(img, (0, midY), (W, midY), color=green, thickness=2)
cv2.line(img, (midX, 0), (midX, H), color=green, thickness=2)

#Visual grid drawn
drawBasicGrid(img, step, midX,midY)
bbox = makeBoundingBox(H, W, step) #Used for finding where the endx and startx BBOX  points are

对此的任何帮助将不胜感激。

标签: python-3.xopencvimage-processingbounding-box

解决方案


推荐阅读