Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- infiles = list(map(addpath, os.listdir('./photos/source/')))
- statfile = open('stat_resized_pyr-2.txt', 'w')
- for egg_image in infiles:
- #read and compress photo
- #
- #
- orig_img = cv2.imread(egg_image)
- scale_percent = 20
- width = int(orig_img.shape[1] * scale_percent / 100)
- height = int(orig_img.shape[0] * scale_percent / 100)
- dim = (width, height)
- img = cv2.resize(orig_img, dim, interpolation = cv2.INTER_AREA)
- # img = orig_img
- #color in range
- #
- #
- img = cv2.pyrMeanShiftFiltering(img, 120, 51)
- img = cv2.GaussianBlur(img, (7, 7), 2)
- shifted_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- shifted_hsv = cv2.cvtColor(shifted_rgb, cv2.COLOR_RGB2HSV)
- #HSV->HSV convertation
- #cv2 HSV: 0-179, 0-255, 0-255
- #paint.net HSV: 0-360, 0-100, 0-100
- lb, db = (175, 0, 80), (210, 40, 100)
- light_blue = (lb[0]/2, int(lb[1]*2.55), int(lb[2]*2.55))
- dark_blue = (db[0]/2, int(db[1]*2.55), int(db[2]*2.55))
- mask = cv2.inRange(shifted_hsv, light_blue, dark_blue)
- res_mask = cv2.bitwise_and(shifted_rgb, shifted_rgb, mask = mask)
- #res_mask_shifted = cv2.pyrMeanShiftFiltering(res_mask, 120, 51)
- #res_mask_shifted = cv2.pyrMeanShiftFiltering(res_mask, 20, 51)
- #plt.imshow(res_mask_shifted)
- #print(mask.shape)
- gray = cv2.cvtColor(res_mask,cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
- #plt.imshow(res_mask)
- # noise removal
- #
- #
- kernel = np.ones((3,3),np.uint8)
- opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 3)
- for string in opening:
- for i in range(len(string)):
- if(string[i] == 255): string[i] = 0
- else: string[i] = 255
- #plt.imshow(opening)
- # sure background area
- #
- #
- sure_bg = cv2.dilate(opening,kernel,iterations=3)
- #else: string[i] = 255
- #plt.imshow(sure_bg)
- # Finding sure foreground area
- #
- #
- dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
- ret, sure_fg = cv2.threshold(dist_transform,0.4*dist_transform.max(),255,0)
- #plt.imshow(sure_fg)
- #plt.imshow(dist_transform, 'gray')
- # Finding unknown region
- #
- #
- sure_fg = np.uint8(sure_fg)
- unknown = cv2.subtract(sure_bg,sure_fg)
- #plt.imshow(sure_fg)
- # Marker labelling
- #
- #
- %matplotlib inline
- ret, markers = cv2.connectedComponents(sure_fg)
- #print(ret)
- #plt.imshow(markers)
- # Add one to all labels so that sure background is not 0, but 1
- markers = markers+1
- # Now, mark the region of unknown with zero
- markers[unknown==255] = 0
- #plt.imshow(markers)
- #
- #
- %matplotlib inline
- import time
- markers = cv2.watershed(res_mask,markers)
- res_mask[markers == -1] = [255,0,0]
- plt.imshow(markers)
- #print(markers)
- eggs = []
- result = img.copy()
- for egg_i in range(0, ret+1):
- egg = np.zeros(shape = markers.shape)
- for i in range(len(markers)):
- for j in range(len(markers[i])):
- if(markers[i,j] == egg_i):
- egg[i,j] = egg_i
- eggs.append(egg)
- counter = 0
- print(len(eggs))
- for i in range(2, len(eggs)):
- result1 = cv2.bitwise_and(img, img, mask=np.int8(eggs[i]))
- contours = findContours(result1)
- maxArea = 0
- ind = 0
- for c in range(len(contours)):
- area = cv2.contourArea(contours[c]) # площадь
- if(area > maxArea):
- ind = c
- maxArea = area
- try:
- kk = contours[ind]
- except:
- continue
- if(len(contours[ind]) < 5): continue
- (x, y), (MA, ma), angle = cv2.fitEllipse(contours[ind])
- el_area = 3.14 * MA * ma / 4
- size = MA*ma*ma*4*3.14/3
- if(el_area > area*1.2 or el_area < area*0.2): continue
- cv2.ellipse(result1, (int(x), int(y)), (int(MA/2), int(ma/2)), int(angle), startAngle = 0, endAngle = 360, color = (0,255,0), thickness = 1)
- cv2.imwrite('./photos/output_rsz_pyr-2/'+egg_image.split('/')[3][:-4]+'_result_'+str(i-1) +'.jpg',result1)
- statfile.write(egg_image.split('/')[3]+'\t')
- statfile.write('{}\t'.format(i-1))
- statfile.write('{}\t'.format(area))
- statfile.write('{}\t'.format(el_area))
- statfile.write('{}\t'.format(round(size,2)))
- statfile.write('{}\t'.format(MA))
- statfile.write('{}\n'.format(ma))
- counter += 1
- statfile.write('eggs: {}\n\n'.format(counter))
- statfile.close()
- statfile = open('stat_resized_pyr-2.txt', 'a')
- statfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement