Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

remove_contours.py 1.36 KB
Newer Older
Hachemin Pierre-Yves's avatar
Hachemin Pierre-Yves committed
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
# import the necessary packages
import numpy as np
import cv2
 
def is_contour_bad(c):
	# approximate the contour
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, 0.02 * peri, True)
 
	# the contour is 'bad' if it is not a rectangle
	return not len(approx) == 4

	# load the shapes image, convert it to grayscale, and edge edges in
# the image
image = cv2.imread("Image1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 200, 600)
cv2.imshow("Original", image)
 
# find contours in the image and initialize the mask that will be
# used to remove the bad contours
(_,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.ones(image.shape[:2], dtype="uint8") * 255


# loop over the contours
for c in cnts:
	dictV={}
	dictH={}
	for pix in c:
		if pix[0][0] in dictH.keys():
			dictH[pix[0][0]]+=1
		else:
			dictH[pix[0][0]]=1
		if pix[0][1] in dictV.keys():
			dictV[pix[0][1]]+=1
		else:
			dictV[pix[0][1]]=1
	
	print("dictH {}".format(dictH))
	print("dictV {}".format(dictV))
	# if the contour is bad, draw it on the mask


	if is_contour_bad(c):
		cv2.drawContours(mask, [c], -1, 0, -1)
 
# remove the contours from the image and show the resulting images
image = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", image)
cv2.imwrite("Image1_After.jpg", image)
cv2.waitKey(0)