图像处理案例01

例01

1.1 目标

找出图形中的圆弧。
在这里插入图片描述

1.2 步骤

  1. 读入图像。彩色图转灰度图。
  2. 图像处理。读灰度图阈值分割,得到img_thre,除掉图中的蓝色和绿色,只留下白色。对img_thre做水平梯度得到gx,这样除去图像中的横线。再对gx求轮廓,根据轮廓面积得到圆弧的索引。求出两个圆弧的外接矩形,画出外接矩形。
  3. 显示图像。

1.3 代码

## 1.  读入图像
img = cv2.imread("EXAMPLES/缺陷检测/p_test_01.jpeg",1)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
## 2.  图像处理
_, img_thre = cv2.threshold(img_gray,200,255,cv2.THRESH_BINARY)
gx = cv2.Sobel(img_thre,-1,1,0,None,11)
plt.imshow(gx,'gray')
cnts ,_ = cv2.findContours(gx,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
areas = []
for i in cnts:
    areas.append(cv2.contourArea(i))
o = img.copy()
idx = np.argsort(areas)[::-1]
for i in idx[:2]:
    x, y, w, h = cv2.boundingRect(cnts[i])
    brcnt = np.array([[[x, y]], [[x + w, y]], [[x + w, y + h]], [[x, y + h]]])
    cv2.drawContours(o, [brcnt], -1, (255, 0, 255), 3)
## 3. 显示图像
plt.subplot(151)
plt.axis('off')
plt.imshow(img)
plt.subplot(152)
plt.axis('off')
plt.imshow(img_gray,'gray')
plt.subplot(153)
plt.axis('off')
plt.imshow(img_thre,'gray')
plt.subplot(154)
plt.axis('off')
plt.imshow(gx,'gray')
plt.subplot(155)
plt.axis('off')
plt.imshow(o)

在这里插入图片描述

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值