• python读取图像小工具


    一、和图像交互获得图像的坐标和像素值

    1. import cv2
    2. import numpy as np
    3. import signal
    4. import threading
    5. import time
    6. if __name__ == '__main__':
    7. img = cv2.imread('XXX',0)#读取图片
    8. font_face,font_scale,thickness=cv2.FONT_HERSHEY_SIMPLEX,0.5,1
    9. #鼠标交互
    10. def mouseHandler(event,x,y,flags,param):
    11. points = (x,y)
    12. global imgCopy
    13. #鼠标左键双击事件
    14. if event == cv2.EVENT_LBUTTONDBLCLK:
    15. #拷贝一张与原图像格式相同的新图像
    16. imgCopy = img.copy()
    17. #拼接文字
    18. text = '['+str(x)+','+str(y)+']'+str(img[y,x])
    19. print(text)
    20. #读取文字(宽,高),下基线
    21. (t_w,t_h),baseLine = cv2.getTextSize(text,font_face,font_scale,thickness)
    22. #在鼠标当前位置的左上角显示文字
    23. cv2.putText(imgCopy,text,(x-t_w,y),font_face,font_scale,(125,125,125))
    24. cv2.imshow('win',imgCopy)
    25. #鼠标移动事件
    26. elif event == cv2.EVENT_MOUSEMOVE:
    27. #显示原图片能使文本框消失
    28. cv2.imshow('win',img)
    29. cv2.namedWindow('win')
    30. #窗口与回调函数绑定
    31. cv2.setMouseCallback('win',mouseHandler)
    32. cv2.imshow('win',img)
    33. cv2.waitKey()

    二、二值化图像

    1. import cv2
    2. import numpy as np
    3. import signal
    4. import threading
    5. import time
    6. if __name__ == '__main__':
    7. img = cv2.imread('path',0)#读取图片
    8. ret, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    9. print("threshold value %s" % ret) #打印阈值,超过阈值显示为白色,低于该阈值显示为黑色
    10. cv2.imshow("threshold", binary) #显示二值化图像
    11. cv2.waitKey(0)
    12. cv2.destroyAllWindows()

    批量图像二值化

    1. import cv2
    2. import numpy as np
    3. import signal
    4. import threading
    5. import time
    6. import os
    7. import sys
    8. import random
    9. import datetime
    10. import argparse
    11. def get_files(path):
    12. files = []
    13. for filename in os.listdir(path):
    14. if os.path.isfile(os.path.join(path, filename)):
    15. files.append(filename)
    16. return files
    17. if __name__ == '__main__':
    18. files_path="XXX"
    19. #print(files_path)
    20. image_files = get_files(files_path)
    21. i=1
    22. #print(image_files)
    23. for image_file in image_files:
    24. image_path=os.path.join(files_path , image_file)
    25. print(image_path)
    26. img = cv2.imread(image_path,0)#读取图片
    27. start_time_init = time.perf_counter()
    28. ret, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    29. end_time_init = time.perf_counter()
    30. elapsed_time_init = (end_time_init - start_time_init)*1000
    31. print("二值化时间: {} ms".format(elapsed_time_init))
    32. print("threshold value %s" % ret) #打印阈值,超过阈值显示为白色,低于该阈值显示为黑色s
    33. cv2.imwrite(files_path+"/binary/"+str(i)+".png",binary)
    34. i=i+1

    三、区域合并提取最大连通域

    1. import cv2
    2. import numpy as np
    3. import signal
    4. import threading
    5. import time
    6. import os
    7. import sys
    8. import random
    9. import datetime
    10. import argparse
    11. def get_files(path):
    12. files = []
    13. for filename in os.listdir(path):
    14. if os.path.isfile(os.path.join(path, filename)):
    15. files.append(filename)
    16. return files
    17. if __name__ == '__main__':
    18. #files_path="/home/robot/PaddleOCR-2.6.0/data/OK0828/raw_data/"
    19. files_path="/home/robot/PaddleOCR-2.6.0/data/829/"
    20. files_path="/home/robot/PaddleOCR-2.6.0/data/NG0823/"
    21. #print(files_path)
    22. image_files = get_files(files_path)
    23. i=1
    24. #print(image_files)
    25. for image_file in image_files:
    26. image_path=os.path.join(files_path , image_file)
    27. print(image_path)
    28. img = cv2.imread(image_path,0)#读取图片
    29. start_time_init = time.perf_counter()
    30. ret, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    31. end_time_init = time.perf_counter()
    32. elapsed_time_init = (end_time_init - start_time_init)*1000
    33. print("二值化时间: {} ms".format(elapsed_time_init))
    34. print("threshold value %s" % ret) #打印阈值,超过阈值显示为白色,低于该阈值显示为黑色s
    35. cv2.imwrite(files_path+"/binary/"+str(i)+".png",binary)
    36. i=i+1
    37. # cv2.imshow("threshold", binary) #显示二值化图像
    38. # cv2.waitKey(0)
    39. # cv2.destroyAllWindows()
    40. start_time = time.perf_counter()
    41. num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary)
    42. end_time = time.perf_counter()
    43. elapsed_time = (end_time - start_time)*1000
    44. print("连通域的时间: {} ms".format(elapsed_time))
    45. max_area=0
    46. j=0
    47. for st in stats[1:]:
    48. j=j+1
    49. area=st[4]
    50. if(max_area
    51. max_area=area
    52. index=j
    53. print("index",index)
    54. print("max_area",max_area)
    55. #index=index+1
    56. print('num_labels: ', num_labels)
    57. labels[labels>0] = 255
    58. labels = labels.astype(np.uint8)
    59. # #将一维灰度图像扩展到三维
    60. labels= np.expand_dims(labels,axis=2).repeat(3,axis=2).astype(np.uint8)
    61. # for st in stats[1:]:
    62. cv2.rectangle(labels, (stats[index][0], stats[index][1]), (stats[index][0]+stats[index][2], stats[index][1]+stats[index][3]), (0, 255, 0), 3)
    63. #cv2.imshow('labels', labels)
    64. #cv2.waitKey(0)
    65. cv2.imwrite(files_path+"/labels/"+str(i)+".png",labels)

  • 相关阅读:
    hdfs笔记
    STP---生成树协议
    二十三种设计模式全面解析-解密迭代器模式:探索遍历之道
    crsctl stop crs -f 停止不成功 (SYSTEMDG in QUIESCING state)
    【代码随想录】算法训练计划15
    如何提高网站权重
    陪诊系统|陪诊软件开发|陪诊系统搭建功能
    C++之常用的排序算法
    python安装的第三方库
    Node.js安装环境环境配置,以及yarn安装以及踩坑之路
  • 原文地址:https://blog.csdn.net/YOULANSHENGMENG/article/details/132586732