1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # coding=utf-8
- import sys
- reload(sys)
- sys.setdefaultencoding('utf8')
- import os
- import numpy as np
- import cv2
- import time
- from log import *
- from skimage import measure, color
- def get_image_box(img,lower_color,upper_color,median=3):
- img_hsv = cv2.inRange(cv2.cvtColor(img,cv2.COLOR_BGR2HSV), lower_color, upper_color)
- # img_hsv=cv2.medianBlur(img_hsv, median)
- # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (median, median))
- # img_hsv = cv2.dilate(img_hsv, kernel)
- # img_hsv = cv2.erode(img_hsv, kernel)
- min_length=15
- edgeThresh =70
- #img2 = cv2.Canny(img_hsv,edgeThresh,edgeThresh*3,apertureSize =3)
- # if lower_color[2]==0:
- # cv2.imwrite(r"e:\b2\canny.jpg",img2)
-
- binary , contours, hierarchy= cv2.findContours(img_hsv,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
- max_rect=None
- cv2.drawContours(img,contours,-1,(0,0,255),1)
-
- return max_rect
-
- def is_in_rect(rect_small,rect_large):
- 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]
- def get_scale_rect(img,deep_height=300):
- height=img.shape[0]
- width=img.shape[1]
- print("width:",width,", height:",height)
- #color
- color_tuples=[
- #white
- (np.array([0, 0, 221]) ,np.array([180, 30, 255]),5)
- #yellow
- #(np.array([26, 43, 46]) ,np.array([34, 255, 255]),5)
- ]
- bbox=None
- for color_tuple in color_tuples:
- bbox=get_image_box(img,color_tuple[0],color_tuple[1],color_tuple[2])
- return bbox
- def get_scale(img):
- scale=get_scale_rect(img)
- return 0 if scale is None else scale[2]-scale[0]
- def test(index):
- jpg_file=unicode(r"E:\浙江需求\images\\"+str(index)+".JPG")
- imgcolor=cv2.imdecode(np.fromfile(jpg_file, dtype=np.uint8),1)
- imgcolor=cv2.GaussianBlur(imgcolor,(5,5),0)
- maxrect=get_scale_rect(imgcolor)
- height=imgcolor.shape[0]
- width=imgcolor.shape[1]
- # if maxrect:
- # cv2.line(imgcolor, (maxrect[0], maxrect[3]+maxrect[4]), (maxrect[2], maxrect[3]+maxrect[4]), (255, 0, 0), 5)
- cv2.namedWindow("enhanced",0)
- cv2.resizeWindow("enhanced",width/2,height/2)
- cv2.imshow("enhanced",imgcolor)
- cv2.waitKey(0)
- if __name__ == '__main__':
- nums=[1,2,3,4]
- for i in nums:
- test(i)
- sys.exit()
- start = time.time()
- log_info("dirameter start...")
- if len(sys.argv)<2:
- log_error("missing parameter")
- sys.exit()
- jpg_file=sys.argv[1].decode('gbk');
- img=cv2.imdecode(np.fromfile(jpg_file, dtype=np.uint8), 1)
- log_info("jpg_file:"+jpg_file)
- length=get_scale(img)
- log_result(length)
- log_info("{0} scale length finished, result:{1} consume:{2}".format(jpg_file,length,str(time.time()-start)))
-
-
|