根据点击图片上显示的类别进行归类
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time :2024/3/1 10:38
# @Author :weiz
# @ProjectName :jiangling_new
# @File :displayAndSelectImage.py
# @Description :
import os
import cv2
import copy
def OnMouseAction(event, x, y, flags, param):
"""
鼠标的回调函数,处理鼠标事件
:param event:
:param x:
:param y:
:param flags:
:param param:
:return:
"""
if event == cv2.EVENT_LBUTTONDOWN:
global g_click_points
g_click_points.append([x, y])
elif event == cv2.EVENT_RBUTTONDOWN:
print("右键点击")
elif flags == cv2.EVENT_FLAG_LBUTTON:
# print("左鍵拖曳")
pass
elif event == cv2.EVENT_MBUTTONDOWN:
print("中键点击")
def showEng4table_cv(img, engList, fontScale=None, gap=None, start_x=None, start_y=None):
"""
以表格的形式在图片的居中(默认是居中)显示英文字符(opencv不支持中文)
:param img:
:param engList:
:param fontScale:
:param gap:表格和字体之间的距离
:param start_x:
:param start_y:
:return:
"""
row = len(engList)
if gap == None:
gap = 2
if fontScale == None:
fontScale = 1
longestEng = ""
for engStr in engList:
if len(engStr) > len(longestEng):
longestEng = engStr
img_h, img_w = img.shape[:2]
(font_w, font_h), baseline = cv2.getTextSize(longestEng, cv2.FONT_HERSHEY_TRIPLEX, fontScale, 1)
font_h = font_h + baseline + 2 * gap
font_w = font_w + 2 * gap
if start_x == None:
start_x = (img_w - font_w) // 2
if start_y == None:
start_y = (img_h - (font_h * row)) // 2
end_x = start_x + font_w
end_y = start_y + font_h * row
rect_points = []
for i, engStr in enumerate(engList):
line_spacing = i * font_h
cv2.rectangle(img, (start_x, start_y + line_spacing), (end_x, start_y + line_spacing + font_h), (0, 0, 0))
cv2.putText(img, engStr, (start_x + gap, start_y + line_spacing + font_h - baseline - gap),
cv2.FONT_HERSHEY_TRIPLEX, fontScale, (188, 188, 188), 1)
rect_points.append([start_x, start_y + line_spacing, end_x, start_y + line_spacing + font_h])
return img, rect_points
def get_click_points(img):
"""
:param img:
:return:
"""
global g_click_points
g_click_points = []
cv2.namedWindow("click_image")
cv2.imshow("click_image", img)
cv2.setMouseCallback("click_image", OnMouseAction)
while g_click_points == []:
cv2.waitKey(50)
cv2.destroyWindow("click_image")
return g_click_points
def main():
imageNameList = os.listdir(g_imagePath)
i = 0
while i < len(imageNameList[0:3]):
img = cv2.imread(os.path.join(g_imagePath, imageNameList[i]))
showImg, rect_points = showEng4table_cv(copy.deepcopy(img), g_showLabel, 0.8, 10, 20, 20)
click_point = get_click_points(showImg)
if len(click_point) != 1:
print("无效点,请从新点击!")
continue
efficient_point_index = -1
for ii, rect_point in enumerate(rect_points):
if (rect_point[0] < click_point[0][0] < rect_point[2]) and (rect_point[1] < click_point[0][1] < rect_point[3]):
efficient_point_index = ii
break
if efficient_point_index > -1:
savePathTmp = os.path.join(g_saveImagePath, g_showLabel[efficient_point_index])
if not os.path.exists(savePathTmp):
print("{} 该路径不存在,创建中...".format(savePathTmp), end="")
if not os.mkdir(savePathTmp):
print("创建完成")
savePathTmp = os.path.join(savePathTmp, imageNameList[i])
cv2.imwrite(savePathTmp, img)
print("图片归为 {} 类".format(g_showLabel[efficient_point_index]))
i = i + 1
else:
print("点击无效,请重新选择类别!")
g_click_points = []
g_showLabel = ["ArmrestEpidermis01", "ArmrestEpidermis02", "ArmrestEpidermis03", "ArmrestEpidermis04", "ArmrestEpidermis05",
"ArmrestEpidermis06", "ArmrestEpidermis07", "ArmrestEpidermis08", "ArmrestEpidermis09", "ArmrestEpidermis10"]
g_saveImagePath = r"C:\Users\weiz\Desktop\atmosphe_skin"
g_imagePath = r"C:\Users\weiz\Desktop\armrest_skin"
if __name__ == "__main__":
main()
信息全在代码里,运行效果。
1、运行界面:(输入的图片是什么,就显示什么)
2、归类:鼠标左击上图左侧显示的类别。例如,点击后左侧的第三行,会出现的一张图片并有对应的输出,这里的效果如下。
3、一直按照第二步操作即可完成图片归类。
原文地址:https://blog.csdn.net/qq_31112205/article/details/136392093
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!