clc;
clear;
close all;
warning off;
addpath(genpath(pwd));
image = imread('Fruit Database/Picture 71.jpg');
figure(1),imshow(image);
title('Original image')
load('featureVectors.mat');
mdl = ClassificationKNN.fit(vector,Y,'NumNeighbors',3);
bananaCount= 0;
orangeCount= 0;
appleCount= 0;
grapeCount= 0;
vector = zeros(1,4);
R = zeros(1,4);
img = imadjust(image,[.2 .2 .2; .65 .65 .65],[]);
img = rgb2hsv(img);
hue = img(:,:,1);
saturation = img(:,:,2);
value = img(:,:,3);
[row, col] = size(saturation);
threshold_sat = saturation > 0.4;
thresh = +threshold_sat;
for u = 1:5
thresh = imclose(thresh,ones(9));
end
thresh = imfill(thresh,'holes');
figure(2), imshow(thresh)
title('Thresholded image')
figure(3), imshow(img)
title('HSV image')
connCompThreshold = 1000;
CC = bwconncomp(thresh);
for i = 1:CC.NumObjects
L = length(CC.PixelIdxList{i});
if L < connCompThreshold
thresh(CC.PixelIdxList{i}) = 0;
end
end
figure(4), imshow(thresh)
title('Connected Component thresholded Image')
%find the fruit in the image
CC = bwconncomp(thresh);
%create a label matrix, may be unneccesarry
label = 1;
label_matrix = zeros(row,col);
for n = 1:CC.NumObjects
label_matrix(CC.PixelIdxList{n}) = label;
label = label + 1;
end
% Extract the fruit from the image, loops around for each individual fruit
for i = 1:CC.NumObjects
%get an image with the individual fruit in the image ignoring all
%others
temp = zeros(row,col);
temp(CC.PixelIdxList{i}) = 1;
figure, imshow(temp)
title('Indiviual Fruit that was extracted out')
stats = regionprops(temp,'Area','Perimeter','BoundingBox','Eccentricity','Centroid','FilledImage');
%Put the bounding box that was detected onto the image
hold on
rectange = rectangle('Position', stats.BoundingBox, 'EdgeColor','r');
hold off
x = stats.BoundingBox(1); x = round(x);
%y is the topmost pixel for the CC
y = stats.BoundingBox(2); y = round(y);
%width is the number of pixels from x to the right
width = stats.BoundingBox(3); width = round(width);
%width is the number of pixels from y to the bottom of the image
height = stats.BoundingBox(4); height = round(height);
fruit_size = [width,height];
%get short to longer width and height vector to get rid of a few
%orientation problem
[T,I_max] = max(fruit_size);
[T,I_min] = min(fruit_size);
longer = fruit_size(I_max);
shorter = fruit_size(I_min);
%get the top left, top right, bottom left, bottom right coordinates
topLeftCorner = [x, y];
topRightCorner = [x + width, y];
bottomLeftCorner = [x, y + height];
bottomRightCorner = [x + width, y + height];
%crop out the sub image to send to knn and feature selection
subImage = image(y:(y + height -1),x:(x + width -1),:);
%Threshold the unneccesarry parts in the RGB to white
filledImage = stats.FilledImage;
bIndinces = find(filledImage == 0);
%seperate out each channel in the rgb and set it equal to white
channelR = subImage(:,:,1);
channelG = subImage(:,:,2);
channelB = subImage(:,:,3);
channelR(bIndinces) = 255;
channelG(bIndinces) = 255;
channelB(bIndinces) = 255;
rgbOut = cat(3, channelR, channelG, channelB);
figure,imshow(rgbOut)
title('Segmented out RGB fruit with white surrounding')
figure,imshow(image(y:(y + height),x:(x + width),:));
title('Segmented out RGB fruit')
%imwrite(subImage,'croppedBanana_1.png','PNG');
[ clr ] = findFeat(rgbOut);
Xnew = [stats.Eccentricity, longer/1000, shorter/1000, clr/3];
vector(i,:) = Xnew;
% returns a matrix of scores, indicating the likelihood that a label comes
% from a particular class.
[label,POSTERIOR, score] = predict(mdl,Xnew);
%get fruit count
switch label
case 'B'
bananaCount= bananaCount+1;
figure(1),text(x,y,'banana')
case 'A'
appleCount= appleCount+1;
figure(1),text(x,y,'apple')
case 'G'
grapeCount= grapeCount +1;
figure(1),text(x,y,'grape')
case 'O'
orangeCount= orangeCount+1;
figure(1),text(x,y,'orange')
end
end
没有合适的资源?快使用搜索试试~ 我知道了~
matlab-基于KNN算法的水果分割和类型识别matlab仿真-源码
共6个文件
m:5个
mat:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 173 浏览量
2021-09-17
23:40:49
上传
评论 1
收藏 6KB RAR 举报
温馨提示
matlab_基于KNN算法的水果分割和类型识别matlab仿真_源码
资源推荐
资源详情
资源评论
收起资源包目录
matlab_基于KNN算法的水果分割和类型识别matlab仿真_源码.rar (6个子文件)
matlab_基于KNN算法的水果分割和类型识别matlab仿真_源码
matlab_基于KNN算法的水果分割和类型识别matlab仿真_源码
matlab
featureVectors.mat 3KB
Runme.m 4KB
func
knnTrainingScript.m 1KB
findFeat.m 294B
FruitFeatureExtract.m 2KB
findFeatTester.m 243B
共 6 条
- 1
资源评论
- 留揽2022-03-24用户下载后在一定时间内未进行评价,系统默认好评。
mYlEaVeiSmVp
- 粉丝: 1918
- 资源: 19万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功