带有虚线或虚线的opencv矩形

时间:2022-06-01 12:21:01

I have a line of code here that uses the python binding for opencv:

我在这里有一行代码,它使用opencv的python绑定:

cv2.rectangle(img, (box[1], box[0]), (box[3], box[2]), (255,0,0), 4)

This draws a red rectangle on image img of thickness 4.

这会在厚度为4的图像img上绘制一个红色矩形。

But is there a way the lines of the rectangles can be stylized? Not too much. Just dotted, or dashed, that's it really.

但有没有办法可以将矩形线条风格化?不是太多。只是点缀,或虚线,这是真的。

3 个解决方案

#1


8  

import cv2
import numpy as np
def drawline(img,pt1,pt2,color,thickness=1,style='dotted',gap=20):
    dist =((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2)**.5
    pts= []
    for i in  np.arange(0,dist,gap):
        r=i/dist
        x=int((pt1[0]*(1-r)+pt2[0]*r)+.5)
        y=int((pt1[1]*(1-r)+pt2[1]*r)+.5)
        p = (x,y)
        pts.append(p)

    if style=='dotted':
        for p in pts:
            cv2.circle(img,p,thickness,color,-1)
    else:
        s=pts[0]
        e=pts[0]
        i=0
        for p in pts:
            s=e
            e=p
            if i%2==1:
                cv2.line(img,s,e,color,thickness)
            i+=1

def drawpoly(img,pts,color,thickness=1,style='dotted',):
    s=pts[0]
    e=pts[0]
    pts.append(pts.pop(0))
    for p in pts:
        s=e
        e=p
        drawline(img,s,e,color,thickness,style)

def drawrect(img,pt1,pt2,color,thickness=1,style='dotted'):
    pts = [pt1,(pt2[0],pt1[1]),pt2,(pt1[0],pt2[1])] 
    drawpoly(img,pts,color,thickness,style)

im = np.zeros((800,800,3),dtype='uint8')
s=(234,222)
e=(500,700)
drawrect(im,s,e,(0,255,255),1,'dotted')

cv2.imshow('im',im)
cv2.waitKey()      

#2


3  

OpenCV does not (currently) support line properties beyond thickness and anti-aliasing.

OpenCV(目前)不支持超出厚度和抗锯齿的线属性。

#3


3  

  • openCV is opensource library so you can find source code for drawing and modify it slightly
  • openCV是开源库,因此您可以找到用于绘制的源代码并稍微修改它

  • you can use LineIterator and get any style you want in a few lines of code
  • 你可以使用LineIterator并在几行代码中获得你想要的任何样式

#1


8  

import cv2
import numpy as np
def drawline(img,pt1,pt2,color,thickness=1,style='dotted',gap=20):
    dist =((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2)**.5
    pts= []
    for i in  np.arange(0,dist,gap):
        r=i/dist
        x=int((pt1[0]*(1-r)+pt2[0]*r)+.5)
        y=int((pt1[1]*(1-r)+pt2[1]*r)+.5)
        p = (x,y)
        pts.append(p)

    if style=='dotted':
        for p in pts:
            cv2.circle(img,p,thickness,color,-1)
    else:
        s=pts[0]
        e=pts[0]
        i=0
        for p in pts:
            s=e
            e=p
            if i%2==1:
                cv2.line(img,s,e,color,thickness)
            i+=1

def drawpoly(img,pts,color,thickness=1,style='dotted',):
    s=pts[0]
    e=pts[0]
    pts.append(pts.pop(0))
    for p in pts:
        s=e
        e=p
        drawline(img,s,e,color,thickness,style)

def drawrect(img,pt1,pt2,color,thickness=1,style='dotted'):
    pts = [pt1,(pt2[0],pt1[1]),pt2,(pt1[0],pt2[1])] 
    drawpoly(img,pts,color,thickness,style)

im = np.zeros((800,800,3),dtype='uint8')
s=(234,222)
e=(500,700)
drawrect(im,s,e,(0,255,255),1,'dotted')

cv2.imshow('im',im)
cv2.waitKey()      

#2


3  

OpenCV does not (currently) support line properties beyond thickness and anti-aliasing.

OpenCV(目前)不支持超出厚度和抗锯齿的线属性。

#3


3  

  • openCV is opensource library so you can find source code for drawing and modify it slightly
  • openCV是开源库,因此您可以找到用于绘制的源代码并稍微修改它

  • you can use LineIterator and get any style you want in a few lines of code
  • 你可以使用LineIterator并在几行代码中获得你想要的任何样式