필터 지우기
필터 지우기

matlab code overload (busy) and not doing what it's supposed to do

조회 수: 3 (최근 30일)
matar maoz
matar maoz 2011년 6월 29일
Hi
I have written an image acquisision and proccessing code which is invoked every time a timer object stops running, or not responding.
the whole project consists 3 fuctions: 1)timer definition 2)image acquisition 3)image processing
when Im debugging the program, everything is working great. when Im making an executable of my program usin the deployment tool, it starts, but closes itself, or not doing anything. if it doesnt close, it just overloads my cpu (to 100%).
The algorithm's run time is infinite, because I am running the timer at a 'fixed spacing' running mode.
the codes are:
1)
function []= timer_me()
clear
clc
close all
%a=imread('tire.tif');
%imshow(a);
global oldfile;
path = 'C:\MATLAB PROJECTS\dirtest';
test=dir(path);
oldfile=test(size(test)).name;
out = timerfind('Name', 'ReadImageTimer');
if(size(out)==0)
t = timer('Name','ReadImageTimer',...
'ExecutionMode', 'fixedSpacing',...
'TimerFcn',@readimage,...
'Period',0.5 );
start(t);
end
while(1);
end-----------------------------
2)
function [ ] = readimage(arg1, arg2, arg3)
path = 'C:\MATLAB PROJECTS\dirtest';
test=dir(path);
filename=test(size(test)).name;
global oldfile
if(~strcmp(oldfile, filename))%IS THE OLDEST FILE IN THE
%DIRECTORY NEW?
i=imread([path '\\' filename]);
imshow(i);
[hit]=coloralg(i);
oldfile=filename;
hit
subplot(2,1,1), imshow(i);
if(hit == 1)
subplot(2,1,1), imshow(i);
xlabel('hit!')
ylabel('hit!')
else
xlabel('miss!')
ylabel('miss!')
end
else
a='same'
end
% start(timer)
end
-------------------------
3)
function [hit] = coloralg(x)
%type=3; %in the actual func will get 1/2/3 according to R/G/B
%[x]=imread('black_red.jpg');
%x=data;
subplot(2,1,1), imshow(x), ylabel('before');
xred=x(:,:,1);
%xgreen=x(:,:,2);
%xblue=x(:,:,3);
[numr,numc,null]=size(x);
quant=90;
for r=1:numr
for c=1:numc
if x(r,c,1)>110
if x(r,c,2)<230
if x(r,c,3)<230
x(r,c,:)=255;
else
x(r,c,:)=0;
end
else
x(r,c,:)=0;
end
else
x(r,c,:)=0;
end
end
end
%xafterred=x(:,:,1);
%xaftergreen=x(:,:,2);
%xafterblue=x(:,:,3);
subplot(2,1,2), imshow(x), ylabel('after ');
middlex=round(size(x)/2);
middlexr=[middlex(1),middlex(2)];
measurepointr = [middlexr(1), middlexr(2) , 1];
wtf=x(measurepointr(1),measurepointr(2),1)
a=imread('tire.tif');
imshow(a);
if(wtf == 255)
hit = 1;
else
hit = 0;
end
------------
Can somebody help me please?
Matar Maoz

채택된 답변

Jan
Jan 2011년 6월 30일
Avoid using "path" as name of a variable. This possibly shadows the important function PATH.
Using == with vectors in an IF condition is not clean:
out = timerfind('Name', 'ReadImageTimer');
if(size(out)==0)
It works here, but prefer:
if isempty(out)
Using CLEAR directly after the function header is meaningless. So better omit it.
I do not think that this does what you expect:
test = dir(path);
filename = test(size(test)).name;
"size(test)" replies a vector. I assume you want "length(test)". But even then, the assumption, that the last file is the oldest is fragile. The reply of the DIR command is sorted alphabetically, but even this is not documented. Better use the datenum's replied by DIR also to find the oldest file.
This is a cruel method to waste energy:
while(1)
end
Because this loop is called infinitely, the timer's callback has no chance to be invoked. At least insert a DRAWNOW, but PAUSE(0.1) would be more friendly. I'd avoid the unnecessary increase of complexity of using a TIMER and use the loop in the main program directly:
while 1
pause(0.5);
readimage;
end
BTW.: The 3 inputs of readimag() are not used anywhere. If you use a modern Matlab version, some warning should appear.
Finally I cannot believe that this program "runs great" when debugging.
  댓글 수: 4
matar maoz
matar maoz 2011년 7월 1일
so I can refrain from using the timer completely?
Paulo Silva
Paulo Silva 2011년 7월 1일
like Jan said you can use the loop and pause instead of the timer, each way has some different problems.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by