OpenCV Series : Target Box Outline Border


角点

c 复制代码
P1      [0]     (255, 000, 000)
P2      [1]     (000, 255, 000)
P3      [2]     (000, 000, 255)
P4      [3]     (000, 000, 000)


垂直矩形框

c 复制代码
	rect = cv2.minAreaRect(cnt)
	targetColor = roi_color
	targetThickness = 1
	targetColor = (255, 255, 255)
	if lineVerbose:
		if True:
		   cv2.line(photo,  (x, y), (x+w//4, y), targetColor, targetThickness)
		   cv2.line(photo,  (x, y), (x, y+h//4), targetColor, targetThickness)
		
		   cv2.line(photo,  (x+3*w//4, y), (x+w, y), targetColor, targetThickness)
		   cv2.line(photo,  (x+w, y), (x+w, y+h//4), targetColor, targetThickness)
		
		   cv2.line(photo,  (x, y+h), (x+w//4, y+h), targetColor, targetThickness)
		   cv2.line(photo,  (x, y+h), (x, y+3*h//4), targetColor, targetThickness)
		
		   cv2.line(photo,  (x+w, y+h), (x+3*w//4, y+h), targetColor, targetThickness)
		   cv2.line(photo,  (x+w, y+h), (x+w, y+3*h//4), targetColor, targetThickness)
		
		   crossLength = 15
		   cv2.line(photo,  (x+w//2-crossLength, y+h//2), (x+w//2+crossLength, y+h//2), targetColor, targetThickness)
		   cv2.line(photo,  (x+w//2, y+h//2-crossLength), (x+w//2, y+h//2+crossLength), targetColor, targetThickness)



倾斜矩形框

c 复制代码
def lineByAngle(photo, p1, p2, length, color, thickness):
    slope = 0 + (p2[1] - p1[1]) / (p2[0] - p1[0])
    theta = np.arctan(slope)
    degree = int(theta * 57.29577) % 360

    length = getDistance(p1, p2) // 4

    if (p2[0] < p1[0]):
        pp = (
            int(p1[0] - length * np.cos(theta)),
            int(p1[1] - length * np.sin(theta))
        )
    else:
        pp = (
            int(p1[0] + length * np.cos(theta)),
            int(p1[1] + length * np.sin(theta))
        )
    cv2.line(photo,  p1, pp, color, thickness)


几何关键点

c 复制代码
	if True:
	    cv2.circle(photo, p1, 1, roi_red, 5)
	    cv2.circle(photo, p2, 1, roi_red, 5)
	    cv2.circle(photo, p3, 1, roi_red, 5)
	    cv2.circle(photo, p4, 1, roi_red, 5)
	
	    center = (int((p1[0] + p3[0]) / 2), int((p2[1] + p4[1]) / 2))
	    cv2.circle(photo, center, 1, roi_green, 5)
c 复制代码
def slopeAngle(p1, p2):
    slope  = (p2[1] - p1[1]) / (p2[0] - p1[0])
    theta  = np.arctan(slope)
    degree = int(theta * 57.29577) % 360
    return theta

def lineByPoint(photo, pp, theta, length, color, thickness):
    if True:
        p1 = (
            int(pp[0] - length * math.cos(theta)),
            int(pp[1] - length * math.sin(theta))
        )
c 复制代码
     center = (int((p1[0] + p3[0]) / 2), int((p2[1] + p4[1]) / 2))
     if not True:
         cv2.circle(photo, p1, 1, roi_blue,  5)
         cv2.circle(photo, p2, 1, roi_green, 5)
         cv2.circle(photo, p3, 1, roi_red,   5)
         cv2.circle(photo, p4, 1, roi_black, 5)
         cv2.circle(photo, center, 1, roi_green, 5)
     if True:
         crossLength = 15
         theta = slopeAngle(p1, p2)
         lineByPoint(photo, center, theta, crossLength, targetColor, targetThickness)
         theta = slopeAngle(p2, p3)
         lineByPoint(photo, center, theta, crossLength, targetColor, targetThickness)


Automatic Mask

c 复制代码
if True:
    matrix = np.array(cnt)
    for i in range(len(matrix)):
        matrix[i][0][0] = matrix[i][0][0] - x
        matrix[i][0][1] = matrix[i][0][1] - y
    mask = np.zeros(characteristic.shape[:2], dtype=np.uint8)
    mask = cv2.fillPoly(mask, [matrix], 255)
    cv2.imshow('Mask', mask)

    image = cv2.bitwise_and(characteristic, characteristic, mask = mask)
    image = stretch(image)
    cv2.imshow('Masked', image)
相关推荐
牧歌悠悠2 小时前
【深度学习】Unet的基础介绍
人工智能·深度学习·u-net
坚毅不拔的柠檬柠檬2 小时前
AI革命下的多元生态:DeepSeek、ChatGPT、XAI、文心一言与通义千问的行业渗透与场景重构
人工智能·chatgpt·文心一言
坚毅不拔的柠檬柠檬2 小时前
2025:人工智能重构人类文明的新纪元
人工智能·重构
jixunwulian2 小时前
DeepSeek赋能AI边缘计算网关,开启智能新时代!
人工智能·边缘计算
Archie_IT2 小时前
DeepSeek R1/V3满血版——在线体验与API调用
人工智能·深度学习·ai·自然语言处理
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
灵感素材坊4 小时前
解锁音乐创作新技能:AI音乐网站的正确使用方式
人工智能·经验分享·音视频
xinxiyinhe4 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
AI服务老曹4 小时前
运用先进的智能算法和优化模型,进行科学合理调度的智慧园区开源了
运维·人工智能·安全·开源·音视频
alphaAIstack4 小时前
大语言模型推理能力从何而来?
人工智能·语言模型·自然语言处理