Question: Python programming: How do i use DBSCAN clustering to detect the lemons from the image of a lemon tree? The below code loads the image

Python programming: How do i use DBSCAN clustering to detect the lemons from the image of a lemon tree? The below code loads the image and run Kmeans clustering with a K = 10. I have to count the number of lemons in that image. The first step to count the lemons is to detect the lemons. You have to do further processing to automatically count the number of lemons. The picture "lemon_count.jpg" is from (https://www.limundo.com/kupovina/Za-kucu-i-dvoriste/Seme-lukovice-i-pelceri/Semena/LIMUN-Meyer-SEME/63891629)

import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg %matplotlib inline

from sklearn import cluster, datasets #Read the image #A color image is read as a 3-dimensional array, so we will convert it to a 2 dimensional pixel array in X

imgo = mpimg.imread('lemon_count.jpg') plt.imshow(imgo) plt.show() imagesz = np.shape(imgo) X = np.reshape(imgo,[imagesz[0]*imagesz[1],3]) X.setflags(write=1) X1 = X #Perform Kmeans clustering

K = 10 km = cluster.KMeans(n_clusters=K, init='k-means++').fit(X) kmlabels = km.labels_ unique_labels = set(kmlabels) K = len(set(kmlabels))

#Plot the clusters on the image replacing original pixel color with the centroid pixel value

for k in range(K): class_member_mask = (kmlabels == k) X1[class_member_mask,:] = km.cluster_centers_[k,:]

#First reshape X1 to the original 3-dimensional image array and then plot

imgc = np.reshape(X1,[imagesz[0],imagesz[1],3]) plt.imshow(imgc) plt.show() # Find the cluster corresponding to the lemon (color = yellow) and show that cluster only

fruit_color = [255, 255, 0] #yellow mindist = 999999999.0 for k in range(K): dist = np.linalg.norm(fruit_color - km.cluster_centers_[k,:]) if(dist

I just need the DBSCAN code on how to count the number of lemons from the result done from the code above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!