Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from skimage import data, img_as_float, img_as_ubyte
- from skimage import io, img_as_ubyte, exposure, util, morphology
- from skimage.color import rgb2gray, rgb2hsv
- import matplotlib.pyplot as plt
- import cv2
- import numpy as np
- import random
- def show2imgs(im1, im2, title1='Obraz pierwszy', title2='Obraz drugi', size=(10,10)):
- import matplotlib.pyplot as plt
- f, (ax1, ax2) = plt.subplots(1,2, figsize=size)
- ax1.imshow(im1, cmap='gray')
- ax1.axis('off')
- ax1.set_title(title1)
- ax2.imshow(im2, cmap='gray')
- ax2.axis('off')
- ax2.set_title(title2)
- plt.show()
- #Segmentacja i scalanie obiektów
- def segmentuj(img):
- for i in range(img.shape[0]-4):
- for j in range(img.shape[1]-4):
- if img[i][j] != 255.0:
- if img[i][j] == 0.0:
- img[i][j] = random.randint(1, 250)
- if img[i-1][j-1] != 0.0 and img[i-1][j-1] != 255.0:
- img[i][j] = img[i-1][j-1]
- elif img[i-1][j] != 0.0 and img[i-1][j] != 255.0:
- img[i][j] = img[i-1][j]
- elif img[i-1][j+1] != 0.0 and img[i-1][j+1] != 255.0:
- img[i][j] = img[i-1][j+1]
- elif img[i][j-1] != 0.0 and img[i][j-1] != 255.0:
- img[i][j] = img[i][j-1]
- elif img[i][j+1] != 0.0 and img[i][j+1] != 255.0:
- img[i][j] = img[i][j+1]
- elif img[i+1][j-1] != 0.0 and img[i+1][j-1] != 255.0:
- img[i][j] = img[i-1][j-1]
- elif img[i+1][j] != 0.0 and img[i+1][j] != 255.0:
- img[i][j] = img[i+1][j]
- elif img[i+1][j+1] != 0.0 and img[i+1][j+1] != 255.0:
- img[i][j] = img[i+1][j+1]
- else:
- continue
- #scalanie obiektów
- if img[i][j] != img[i-1][j-1] and img[i-1][j-1] != 255.0:
- img[i][j] = img[i-1][j-1]
- elif img[i][j] != img[i-1][j] and img[i-1][j] != 255.0:
- img[i][j] = img[i-1][j]
- elif img[i][j] != img[i-1][j+1] and img[i-1][j+1] != 255.0:
- img[i-1][j+1] = img[i][j]
- elif img[i][j] != img[i][j-1] and img[i][j-1] != 255.0:
- img[i][j-1] = img[i][j]
- elif img[i][j] != img[i][j+1] and img[i][j+1] != 255.0:
- img[i][j+1] = img[i][j]
- elif img[i][j] != img[i+1][j-1] and img[i+1][j-1] != 255.0:
- img[i][j] = img[i+1][j-1]
- elif img[i][j] != img[i+1][j] and img[i+1][j] != 255.0:
- img[i][j] = img[i+1][j]
- elif img[i][j] != img[i+1][j+1] and img[i+1][j+1] != 255.0:
- img[i][j] = img[i+1][j+1]
- else:
- continue
- return img
- def scal(img):
- for i in range(img.shape[0]-4):
- for j in range(img.shape[1]-4):
- if img[i][j] != img[i-1][j-1] and img[i-1][j-1] != 255:
- img[i][j] = img[i-1][j-1]
- elif img[i][j] != img[i-1][j] and img[i-1][j] != 255:
- img[i][j] = img[i-1][j]
- elif img[i][j] != img[i-1][j+1] and img[i-1][j+1] != 255:
- img[i-1][j+1] = img[i][j]
- elif img[i][j] != img[i][j-1] and img[i][j-1] != 255:
- img[i][j-1] = img[i][j]
- elif img[i][j] != img[i][j+1] and img[i][j+1] != 255:
- img[i][j+1] = img[i][j]
- elif img[i][j] != img[i+1][j-1] and img[i+1][j-1] != 255:
- img[i][j] = img[i+1][j-1]
- elif img[i][j] != img[i+1][j] and img[i+1][j] != 255:
- img[i][j] = img[i+1][j]
- elif img[i][j] != img[i+1][j+1] and img[i+1][j+1] != 255:
- img[i][j] = img[i+1][j+1]
- else:
- continue
- return img
- im = io.imread('przeskalowane/0.09v2.jpg')
- plt.figure(figsize=(15,10))
- plt.imshow(im, cmap="gray")
- plt.axis('off')
- plt.show()
- th = 150
- img = img_as_ubyte(rgb2gray(im))
- plt.imshow(img, cmap="gray")
- plt.axis('on')
- plt.show()
- th, bimm = cv2.threshold(img, thresh=th, maxval=255, type=cv2.THRESH_OTSU)
- mbim = cv2.medianBlur(bimm, 11)
- plt.imshow(mbim, cmap="gray")
- plt.axis('on')
- plt.show()
- dt = cv2.distanceTransform(mbim, distanceType=cv2.DIST_L2, maskSize=cv2.DIST_MASK_PRECISE)
- th = 45
- th, bim = cv2.threshold(dt, thresh=th, maxval=255, type=cv2.THRESH_BINARY_INV)
- show2imgs(bim, dt, title1='Obraz po binaryzacji', title2='Obraz po transformacie odległościowej', size=(25,25))
- kernel = np.ones((3,3),np.uint8)
- erodeBin = cv2.erode(bim, kernel=kernel, iterations=25)
- plt.imshow(erodeBin, cmap="hot")
- plt.axis('on')
- plt.show()
- #Liczenie ilości obiektów na obrazie
- def licz_obiekty(img):
- ile_obiektow = 0
- obiekty = []
- for i in range(img.shape[0]):
- for j in range(img.shape[1]):
- if img[i][j] != 255:
- obiekty.append(img[i][j])
- ile_obiektow = len(list(set(obiekty)))# zlicza niepowtarzające się elementy listy
- return ile_obiektow
- nimg = segmentuj(bim)
- tmp = 0
- ile = licz_obiekty(nimg)
- print(ile)
- while tmp != ile:
- print(tmp)
- nimg = scal(nimg)
- nimg = scal(nimg)
- tmp = ile
- ile = licz_obiekty(nimg)
- print("Wykonało się!")
- plt.imshow(nimg, cmap="hot")
- plt.axis('on')
- plt.show()
- ile_ob = ile
- print("Obiektów jest:\t", ile_ob)
- #od tego momentu próby zliczenia ich nominałów
- nimg = erodeBin = cv2.erode(bim, kernel=kernel, iterations=48)
- mbim = cv2.medianBlur(bimm, 9)
- dist = cv2.distanceTransform(mbim, cv2.DIST_L1 , 5)
- im2, contours, hierarchy = cv2.findContours(mbim, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- cv2.drawContours(nimg, contours, -1, (0,0,0), 7)
- #show2imgs(mbim, nimg, title1='Obraz po binaryzacji', title2='Obraz po transformacie odległościowej', size=(25,25))
- plt.imshow(nimg, cmap="gray")
- plt.axis('on')
- plt.show()
- print(contours)
- kolory = []
- jakie_obiekty = []
- for i in range(nimg.shape[0]):
- for j in range(nimg.shape[1]):
- if nimg[i][j] != 0:
- if nimg[i][j] != 255:
- kolory.append(nimg[i][j])
- jakie_obiekty = list(set(kolory))
- print(jakie_obiekty)
- pola_nowe = {}
- for x in jakie_obiekty:
- ile_pikseli = 0
- for i in range(nimg.shape[0]):
- for j in range(nimg.shape[1]):
- if nimg[i][j] == x:
- ile_pikseli += 1
- pola_nowe[x] = ile_pikseli
- for i in pola_nowe:
- print(i, pola_nowe[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement