add scribble image processing
This commit is contained in:
109
markup/img.py
Normal file
109
markup/img.py
Normal file
@ -0,0 +1,109 @@
|
||||
#from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import sys, os.path, re, json, pickle, subprocess
|
||||
import shutil
|
||||
|
||||
from PIL import Image, ImageFilter
|
||||
import numpy
|
||||
import imutils
|
||||
import cv2
|
||||
import dumper
|
||||
import random as rng
|
||||
from pathlib import Path
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
WORKDIR = os.path.join(settings.ASSET_DIR, 'markup', 'work')
|
||||
|
||||
# https://www.pyimagesearch.com/2014/10/20/finding-shapes-images-using-python-opencv/
|
||||
|
||||
|
||||
def find_shapes(image_path):
|
||||
path = Path(image_path)
|
||||
|
||||
img = Image.open(image_path, 'r')
|
||||
if not img.mode in ('RGBA', 'LA'):
|
||||
print('no alpha channel: {}'.format(img.mode))
|
||||
return None
|
||||
|
||||
alpha_layer = img.convert('RGBA').split()[-1]
|
||||
|
||||
alpha_layer = alpha_layer.filter(ImageFilter.GaussianBlur(5))
|
||||
|
||||
threshold = 5
|
||||
alpha_layer = alpha_layer.point(lambda p: p > threshold and 255)
|
||||
|
||||
threshold = numpy.array(alpha_layer)
|
||||
|
||||
#bnw = Image.new('RGBA', img.size, (255, 255, 255))
|
||||
#src = cv2.imread(image_path)
|
||||
|
||||
# TODO just get alpha, 1 bit
|
||||
#threshold = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
#threshold = cv2.blur(threshold, (4,4))
|
||||
|
||||
thresh_path = str(path.with_suffix('.thresh.png'))
|
||||
print('write to', thresh_path)
|
||||
cv2.imwrite(thresh_path, threshold)
|
||||
|
||||
# find all the 'not black' shapes in the image
|
||||
# upper = np.array([255, 255, 255])
|
||||
# lower = np.array([15, 15, 15])
|
||||
# shapeMask = cv2.inRange(image, lower, upper)
|
||||
|
||||
# # alternate to below:
|
||||
# #imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# #ret, thresh = cv2.threshold(imgray, 127, 255, 0)
|
||||
# #im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
# #cv2.imshow('Thresh', thresh)
|
||||
# #cv2.waitKey(0)
|
||||
|
||||
#threshold = 100
|
||||
#canny_output = cv2.Canny(threshold, threshold, threshold * 2)
|
||||
#contours = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
#print("contours: {}".format(contours))
|
||||
|
||||
# find the contours in the mask
|
||||
#contours = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
#contours = cv2.findContours(shapeMask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||
contours = imutils.grab_contours(contours)
|
||||
print("{} shapes".format(len(contours)))
|
||||
#cv2.imshow("Mask", shapeMask)
|
||||
|
||||
# Find the convex hull object for each contour
|
||||
hull_list = []
|
||||
#for i in range(len(contours)):
|
||||
for c in contours:
|
||||
hull = cv2.convexHull(c)
|
||||
hull_list.append(hull)
|
||||
|
||||
# Draw contours + hull results
|
||||
#contour_image = shapeMask.copy() # np.zeros((shapeMask.shape[0], shapeMask.shape[1], 3), dtype=numpy.uint8)
|
||||
contour_image = numpy.zeros((threshold.shape[0], threshold.shape[1], 3), dtype=numpy.uint8)
|
||||
for i in range(len(contours)):
|
||||
# compute the center of the contour
|
||||
M = cv2.moments(contours[i])
|
||||
if M["m00"] == 0: M["m00"] = 0.00001
|
||||
cX = int(M["m10"] / M["m00"])
|
||||
cY = int(M["m01"] / M["m00"])
|
||||
color = (rng.randint(0,512), rng.randint(0,512), rng.randint(0,512))
|
||||
cv2.drawContours(contour_image, contours, i, color)
|
||||
#cv2.drawContours(contour_image, hull_list, i, color)
|
||||
x, y, w, h = cv2.boundingRect(contours[i])
|
||||
cv2.rectangle(contour_image, (x,y), (x+w,y+h), color, 1)
|
||||
cv2.circle(contour_image, (cX, cY), 2, color, -1)
|
||||
# cv2.putText(contour_image, "center", (cX - 20, cY - 15),
|
||||
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
|
||||
|
||||
contour_path = str(path.with_suffix('.contour.png'))
|
||||
print('write to', contour_path)
|
||||
cv2.imwrite(contour_path, contour_image)
|
||||
|
||||
# for c in contours:
|
||||
# #print json.dumps(c)
|
||||
# print(dumper.dump(c))
|
||||
# # draw the contour and show it
|
||||
# # cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
|
||||
# # cv2.imshow("Image", image)
|
||||
# # cv2.waitKey(0)
|
||||
Reference in New Issue
Block a user