what is my wrong with my code

조회 수: 1 (최근 30일)
cem yanbul
cem yanbul 2021년 6월 17일
댓글: cem yanbul 2021년 6월 17일
Write a program that finds the number of each fruit in the picture below and writes its name on the fruit.
m=imread('fruits.jpg');
figure(1), subplot(2,3,1),imshow(m);
gm=rgb2gray(m);
figure(1), subplot(2,3,2),imshow(gm);
lv=graythresh(gm);
sbm=im2bw(gm,lv);
figure(1), subplot(2,3,3),imshow(sbm);
nk=bwareaopen(sbm,180);
figure(1), subplot(2,3,4),imshow(nk);
s=strel('disk',10);
bk=imclose(nk,s);
figure(1), subplot(2,3,5),imshow(bk);
tc=imcomplement(bk);
figure(1), subplot(2,3,6),imshow(tc);
[B,L] = bwboundaries(tc);
stats = regionprops(tc, 'Area','Centroid');
figure(2),imshow(m);
banana=0;
elma=0;
orange=0;
unknow=0;
for n=1:length(B)
a=stats(n).Area;
centroid=stats(n).Centroid;
if a> 28000
banana=banana+1;
text(centroid(1),centroid(2),'\color{red}banana');
elseif a >19000 && a < 21000
orange=orange+1;
text(centroid(1),centroid(2),'\color{magenta}orange');
elseif a >1700 && a < 10000
elma=elma+1 ;
text(centroid(1),centroid(2),'\color[rgb]{0 .5 .5}apple');
else
unknow=unknow+1;
text(centroid(1),centroid(2),'unknow');
end
end
title(['banana: ',num2str(banana),', apple: ',num2str(apple),', orange: ',num2str(orange)]);
  댓글 수: 1
Steven Lord
Steven Lord 2021년 6월 17일
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2021년 6월 17일
Try the color thresholder to segment each color individually. So you'll export 3 createMask() functions from it.
Then use bwareaopen() or bwareafilt() to limit the blobs to only those that are big enough. Then use this link to separate the blobs:
For the bananas, it's trickier. You might have to segment out brown color and find the brown tips. Then take those that either are on the left half of the image (if you're allowed to assume that), or keep those that are touching yellow pixels.
  댓글 수: 6
Image Analyst
Image Analyst 2021년 6월 17일
I have no time now. I need to leave. Try the steps I gave you and see how that works.
cem yanbul
cem yanbul 2021년 6월 17일
okay and thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by