diameter.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # coding=utf-8
  4. import sys
  5. reload(sys)
  6. sys.setdefaultencoding('utf8')
  7. import os
  8. import numpy as np
  9. import cv2
  10. import time
  11. from log import *
  12. from skimage import measure, color
  13. def get_image_box(img,lower_color,upper_color,median=3):
  14. img_hsv = cv2.inRange(cv2.cvtColor(img,cv2.COLOR_BGR2HSV), lower_color, upper_color)
  15. # img_hsv=cv2.medianBlur(img_hsv, median)
  16. # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (median, median))
  17. # img_hsv = cv2.dilate(img_hsv, kernel)
  18. # img_hsv = cv2.erode(img_hsv, kernel)
  19. min_length=15
  20. edgeThresh =70
  21. #img2 = cv2.Canny(img_hsv,edgeThresh,edgeThresh*3,apertureSize =3)
  22. # if lower_color[2]==0:
  23. # cv2.imwrite(r"e:\b2\canny.jpg",img2)
  24. binary , contours, hierarchy= cv2.findContours(img_hsv,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  25. max_rect=None
  26. cv2.drawContours(img,contours,-1,(0,0,255),1)
  27. return max_rect
  28. def is_in_rect(rect_small,rect_large):
  29. return rect_small[0]>rect_large[0] and rect_small[0]+rect_small[2]<rect_large[0]+rect_large[2] and rect_small[1]>rect_large[1] and rect_small[1]+rect_small[3]<rect_large[1]+rect_large[3]
  30. def get_scale_rect(img,deep_height=300):
  31. height=img.shape[0]
  32. width=img.shape[1]
  33. print("width:",width,", height:",height)
  34. #color
  35. color_tuples=[
  36. #white
  37. (np.array([0, 0, 221]) ,np.array([180, 30, 255]),5)
  38. #yellow
  39. #(np.array([26, 43, 46]) ,np.array([34, 255, 255]),5)
  40. ]
  41. bbox=None
  42. for color_tuple in color_tuples:
  43. bbox=get_image_box(img,color_tuple[0],color_tuple[1],color_tuple[2])
  44. return bbox
  45. def get_scale(img):
  46. scale=get_scale_rect(img)
  47. return 0 if scale is None else scale[2]-scale[0]
  48. def test(index):
  49. jpg_file=unicode(r"E:\浙江需求\images\\"+str(index)+".JPG")
  50. imgcolor=cv2.imdecode(np.fromfile(jpg_file, dtype=np.uint8),1)
  51. imgcolor=cv2.GaussianBlur(imgcolor,(5,5),0)
  52. maxrect=get_scale_rect(imgcolor)
  53. height=imgcolor.shape[0]
  54. width=imgcolor.shape[1]
  55. # if maxrect:
  56. # cv2.line(imgcolor, (maxrect[0], maxrect[3]+maxrect[4]), (maxrect[2], maxrect[3]+maxrect[4]), (255, 0, 0), 5)
  57. cv2.namedWindow("enhanced",0)
  58. cv2.resizeWindow("enhanced",width/2,height/2)
  59. cv2.imshow("enhanced",imgcolor)
  60. cv2.waitKey(0)
  61. if __name__ == '__main__':
  62. nums=[1,2,3,4]
  63. for i in nums:
  64. test(i)
  65. sys.exit()
  66. start = time.time()
  67. log_info("dirameter start...")
  68. if len(sys.argv)<2:
  69. log_error("missing parameter")
  70. sys.exit()
  71. jpg_file=sys.argv[1].decode('gbk');
  72. img=cv2.imdecode(np.fromfile(jpg_file, dtype=np.uint8), 1)
  73. log_info("jpg_file:"+jpg_file)
  74. length=get_scale(img)
  75. log_result(length)
  76. log_info("{0} scale length finished, result:{1} consume:{2}".format(jpg_file,length,str(time.time()-start)))