# -*- coding: utf-8 -*-
#!/usr/bin/env python
# @File : batch_extraction.py
# @Software: PyCharm
import os
import cv2
import numpy as np
def process_picture(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print("单击了鼠标左键")
elif event == cv2.EVENT_RBUTTONDOWN :
print("单击了鼠标右键")
elif flags == cv2.EVENT_FLAG_LBUTTON:
print("按住左键拖动了鼠标")
elif event == cv2.EVENT_MBUTTONDOWN :
print("单击了中间键")
def picture_contours(o_img):
"""
提取V字特征图像特征轮廓
:param o_img: 要处理的图片
:return: 全部的轮廓特征
"""
# print("原图类型%s" % str(o_img.shape))
gray = cv2.cvtColor(o_img, cv2.COLOR_BGRA2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
# cv2.imshow("binary-img", binary)
cts, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
return cts
# def intercept_contour(p_contours, index, c_contours):
# n = len(contours)
# # 原图像大小相同的画布
# contours_img = []
# max_area = 0
# max_i = 0
# for i in range(n):
# temp = np.zeros(o_img.shape, np.uint8)
# contours_img.append(temp)
# contours_img[i] = cv2.drawContours(contours_img[i], contours, i, (255, 255, 255), -1)
# if cv2.contourArea(contours[i]) > max_area:
# max_area = cv2.contourArea(contours[i])
# max_i = i
# # 获取最大轮廓的矩形边界值
# x, y, w, h = cv2.boundingRect(contours[max_i])
# # 画矩形
# cv2.rectangle(o_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# # 取最小矩形轮廓
# # rect = cv2.minAreaRect(contours[max_i])
# # # 矩形四个角点取整
# # box = np.int0(cv2.boxPoints(rect))
# # cv2.drawContours(o, [box], 0, (0, 255, 0), 2)
# # cv2.imshow("feature-img", contours_img[max_i])
# cv2.imshow("finally-img", o_img)
# # 截取图片并保存(截取面积最大的)
# if cv2.imwrite(os.path.join(s_path, p_name), o_img[y:y + h, x:x + w]):
# print("保存图像V特征%s", p_name)
# print("最大轮廓面积%s" % max_area)
if __name__ == "__main__":
# 要处理的图片路径
process_path = "G:\\MyProjects\\python\\Breakout-Identification\\morphologyEX"
# 要保存的图片路径
save_path = "G:\\MyProjects\\python\\Breakout-Identification\\v_feature"
os.chdir(process_path)
print("当前路径%s" % os.getcwd())
print("当前路径下的文件%s" % os.listdir(os.getcwd()))
# 图片路径列表
picture_path_list = os.listdir(os.getcwd())
# 排除不是png图片的文件或文件夹
picture_path_list = [picture_path_list[i] for i in range(0, len(picture_path_list))
if picture_path_list[i].find('png') != -1]
# 图片个数
picture_list_number = len(picture_path_list)
# 图片路径列表的索引
picture_path_index = 0
# 轮廓索引
c_index = 0
# 当前轮廓索引
n_index = 0
# 原图像大小相同的画布
contours_img = []
# 实时显示的轮廓索引列表
n_s_index_list = []
# 要保存的轮廓图像矩阵
save_temp = []
while (1):
# 图片索引超出时跳出循环
if picture_path_index > picture_list_number-1:
print("图片已经全部处理完")
break
# 读取图片
img = cv2.imread(os.path.join(process_path, picture_path_list[picture_path_index]))
# 图片文件名
img_name = picture_path_list[picture_path_index]
# 图片的全部轮廓
contours = picture_contours(img)
# 轮廓个数
c_num = len(contours)
# 等待键盘输入
k_value = cv2.waitKey(1) & 0xFF
# 显示原图
cv2.imshow(img_name, img)
# 实时显示要保存的特征图
if n_s_index_list:
# 将保存的轮廓放一块显示
temp = np.zeros(img.shape, np.uint8)
for i in n_s_index_list:
temp = cv2.drawContours(temp, contours, i, (255, 255, 255), -1)
save_temp = temp
cv2.imshow("save-contour", temp)
else:
cv2.imshow("save-contour", np.zeros(img.shape, np.uint8))
# 实时显示单个轮廓图片
if contours_img:
cv2.imshow("single-contour", contours_img[n_index])
else:
cv2.imshow("single-contour", np.zeros(img.shape, np.uint8))
# 读取后一张后一张
if k_value == ord('d'):
picture_path_index += 1
c_index = 0
contours_img.clear()
n_s_index_list.clear()
cv2.destroyAllWindows()
print("后一张")
# 保存图片V特征
elif k_value == ord('s'):
ret, binary = cv2.threshold(save_temp, 127, 255, cv2.THRESH_BINARY_INV)
loc = cv2.bitwise_or(img, binary)
if cv2.imwrite(os.path.join(save_path, img_name), loc):
print("保存V字特征轮廓+1")
# 查看一个轮廓的特征
elif k_value == ord('n'):
if c_index == c_num:
print("已显示全部轮廓")
c_index = 0
contours_img.clear()
n_s_index_list.clear()
cv2.destroyAllWindows()
continue
temp = np.zeros(img.shape, np.uint8)
contours_img.append(temp)
cv2.drawContours(contours_img[c_index], contours, c_index, (255, 255, 255), -1)
n_index = c_index
c_index += 1
# 记录要保存的轮廓索引
elif k_value == ord('m'):
# 添加保存轮廓的在contours的索引
n_s_index_list.append(n_index)
print("记录当前轮廓")
# 按ESC退出
elif k_value == 27:
break
cv2.destroyAllWindows()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于python+opencv的连铸漏钢感兴趣区域快速识别与标记源码+超详细注释(阈值分割、形态学操作、特征提取等).zip 基于python+opencv的连铸漏钢感兴趣区域快速识别与标记源码+超详细注释(阈值分割、形态学操作、特征提取等).zip 基于python+opencv的连铸漏钢感兴趣区域快速识别与标记源码+超详细注释(阈值分割、形态学操作、特征提取等).zip 【资源说明】 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到94.5分,放心下载使用! 该资源适合计算机相关专业(如人工智能、通信工程、自动化、软件工程等)的在校学生、老师或者企业员工下载,适合小白学习或者实际项目借鉴参考! 当然也可作为毕业设计、课程设计、课程作业、项目初期立项演示等。如果基础还行,可以在此代码基础之上做改动以实现更多功能。
资源推荐
资源详情
资源评论
收起资源包目录
基于python+opencv的连铸漏钢感兴趣区域快速识别与标记源码+超详细注释(阈值分割、形态学操作、特征提取等).zip (199个子文件)
lena512.bmp 257KB
Breakout-Identification.iml 408B
lenacolor.jpg 43KB
lena512.pgm 256KB
lena.png 315KB
18.png 176KB
16.png 171KB
43.png 169KB
43.png 169KB
17.png 168KB
5.png 166KB
5.png 166KB
3.png 166KB
9.png 165KB
9.png 165KB
8.png 165KB
8.png 165KB
23.png 162KB
23.png 162KB
21.png 161KB
21.png 161KB
16.png 161KB
16.png 161KB
4.png 161KB
4.png 161KB
45.png 161KB
45.png 161KB
2.png 159KB
2.png 159KB
50.png 159KB
50.png 159KB
13.png 156KB
13.png 156KB
26.png 156KB
26.png 156KB
13.png 155KB
9.png 153KB
47.png 153KB
47.png 153KB
19.png 152KB
44.png 152KB
29.png 152KB
29.png 152KB
31.png 152KB
31.png 152KB
3.png 151KB
3.png 151KB
14.png 151KB
14.png 151KB
35.png 151KB
4.png 150KB
15.png 150KB
15.png 150KB
60.png 150KB
60.png 150KB
45.png 148KB
30.png 148KB
30.png 148KB
17.png 147KB
17.png 147KB
25.png 147KB
25.png 147KB
57.png 147KB
57.png 147KB
28.png 147KB
39.png 146KB
39.png 146KB
19.png 146KB
19.png 146KB
42.png 146KB
42.png 146KB
47.png 146KB
12.png 146KB
12.png 146KB
2.png 146KB
51.png 146KB
51.png 146KB
24.png 145KB
54.png 145KB
10.png 143KB
10.png 143KB
14.png 143KB
6.png 143KB
6.png 143KB
8.png 143KB
52.png 143KB
28.png 142KB
28.png 142KB
1.png 141KB
24.png 141KB
24.png 141KB
5.png 140KB
22.png 140KB
22.png 140KB
35.png 140KB
35.png 140KB
46.png 140KB
46.png 139KB
46.png 139KB
7.png 139KB
共 199 条
- 1
- 2
资源评论
Make程序设计
- 粉丝: 5627
- 资源: 3567
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功