Hi,
To automatically detect the infected region in an olive leaf image after segmenting it into clusters using KNN, using a neural network inside KNN is not feasible, as they have fundamentally different approaches. You can try some things below:
Post-Processing with KNN Results:
- After segmenting the image using KNN, you can analyze the clusters to identify the one corresponding to the infected region. This can be done by examining the color or texture characteristics typical of infection.
Machine Learning Approaches:
- Random Forest or SVM: Use features extracted from the segmented clusters (e.g., color histograms, texture features) as input to a classifier like Random Forest or Support Vector Machine to identify the infected region.
- Deep Learning: Train a convolutional neural network (CNN) specifically for the task of infection detection. This requires a labeled dataset of infected and healthy leaf images.
Image Processing Techniques:
- Thresholding: Apply thresholding techniques to enhance the contrast between healthy and infected areas.
- Edge Detection: Use edge detection algorithms to highlight the boundaries of the infected region.
Unsupervised Learning:
- Clustering Analysis: After KNN, you can apply additional clustering techniques like DBSCAN or Agglomerative Clustering to further refine and identify the infected region.
Assuming you have your image:
leafImage = imread('olive_leaf.jpg');
labImage = rgb2lab(leafImage);
[nrows, ncols, nchannels] = size(labImage);
labImageReshaped = reshape(labImage, nrows*ncols, nchannels);
[clusterIdx, clusterCenters] = kmeans(labImageReshaped, numClusters, 'Distance', 'sqEuclidean', 'Replicates', 3);
pixelLabels = reshape(clusterIdx, nrows, ncols);
segmentedImages = cell(1, numClusters);
rgbLabel = repmat(pixelLabels, [1, 1, 3]);
color(rgbLabel ~= k) = 0;
segmentedImages{k} = color;
figure, imshow(segmentedImages{k}), title(['Objects in Cluster ', num2str(k)]);
infectedRegion = segmentedImages{2};
grayInfected = rgb2gray(infectedRegion);
binaryInfected = imbinarize(grayInfected, 'adaptive', 'ForegroundPolarity', 'dark', 'Sensitivity', 0.4);
cleanedInfected = imopen(binaryInfected, se);
figure, imshow(cleanedInfected), title('Detected Infected Region');
Try this out and let me know if this helps!