Last active
March 31, 2016 07:35
-
-
Save lucapiccinelli/30fcfeb8c0e8e3ca85fdbd2ab0319699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#rilettura dell'immagine, stavolta a colori | |
im_out = cv2.imread(r'img\barcodes.jpg') | |
#estrazione dei componenti connessi | |
contours, hierarchy = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | |
unscale = 1.0 / scale | |
if contours != None: | |
for contour in contours: | |
# se l'area non è grande a sufficienza la salto | |
if cv2.contourArea(contour) <= 2000: | |
continue | |
#estraggo il rettangolo di area minima (in formato (centro_x, centro_y), (width, height), angolo) | |
rect = cv2.minAreaRect(contour) | |
#l'effetto della riscalatura iniziale deve essere eliminato dalle coordinate rilevate | |
rect = \ | |
((int(rect[0][0] * unscale), int(rect[0][1] * unscale)), \ | |
(int(rect[1][0] * unscale), int(rect[1][1] * unscale)), \ | |
rect[2]) | |
#disegno il tutto sull'immagine originale | |
box = np.int0(cv2.cv.BoxPoints(rect)) | |
cv2.drawContours(im_out, [box], 0, (0, 255, 0), thickness = 2) | |
plt.imshow(im_out) | |
#scrittura dell' immagine finale | |
cv2.imwrite(r'img\out.png', im_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment