Tải bản đầy đủ - 0 (trang)
Code MATLAB nhận dạng giống lúa.

# Code MATLAB nhận dạng giống lúa.

Tải bản đầy đủ - 0trang

function pushbutton2_Callback(hObject, eventdata, handles)

global cam1;

global Q;

I=snapshot(cam1);

axes(handles.axes1);

imshow(I);

anhxam=rgb2gray(I);

anhnhiphan=im2bw(anhxam,graythresh(anhxam));

loc=edge(anhnhiphan,'canny',graythresh(anhxam));

xuly1=bwareaopen(loc,20);

se = strel('disk',1);

xuly2 = imclose(xuly1,se);

r=I(:,:,1);

r=double(r);

[x1,z1]=size(r);

R = sum(r,1);

Sr = sum(R,2);

TBr = Sr/(x1*z1);

maxR = max( max(r));

minR = min(min(r));

stdR = std(std(r));

g=I(:,:,2);

g=double(g);

[x2,z2]=size(g);

G = sum(g,1);

Sg = sum(G,2);

TBg = Sg/(x2*z2);

maxG = max( max(g));

minG = min(min(g));

stdG = std(std(g));

b=I(:,:,3);

b=double(b);

[x3,z3]=size(b);

B = sum(b,1);

Sb = sum(B,2);

TBb = Sb/(x3*z3);

maxB = max( max(b));

minB = min(min(b));

stdB = std(std(b));

for mm = 1:size(xuly2,1)

for nn = 1:size(xuly2,2)

if ((xuly2(mm,nn) == 1))

X(mm,nn,:) = [0 255 0]; % green outline in shape

end

end

end

axes(handles.axes2);

imshow(X);

[laythongso num] = bwlabel(xuly2,8);

thongso = regionprops(laythongso,'all');

dtich =double( uint16(zeros(1,num)));

chuvi = double(uint16(zeros(1,num)));

dai = double(uint16(zeros(1,num)));

rong =double(uint16(zeros(1,num)));

tyledairong = double(uint16(zeros(1,num)));

dotron=double(uint8(zeros(1,num)));

for i = 1 : num

dtich(i) = thongso(i).FilledArea;

chuvi(i) = thongso(i).Perimeter;

dai(i) = thongso(i).MajorAxisLength;

rong(i) = thongso(i).MinorAxisLength;

dotron(i) = (4*dtich(i)*pi)/(chuvi(i).^2);

tyledairong(i) = dai(i)/rong(i);

end

dtichTB = sum(dtich,2)/num;

chuviTB= sum(chuvi,2)/num;

daiTB= sum(dai,2)/num;

rongTB = sum(rong,2)/num;

dotronTB = sum(dotron,2)/num;

tyledairongTB = sum(tyledairong,2)/num;

