-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgSeg.py
56 lines (44 loc) · 1.73 KB
/
imgSeg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def imgSeg(path):
from urllib import request
import cv2
import numpy as np
# URL to image
resp = request.urlopen(path)
img = np.asarray(bytearray(resp.read()), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(gray, cv2.CV_8UC1)
sobel = cv2.Sobel(gray, cv2.CV_8UC1, 0, 1, ksize= 3)
# Remove some small noise if any.
dilate = cv2.dilate(sobel,None)
erode = cv2.erode(dilate,None)
ret,thresh1 = cv2.threshold(erode,127,255,cv2.THRESH_BINARY)
# Find contours with cv2.RETR_CCOMP
contours, hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2:]
# Get width, height of origin image
height, width, channels = img.shape
# Temp criteria of width
tempWidth = width - 100
count = 0
tempY = height
for i,cnt in enumerate(contours):
# Check if it is an external contour and its area is more than 100
if hierarchy[0,i,3] == -1 :
x,y,w,h = cv2.boundingRect(cnt)
if(w > tempWidth):
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
m = cv2.moments(cnt)
tempHeight = height -( height- tempY )
if( tempHeight -y > 100 ) :
count += 1
cv2.imwrite("output/Img"+str(count)+".jpg", img[y:tempHeight, 0:0+width])
tempY = y
if(tempY > 10):
count += 1
cv2.imwrite("output/Img"+str(count)+".jpg", img[0:tempY, 0:0+width])
print(count)
#cv2.imwrite("output/Img"+str(count)+".jpg", img[0:tempY, 0:0+width])
#cv2.imshow('img', temp)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
imgSeg('http://gi.esmplus.com/orgastore/img/hobak_total.jpg')