|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +def main(): |
| 6 | + |
| 7 | + path = "C:\\Users\\enesa\\Documents\\MATLAB\\blobs_objects.jpg" |
| 8 | + |
| 9 | + img = cv2.imread(path, 1) |
| 10 | + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 11 | + |
| 12 | + filter1 = np.array(([0, -1, 0], [-1, 5, -1], [0, -1, 0]), np.float32) #Sharpening Filter |
| 13 | + output = cv2.filter2D(img, -1, filter1) #convolution filter |
| 14 | + |
| 15 | + blur = cv2.GaussianBlur(img,(5,5),0) |
| 16 | + |
| 17 | + gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY) |
| 18 | + |
| 19 | + _, thresh = cv2.threshold(gray,170,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) |
| 20 | + |
| 21 | +########################################################################################################################### |
| 22 | + |
| 23 | + # Create a simple filter. The kernel slides through the image (as in 2D convolution). |
| 24 | + kernel = np.ones((3, 3), np.uint8) |
| 25 | + |
| 26 | + # Create a Rectangular Structuring Element |
| 27 | + se1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
| 28 | + |
| 29 | + # Create a Elliptical Structuring Element |
| 30 | + se2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 31 | + |
| 32 | + # Apply Erosion method over the image with kernel |
| 33 | + erosion = cv2.erode(thresh,se1,iterations = 1) |
| 34 | + |
| 35 | + # Apply Dilation method over the image with kernel |
| 36 | + dilation = cv2.dilate(thresh,se2,iterations = 1) |
| 37 | + |
| 38 | + # Noise removal using Morphological closing operation |
| 39 | + closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel, iterations = 4) |
| 40 | + |
| 41 | + # Noise removal using Morphological opening operation |
| 42 | + opening = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, kernel, iterations = 1) |
| 43 | + |
| 44 | +########################################################################################################################### |
| 45 | + |
| 46 | + dilation = 255 - dilation # Complementing Operation |
| 47 | + |
| 48 | + _, contours, _ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| 49 | + |
| 50 | + print("{} Objects have detected!".format(len(contours))) |
| 51 | + |
| 52 | + original = cv2.imread(path, 1) |
| 53 | + |
| 54 | + original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) |
| 55 | + |
| 56 | + sayac = 0 |
| 57 | + for i in contours: |
| 58 | + # perimeter = cv2.arcLength(i,True) |
| 59 | + # if perimeter > 20: |
| 60 | + sayac = sayac +1 |
| 61 | + #cv2.drawContours(img, contours, -1, (0, 0, 255), 2) |
| 62 | + x,y,w,h = cv2.boundingRect(i) |
| 63 | + cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) |
| 64 | + cv2.putText(img, str(sayac), (x+10, y+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) |
| 65 | + |
| 66 | + #plt.plot(cx, cy, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=12) # belilenen noktaya x isareti koy. |
| 67 | + #cv2.putText(img, 'x', (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) |
| 68 | + #cv2.putText(closing, str(sayac), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) |
| 69 | + |
| 70 | + print("{} Objects have drown!".format(sayac)) |
| 71 | + |
| 72 | +########################################################################################################################### |
| 73 | + # output = [original, img] |
| 74 | + # titles = ['Original', 'Contours'] |
| 75 | + |
| 76 | + |
| 77 | + # for i in range(2): |
| 78 | + # plt.subplot(1, 2, i+1) |
| 79 | + # plt.imshow(output[i]) |
| 80 | + # plt.title(titles[i]) |
| 81 | + # plt.xticks([]) |
| 82 | + # plt.yticks([]) |
| 83 | + |
| 84 | + cv2.imshow('Orignal Image', img) |
| 85 | + #cv2.imshow('Erosion Image', erosion) |
| 86 | + cv2.imshow('Dilation Image', dilation) |
| 87 | + cv2.imshow('Closing Image', closing) |
| 88 | + cv2.imshow('Opening Image', opening) |
| 89 | + |
| 90 | + |
| 91 | + plt.show() |
| 92 | + cv2.waitKey(0) |
| 93 | + cv2.destroyAllWindows() |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments