python之logo编程

Logo标志是一种视觉符号,代表着一个品牌、企业或组织的形象。它通常采用图形、字母或字形来代表一个公司或品牌,起到对徽标拥有公司的识别和推广的作用。Logo的设计需要考虑多种因素,例如颜色搭配、字体选择和构图等,以创造出独特且易于记忆的标志。

本例实现针对给定的logo图案,生成孔位坐标供钻孔加工出logo图案,具体如下:

输入:一张logo图片,钻孔半径,孔间距。

输出:孔位坐标。

实现原理:先读取图片,然后提取轮廓,针对每个轮廓计算其x、y坐标最大值和最小值,在这些值构成的矩形内划分正交网格,网格点为候选圆心坐标点,最后以候选点是否在轮廓内为条件进行筛选,得到最终满足条件的坐标点。

图:logo示例

import cv2
import numpy as np
def generate_grid(contour, spacing):
    con=np.squeeze(contour)
    x_min= np.min(con[:, 0])
    x_max = np.max(con[:, 0])
    y_min = np.min(con[:, 1])
    y_max = np.max(con[:, 1])
    x = np.arange(x_min-spacing, x_max+spacing, spacing)
    y = np.arange(y_min-spacing, y_max+spacing, spacing)
    xx, yy = np.meshgrid(x, y)
    return xx.flatten(), yy.flatten()
​
​
def draw_circle(img, hole_coordinates, radius):
    # 将hole_coordinates转换成numpy数组
    hole_coordinates = np.array(hole_coordinates)
    # 遍历每个圆心点坐标,画圆并显示在img中
    for coordinate in hole_coordinates:
        x, y = coordinate
        cv2.circle(img, (int(x), int(y)), radius, (0, 0, 255), -1)
​
    # 显示图像
    cv2.imshow('image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
​
​
def extract_logo_boundary(img):
    imginfo = img.shape
    height = imginfo[0]
    width = imginfo[1]
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
​
    # 二值化处理
    ret, binary = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)
​
    # 找到所有轮廓
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
​
    # 筛选轮廓
    logo_contours = []
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > 100 and area < 0.5 * width * height:
            logo_contours.append(contour)
​
    # 在原图上绘制轮廓
    img_with_logo = img.copy()
    cv2.drawContours(img_with_logo, logo_contours, -1, (0, 0, 255), 6)
​
    # 显示图像
    cv2.imshow('Image with Logo', img_with_logo)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
​
    return logo_contours
​
​
def generate_hole_coordinates(contour, hole_spacing):
    xx, yy = generate_grid(contour, hole_spacing)
    hole_coordinates = []
    for i, j in zip(xx, yy):
        pt=(i.astype(float),j.astype(float))
        # 使用cv2.pointPolygonTest()函数判断点是否在轮廓内
        result = cv2.pointPolygonTest(contour,pt, measureDist=False)
        if result >= 0:
            hole_coordinates.append((i, j))
    return hole_coordinates
​
​
if __name__ == "__main__":
    image_path = r"C:\Usersxxx\1.jpg"
    img = cv2.imread(image_path)
    logo_boundary = extract_logo_boundary(img)
    hole_spacing = 6
    radius = 2
    hole_coordinates=[]
    for bd in logo_boundary:
        hole_coordinates+=generate_hole_coordinates(bd, hole_spacing)
    draw_circle(img, hole_coordinates, radius)
​

图:提取的logo轮廓

图:获得的孔位结果

从程序运行结果可以看出,处理环形区域内部也计算了孔位坐标外,效果还可以,需要进一步去除环形区域内的孔位点。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值