set(handles.edit1,'String',num2str(maxR));set(handles.edit4,'String',num2st

r(minR));set(handles.edit7,'String',num2str(TBr));set(handles.edit10,'Strin

g',num2str(stdR));

set(handles.edit2,'String',num2str(maxG));set(handles.edit5,'String',num2st

r(minG));set(handles.edit8,'String',num2str(TBg));set(handles.edit11,'Strin

g',num2str(stdG));

set(handles.edit3,'String',num2str(maxB));set(handles.edit6,'String',num2st

r(minB));set(handles.edit9,'String',num2str(TBb));set(handles.edit12,'Strin

g',num2str(stdB));

set(handles.edit13,'String',num2str(dtichTB));set(handles.edit16,'String',n

um2str(rongTB));

set(handles.edit14,'String',num2str(chuviTB));set(handles.edit17,'String',n

um2str(tyledairongTB));

set(handles.edit15,'String',num2str(daiTB));set(handles.edit18,'String',num

2str(dotronTB));

Q =

[dtichTB;chuviTB;daiTB;rongTB;dotronTB;tyledairongTB;maxR;maxG;maxB;minR;mi

nG;minB;stdR;stdG;stdB;TBr;TBg;TBb];

function pushbutton3_Callback(hObject, eventdata, handles)

closepreview;

clear all;

trainnn;

function pushbutton4_Callback(hObject, eventdata, handles)

global s;

closepreview;

fclose(s);

close doannhandang ;

clear all;

function pushbutton6_Callback(hObject, eventdata, handles)

set(handles.edit1,'String','');set(handles.edit4,'String','');set(handles.e

dit7,'String','');set(handles.edit10,'String','');

set(handles.edit2,'String','');set(handles.edit5,'String','');set(handles.e

dit8,'String','');set(handles.edit11,'String','');

set(handles.edit3,'String','');set(handles.edit6,'String','');set(handles.e

dit9,'String','');set(handles.edit12,'String','');

set(handles.edit13,'String','');set(handles.edit16,'String','');

set(handles.edit14,'String','');set(handles.edit17,'String','');

set(handles.edit15,'String','');set(handles.edit18,'String','');

set(handles.edit19,'String','');

axes(handles.axes1);

cla reset;

axes(handles.axes2);

function pushbutton9_Callback(hObject, eventdata, handles)

global s;

global p2;

if (p2==1)

fopen(s);

end

if (p2==2)

fwrite(s,get(handles.edit19,'String'));

end

global p2;

function BytesAvailable_callback(hObject, eventdata,handles)

global s;

global cam1;

global Q;

kiemtrahocki=fscanf(s,'%d')

if (kiemtrahocki == 1)

I=snapshot(cam1);

figure

imshow(I);

anhxam=rgb2gray(I);

anhnhiphan=im2bw(anhxam,graythresh(anhxam));

loc=edge(anhnhiphan,'canny',graythresh(anhxam));

xuly1=bwareaopen(loc,20);

se = strel('disk',1);

xuly2 = imclose(xuly1,se);

r=I(:,:,1);

r=double(r);

[x1,z1]=size(r);

R = sum(r,1);

Sr = sum(R,2);

TBr = Sr/(x1*z1);

maxR = max( max(r));

minR = min(min(r));

stdR = std(std(r));

g=I(:,:,2);

g=double(g);

[x2,z2]=size(g);

G = sum(g,1);

Sg = sum(G,2);

TBg = Sg/(x2*z2);

maxG = max( max(g));

minG = min(min(g));

stdG = std(std(g));

b=I(:,:,3);

b=double(b);

[x3,z3]=size(b);

B = sum(b,1);

Sb = sum(B,2);

TBb = Sb/(x3*z3);

maxB = max( max(b));

minB = min(min(b));

stdB = std(std(b));

for mm = 1:size(xuly2,1)

for nn = 1:size(xuly2,2)

if ((xuly2(mm,nn) == 1))

X(mm,nn,:) = [0 255 0]; % green outline in shape

end

end

end

figure

hold on

imshow(X);

[laythongso num] = bwlabel(xuly2,8);

thongso = regionprops(laythongso,'all');

dtich =double( uint16(zeros(1,num)));

chuvi = double(uint16(zeros(1,num)));

dai = double(uint16(zeros(1,num)));

rong =double(uint16(zeros(1,num)));

tyledairong = double(uint16(zeros(1,num)));

dotron=double(uint8(zeros(1,num)));

for i = 1 : num

dtich(i) = thongso(i).FilledArea;

chuvi(i) = thongso(i).Perimeter;

dai(i) = thongso(i).MajorAxisLength;

rong(i) = thongso(i).MinorAxisLength;

dotron(i) = (4*dtich(i)*pi)/(chuvi(i).^2);

tyledairong(i) = dai(i)/rong(i);

end

dtichTB = sum(dtich,2)/num;

chuviTB= sum(chuvi,2)/num;

daiTB= sum(dai,2)/num;

rongTB = sum(rong,2)/num;

dotronTB = sum(dotron,2)/num;

tyledairongTB = sum(tyledairong,2)/num;

set(handles.edit1,'String',num2str(maxR));set(handles.edit4,'String',num2st

r(minR));set(handles.edit7,'String',num2str(TBr));set(handles.edit10,'Strin

g',num2str(stdR));

set(handles.edit2,'String',num2str(maxG));set(handles.edit5,'String',num2st

r(minG));set(handles.edit8,'String',num2str(TBg));set(handles.edit11,'Strin

g',num2str(stdG));

set(handles.edit3,'String',num2str(maxB));set(handles.edit6,'String',num2st

r(minB));set(handles.edit9,'String',num2str(TBb));set(handles.edit12,'Strin

g',num2str(stdB));

set(handles.edit13,'String',num2str(dtichTB));set(handles.edit16,'String',n

um2str(rongTB));

set(handles.edit14,'String',num2str(chuviTB));set(handles.edit17,'String',n

um2str(tyledairongTB));

set(handles.edit15,'String',num2str(daiTB));set(handles.edit18,'String',num

2str(dotronTB));

Q =

[dtichTB;chuviTB;daiTB;rongTB;dotronTB;tyledairongTB;maxR;maxG;maxB;minR;mi

nG;minB;stdR;stdG;stdB;TBr;TBg;TBb];

end

if (kiemtrahocki == 2)

t = sim(network1,Q);

t= compet(t);

set(handles.edit19,'String',num2str(nhandang(t)));

fwrite(s,get(handles.edit19,'String'));

end

if (kiemtrahocki == 3)

set(handles.edit1,'String','');set(handles.edit4,'String','');set(handles.e

dit7,'String','');set(handles.edit10,'String','');

set(handles.edit2,'String','');set(handles.edit5,'String','');set(handles.e

dit8,'String','');set(handles.edit11,'String','');

set(handles.edit3,'String','');set(handles.edit6,'String','');set(handles.e

dit9,'String','');set(handles.edit12,'String','');

set(handles.edit13,'String','');set(handles.edit16,'String','');

set(handles.edit14,'String','');set(handles.edit17,'String','');

set(handles.edit15,'String','');set(handles.edit18,'String','');

set(handles.edit19,'String','');

axes(handles.axes1);

cla reset;

axes(handles.axes2);

cla reset;

end

 Code MATLAB hàm nhận dạng

function gionglua = nhandang(w)

pos = find(w==1)

if (pos==1)

gionglua='Thom RVT';

end

if (pos==2)

gionglua='HT1';

end

if (pos==3)

gionglua='IR 4625';

end

if (pos==4)

gionglua='OM 4500';

end

if (pos==5)

gionglua='Dai thom 8';

end

 Code Arduino hiển thị LCD

#include ;

#include ;

LiquidCrystal_I2C lcd(0x3F,16,2);

const int nutnhan1 = 3;

const int nutnhan2 = 4;

const int nutnhan3 = 6;

int nn1 = 0;

int nn2 = 0;

int nn3 = 0;

int a = 0;

int b = 0;

int c = 0;

void setup() {

Serial.begin(9600);

lcd.init();

lcd.backlight();

lcd.clear();

lcd.setCursor(1,0);

lcd.print("NHAN DANG LUA");

pinMode(nutnhan1,INPUT_PULLUP);

pinMode(nutnhan2,INPUT_PULLUP);

pinMode(nutnhan3,INPUT_PULLUP);

}

void loop() {

Serial.println(a);

delay(100);

Serial.println(b);

delay(50);

Serial.println(c);

delay(50);

if (nn1 == LOW){

a = 1;

lcd.clear();

lcd.setCursor(3,0);

lcd.print("DA THU ANH");}

else{

a = 0;

}

if (nn2 == LOW){

b = 2;

lcd.clear();

}

else {

b = 0;

}

if (nn3 == LOW){

c = 3;

lcd.clear();

lcd.setCursor(1,0);

lcd.print("NHAN DANG LUA");

}

else {

c = 0;

}

if (Serial.available())

{ delay(1000);

lcd.clear();

lcd.setCursor(3,0);

lcd.print("GIONG LUA");

lcd.setCursor(4,1);

while (Serial.available() > 0){

lcd.print(du_lieu);

}

}

}

### Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Code MATLAB nhận dạng giống lúa.

Tải bản đầy đủ ngay(0 tr)

×