Skip to content

Commit

Permalink
Working on contouring
Browse files Browse the repository at this point in the history
  • Loading branch information
rsullivan00 committed Apr 8, 2015
1 parent 1dbd4d7 commit a2e4897
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python/BLUR
python/HIST
python/CONTOUR
python/THRESHOLD
python/FINAL
python/THRESHOLDM
python/THRESHOLDG
python/GRAY
Expand Down
Binary file modified python/__pycache__/categories.cpython-34.pyc
Binary file not shown.
Binary file modified python/__pycache__/label.cpython-34.pyc
Binary file not shown.
50 changes: 28 additions & 22 deletions python/contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def contour(imagepath, invert=False):
im_x = len(im[0])
im_y = len(im)
im_size = im_x * im_y

imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
if invert:
imgray = invert_color(imgray)
Expand All @@ -146,47 +147,50 @@ def contour(imagepath, invert=False):
centroid = (int(im_x/2), int(im_y/2))

for x,j in enumerate(large_contours):
name = fileNameContour + "-" + str(x) + ".jpg"
name = fileNameContour.replace('.jpg', '-' + str(x) + '.jpg')
cv2.drawContours(im, [j], -1, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), -5)
cv2.circle(im, centroid, 20, (0, 0, 0), thickness=-1)
cv2.imwrite(name, im)
im = cv2.imread(imagepath)

# Is this actually useful?
# possible_label_contours = contour_filter_centroid(large_contours, im_size/2000, centroid)

# This should only return a single contour...and will supercede any other filtering. Should we only use this type of filtering?
# Filter to contours containing central point
label_contours = contour_filter_center(large_contours, centroid)

if len(label_contours) == 0:
return False

label_contour = label_contours[0]

# Draw contour on original image
cv2.drawContours(im, [label_contour], -1, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), -5)
cv2.imwrite(fileNameContour, im)

epsilon = 50
contour_corners = cv2.approxPolyDP(label_contour, epsilon, True)
label_corners = None
label_contour = None
# Iteratively apply approximations to try to achieve a rectangle.
for i in range(2):
if len(contour_corners) == 4:
break
for l_c in label_contours:
contour_corners = cv2.approxPolyDP(l_c, epsilon, True)
if len(contour_corners) == 4:
label_corners = contour_corners
label_contour = l_c
break
epsilon *= 2
contour_corners = cv2.approxPolyDP(label_contour, epsilon, True)

# If we did not find a rectangle, fail.
if len(contour_corners) != 4:
print("Contour not rectangular, but of size %s" % len(contour_corners))
if label_contour is None:
print('No rectangular contour found')
return False

# If we did not find a rectangle, fail.
# if len(label_corners) != 4:
# print("Contour not rectangular, but of size %s" % len(contour_corners))
# return False

# Arrange corners in clockwise order
contour_corners = corners(contour_corners)
label_corners = corners(label_corners)

# Draw contour on original image
cv2.drawContours(im, [label_contour], -1, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), -5)
cv2.imwrite(fileNameContour, im)

# Find minimum containing (rotated) rectangle and relevant info
# Find minimum containing (rotated) rectangle to set up transformation image
min_rect = cv2.minAreaRect(label_contour)
center = min_rect[0]
# center = min_rect[0] # Not necessary?
size = (int(min_rect[1][0]), int(min_rect[1][1]))
angle = min_rect[2]

Expand All @@ -196,10 +200,12 @@ def contour(imagepath, invert=False):
size = (size[1], size[0])

new_corners = corners(np.array([[0,0], [size[0],0], [size[0],size[1]], [0,size[1]]], np.float32))
M = cv2.getPerspectiveTransform(np.array(contour_corners, np.float32), new_corners)
M = cv2.getPerspectiveTransform(np.array(label_corners, np.float32), new_corners)
label_im = cv2.warpPerspective(imageToBeCut, M, dsize=size)

ret,thresh = cv2.threshold(label_im, 125, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# label_im = cv2.medianBlur(label_im,3)
# thresh = cv2.adaptiveThreshold(label_im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 5)
cv2.equalizeHist(thresh, thresh)
cv2.imwrite(fileNameFinal, thresh)
return thresh
Expand Down
6 changes: 6 additions & 0 deletions python/ideas.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Meeting April 7th:
1)In the case where we have multiple center contours returned, choose more intelligently by Choosing a contour that easily approximates to a rectangle
2)Save final transformed image or print the approximation rectangle
3)Current solution does a good job of finding contour, but we should take that area from the ORIGINAL image, and process that for final label extraction.

Ideas April 5th:
Problem cases:
1) Inverted color schemes
2) Faded background, trying sharpening (ask Nate)
Expand Down
56 changes: 23 additions & 33 deletions python/temp.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
Nutrition Facts
fi A

Sewing Size 1/2 cup (1269)
Servings Per Carton About 26
Serving Size 11 chips (289)











knot-rt Per Serving
m
% Daily Value“

WW7;
WM
Trans Fat 09

Cholesterol 0mg 0%

Sodium 280mg 1 2%

Tom Carbohydrate 89 3%
Dietary Fiber 29 8%
Sugars 59



h
Vitamin A 6% - Vitamin C 25%
Calcium 4% 0 iron 2%








fzxmma Lam than 300mg 300W;
,l/ijn Lissa than 2,400mg 2300"“;
Iggflmflmrkme “Wk; 3759
Servings Per Container 40
Amountliorsonrlng
Calorles 140 Caioriw from Fat 60
96IhflyVIluo*
Total Fat 7g 11 94.
Saturated Fat 1g 5%
Trans Fat 09 A
Cholesterol 0mg 0%
Sodium 100mg 4%
frotal carbohydrate 19g 6%
%):[)~ietary Fiber 1g 4%
Slgars Og
proteliiiég
T
Vmmnn A

V 0% 0 Vitaminc 0%
:‘”‘l|lJrii 4‘?/Q. 0 lrnn

0 comments on commit a2e4897

Please sign in to comment.