# -*- coding: utf-8 -*- """ Created on Wed May 22 14:42:41 2019 @author: Administrator """ # -*- coding: utf-8 -*- """ Created on Wed May 22 13:07:41 2019 @author: Administrator """ import cv2 from PIL import Image as IM import matplotlib.pyplot as plt import numpy as np img = cv2.imread('data/data/train_data/0AX7DFRB.jpg') img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) img_gray = cv2.equalizeHist(img_gray) #img_gray[img_gray>200]=255 #IM.fromarray(img_gray).show() #hist = np.histogram(img_gray[100,:],bins=256) Slide_range=5 Threshold=190 img_gray = cv2.copyMakeBorder(img_gray,Slide_range,Slide_range,Slide_range,Slide_range,cv2.BORDER_CONSTANT,value=255) img = cv2.copyMakeBorder(img,Slide_range,Slide_range,Slide_range,Slide_range,cv2.BORDER_CONSTANT,value=255) #IM.fromarray(img_gray).show() l = img_gray[img_gray.shape[0]//2,:].copy().astype(np.int16) #print(l[3]-l[4]) for i in range(0,len(l)-1): if abs(l[i]-l[i+1])<10: l[i]=255 if i == len(l)-2: l[i+1]=255# -*- coding: utf-8 -*- """ Created on Wed May 22 13:07:41 2019 @author: Administrator """ import cv2 from PIL import Image as IM import matplotlib.pyplot as plt import numpy as np img = cv2.imread('data/data/train_data/0BRO7XVG.jpg') img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) img_gray = cv2.equalizeHist(img_gray) #img_gray[img_gray>200]=255 #IM.fromarray(img_gray).show() #hist = np.histogram(img_gray[100,:],bins=256) Slide_range=5 Threshold=250 img_gray = cv2.copyMakeBorder(img_gray,Slide_range,Slide_range,Slide_range,Slide_range,cv2.BORDER_CONSTANT,value=255) img = cv2.copyMakeBorder(img,Slide_range,Slide_range,Slide_range,Slide_range,cv2.BORDER_CONSTANT,value=255) #IM.fromarray(img_gray).show() l = img_gray[img_gray.shape[0]//2,:].copy().astype(np.int16) hhh = np.mean(img_gray,axis=0) xxx = np.mean(img_gray,axis=1) #print(l[3]-l[4]) for i in range(0,len(hhh)-1): if abs(hhh[i]-hhh[i+1])<10: hhh[i]=255 if i == len(l)-2: hhh[i+1]=255 #print(np.mean(ly[i-5:i+5])) for i in range(Slide_range,len(hhh)-Slide_range): hhh[i]=np.mean(hhh[i-Slide_range:i+Slide_range]) if hhh[i]>Threshold: hhh[i]=255 ly = img_gray[:,img_gray.shape[1]//2+20].copy().astype(np.int16) for i in range(0,len(xxx)-1): if abs(xxx[i]-xxx[i+1])<10: xxx[i]=255 if i == len(ly)-2: xxx[i+1]=255 for i in range(Slide_range,len(xxx)-Slide_range): xxx[i]=np.mean(xxx[i-Slide_range:i+Slide_range]) if xxx[i]>Threshold: xxx[i]=255 #for i in range(0,len(l)-1): # if abs(l[i]-l[i+1])<100: # l[i]=255 #print(np.where(hhh[0]!=255)) #hhh = np.squeeze(hhh,-1) xmin = np.where(hhh!=255)[0][0] xmax = np.where(hhh!=255)[-1][-1] ymin = np.where(xxx!=255)[0][0] ymax = np.where(xxx!=255)[-1][-1] img = cv2.rectangle(img,(xmin,ymin),(xmax,ymax),(255,0,0),3) plt.plot(hhh) plt.plot(xxx) plt.show() #plt.figure() plt.imshow(img,cmap='gray') plt.show()