Skip to content

Instantly share code, notes, and snippets.

@LieutenantChips
Created September 12, 2017 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LieutenantChips/8c27ef6be8f0d6c81ad6321fd92d59be to your computer and use it in GitHub Desktop.
Save LieutenantChips/8c27ef6be8f0d6c81ad6321fd92d59be to your computer and use it in GitHub Desktop.
import math
from scipy import ndimage as ndi
from skimage import feature
import numpy as np
import os
from scipy import ndimage as ndi
import cv2
import Queue
img = cv2.imread('1.jpeg')
gray = cv2.imrad('1.jpeg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
# Get rid of the tupple
# Gaussian thresholding
img = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5,2)
# Thresholding Otsu
ret, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# find all the 'black' shapes in the image
shapeMask = cv2.inRange(img, 200, 255)
# find the contours in the mask
(cnts, _)= cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
print "I found %d black shapes" % (len(cnts))
cv2.imshow("Mask", shapeMask)
img = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)
# loop over the contours
cv2.waitKey(0)
averageArea = 0
count = 0
list = Queue.Queue()
list_coordY = Queue.Queue()
list_coordX = Queue.Queue()
for c in cnts:
# draw the contour and show it
cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
leftMost = tuple(c[c[:,:,0].argmin()][0])
rightMost = tuple(c[c[:,:,0].argmax()][0])
topMost = tuple(c[c[:,:,1].argmin()][0])
bottomMost = tuple(c[c[:,:,1].argmax()][0])
y1 = topMost[1]
y2 = bottomMost[1]
x1 = leftMost[0]
x2 = rightMost[0]
print "\n %d:%d \n", topMost[1] ,bottomMost[1]
print "\n %d:%d \n", leftMost[0] ,rightMost[0]
radius1 = bottomMost[1] - topMost[1];
radius2 = rightMost[0] - leftMost[0];
crc1 = radius1 * radius1 * math.pi;
crc2 = radius2 * radius2 * math.pi;
calArea = cv2.contourArea(c);
if(crc1 != 0):
areaPerError1 = math.fabs((calArea - crc1)/crc1);
if(areaPerError1 > 0.2):
print "\n error 1 %d \n", areaPerError1;
if(crc2 != 0):
areaPerError2 = math.fabs((calArea - crc2)/crc2);
if(areaPerError2 > 0.2):
print "\n error 2 %d \n", areaPerError2;
#cv2.rectangle(img,(bottomMost[0], bottomMost[1]),(topMost[0],topMost[1]),(255,0,0),3)
if(y2 > y1 and x2 > x1):
crop = img[y1:y2 , x1:x2]
count += 1
averageArea += cv2.contourArea(c)
list.put(crop)
list_coordY.put((y1,y2))
list_coordX.put((x1,x2))
cv2.imshow("Image", img)
cv2.waitKey(0)
averageArea = averageArea / count
for i in range(0,list.qsize()):
crop_img = list.get()
orig_coordY = list_coordY.get()
orig_coordX = list_coordX.get()
x_list = crop_img.shape[0]
y_list = crop_img.shape[1]
print "\n reso : %d \n", x_list*y_list
percentError = ((averageArea - (x_list*y_list))/averageArea)*100
print "\n percent Error : %d\n", percentError
if(percentError > 50):
print "THIS IS CANCELED"
print "SHAPE : %d %d", crop_img.shape[1], crop_img.shape[0]
cv2.rectangle(crop_img,(0,0),(crop_img.shape[1], crop_img.shape[0]),(0,0,255),3)
# Only place corrected images into their spaces
else:
list.put(crop_img)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment