Skip to content

Instantly share code, notes, and snippets.

@andreemidio
Created July 19, 2019 16:47
Show Gist options
  • Select an option

  • Save andreemidio/ab09d7b1b25fdac6f4c35dfa6817562d to your computer and use it in GitHub Desktop.

Select an option

Save andreemidio/ab09d7b1b25fdac6f4c35dfa6817562d to your computer and use it in GitHub Desktop.
from __future__ import print_function
import cv2
import numpy as np
import align_images as align
import findGabarito as gabarito
import contornosInternos
import bubble as bubble
import imutils
from imutils.perspective import four_point_transform
from imutils import contours
import bitwiseImage as bt
#importação da imagem
#im = 'ImagensTeste/photo_2019-07-04_13-33-01.jpg'
#im = 'ImagensTeste/photo_2019-07-12_16-43-47.jpg'
#im = 'ImagensTeste/photo_2019-07-12_16-43-47.jpg'
#im = 'template/template.png'
im = 'template/templatePreenchido.png'
#im = 'ImagensTeste/photo_2019-07-12_16-43-47.jpg'
#alinhamento da imagem
imagemAlinhada = align.alignImages(im)
cv2.imwrite('processadas/processadaAlinhada.png', imagemAlinhada)
#imagemAlinhada = cv2.addWeighted(imagemAlinhada, 0.9, np.zeros(imagemAlinhada.shape, imagemAlinhada.dtype), 2, 2)
gabarito = gabarito.findGabarito(imagemAlinhada)
dim = (1595,556)
gabarito = cv2.resize(gabarito, dim, interpolation=cv2.INTER_CUBIC )
cv2.imwrite('processadas/gabarito.png', gabarito)
gabaritoInteresse = cv2.imread('processadas/gabarito.png')
bolhas = bubble.bolhas(gabaritoInteresse)
#gabaritoInteresse = cv2.addWeighted(gabaritoInteresse, 1.09, np.zeros(gabaritoInteresse.shape, gabaritoInteresse.dtype),0,0)
gabaritoInteresse = cv2.cvtColor(gabaritoInteresse,cv2.COLOR_BGR2GRAY)
imageBT = bt.bitwise(gabaritoInteresse)
print(imageBT.shape)
cv2.imshow("imageBT",imageBT)
blurred = cv2.GaussianBlur(gabaritoInteresse, (17,17),0)
#cv2.imshow("dilation",dilation)
thresh = cv2.adaptiveThreshold(gabaritoInteresse, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
edged = cv2.Canny(blurred, 100,200)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
heirarchy = cnts[1][0]
cnts = cnts[0] if imutils.is_cv4() else cnts[1]
#M = cv2.moments(cnts)
#print(M)
questions = []
imageBT = bt.bitwise(gabaritoInteresse)
cv2.imshow("imageBT",imageBT)
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
if (w >= 20 and h >= 20) and (w <= 25 and h <= 25) and ar >= 0.7 and ar <= 1.3:
box = [(x//5)*5, y]
#box = [x+w/2, y+h/2, w/2]
questions.append([c, box])
#print(x, y)
cv2.rectangle(gabarito, (x, y), (x+w, y+h), (255, 0, 0), 1)
questions = sorted(questions, key=lambda q: q[1][1])
print(len(box))
questionCnts = []
'''
Agora estamos classificando da esquerda para a direita tomando um lote de 30 contornos
que são basicamente uma linha inteira e, em seguida, classificá-los a partir da ordem crescente de x
'''
boxes = []
for i in np.arange(0, len(questions), 30):
# take a row of bubbles
q = list(questions[i: i+30])
#print(q)
for o in q:
boxes.append(o[1])
# append each contour sorted from left to right in a row
# sort them using x
q = sorted(q, key=lambda k: k[1][0])
for o in q:
# append each contour sorted from left to right in a row
#questionCnts.append(o[0])
questionCnts.append(o[0])
# each question has 5 possible answers, to loop over the
# question in batches of 5
#matrizRespostas = np.empty((0,4))
#print(matrizRespostas.shape)
respostas = []
questao = []
letra = []
for (q, i) in enumerate(np.arange(0, len(questionCnts), 30)):
# calculate the old question no
row = q // 5
col = q % 5
old_question_no = col + row
#print(q)
#print(i)
cnts = contours.sort_contours(questionCnts[i:i+5])[0]
#cnts = cnts[0:4:5]
for (l ,k )in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(k)
if (w >= 20 and h >= 20) and (w <= 25 and h <= 25) and ar >= 0.7 and ar <= 1.3:
box = [(x//5)*5, y]
#print(x, y)
respostas.append(l)
#rect = np.array(cv2.boundingRect(k)).reshape(1,4)
#print(len(rect.shape))
#matrizRespostas = np.append(matrizRespostas,rect,0)
cv2.rectangle(bolhas, (x, y), (x+w, y+h), (0, 0, 255), 1)
#for o in respostas:
#print(o)
# questao.append(o)
# letra.append(o)
#print(questao[15]//5)
#print(letra[15]%5)
#print(type(o))
#print(len(boxes))
#print(len(questionCnts))
#print(len(questions))
#print(len(bolhas))
cv2.imshow("Bolhas",bolhas)
#cv2.imshow("Imagem Alinhada",imagemAlinhada)
#cv2.imshow("Gabarito",gabarito)
cv2.waitKey(0)
cv2.destroyAllWindows()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment