Why my code doesn't work in Matlab 2017, Although it works very well in Matlab 2013 ?

1 view (last 30 days)
Hello,
I wrote a code via Matlab 2013 for processing images from video, and it works very well.
But when I run it on Matlab 2017, It works very slowly, and the response of sliders moving take a lot of time. then the button "OK" fails to respond at all.
Here is my code:
clc; clear;
% here you need to change the video you are working on
Vid = VideoReader('1.mp4');
Num_Frames = Vid.NumberOfFrames;
flag=1;
rot=0;
for k = 1:Num_Frames
Img = read(Vid,k);
if(k==1)
mg=imresize(Img,[360,480]);
imshow(mg)
ss=input('Is your image rotated [yes or no]? ','s');
if(strcmp(ss,'yes')==1)
rot=1;
end
close all
end
if rot==1
Img=imrotate(Img,-90);
end
Img=imresize(Img,[360,480]);
if(k==1)
f = figure('position', [150 75 900 600]);
p = uipanel(f,'Position',[0.1 0.1 0.8 0.2]);
lower=0; upper=255;
c1 = uicontrol(p,'Style','slider','position',[40 60 370 20],'Min',0,'Max',255,'Value',lower);
c2 = uicontrol(p,'Style','slider','position',[40 10 370 20],'Min',0,'Max',255,'Value',upper);
c11 = uicontrol('Parent',p,'Style','text','Position',[10,60,20,20],...
'String','0');
c12 = uicontrol('Parent',p,'Style','text','Position',[420,60,20,20],...
'String','255');
c13 = uicontrol('Parent',p,'Style','text','Position',[210,80,30,20],...
'String','lower');
c21 = uicontrol('Parent',p,'Style','text','Position',[10,10,20,20],...
'String','0');
c22 = uicontrol('Parent',p,'Style','text','Position',[420,10,20,20],...
'String','255');
c23 = uicontrol('Parent',p,'Style','text','Position',[210,30,30,20],...
'String','upper');
but=uicontrol('parent',p,'Style','push','Position',[500,40,40,30],'String','OK','callback',{@Close});
global ending;
refe=[1,220,180,360];
lower=[0,0]; upper=[255,255];
for i=1:2
ending=0;
set(c1,'value',0);
set(c2,'value',255);
while ending==0
img=imresize(Img,[240,360]);
%img=img(:,refe(2*i-1):refe(2*i),:);
img1=rgb2gray(img); img2=img1;
lower(i)=get(c1,'value'); upper(i)=get(c2,'value');
img2=img2(:,:)<upper(i) & img2(:,:)>lower(i);
subplot('Position',[.1 .35 .4 .4]);
imshow(img);
subplot('Position',[.5 .35 .4 .4]);
imshow(img2);
pause(0.0001);
end
end
close all;
lowerleft=lower(1);
lowerright=lower(2);
upperleft=upper(1);
upperright=upper(2);
imshow(Img);
close all;
pause(1);
disp('now we need to determine the region of intersest of the left curve')
pause(1);
figure,imshow(Img);
disp('click on the upper left corner of the region')
[x1,y1]=ginput(1);
disp('click on the bottom right corner of the region')
[x2,y2]=ginput(1);
disp('now we need to determine the region of intersest of the right curve')
pause(1);
disp('click on the upper left corner of the region')
[x3,y3]=ginput(1);
disp('click on the bottom right corner of the region')
[x4,y4]=ginput(1);
pause(1)
disp('now we need to measure 10 cm from the image');
pause(1);
disp('click on the first line');
[x5,y5]=ginput(1);
disp('click on the second line');
[x6,y6]=ginput(1);
close all;
x=[x1,x2,x3,x4]; y=[y1,y2,y3,y4];
x=round(x); y=round(y);
close all
end
one_cm=abs(y6-y5)/10;
img1=rgb2gray(Img); img2=img1;
img2=img2(:,:)<upperright & img2(:,:)>lowerright;
img1=img1(:,:)<upperleft & img1(:,:)>lowerleft ;
mask1=zeros(360,480); mask2=mask1;
mask1(y(1):y(2),x(1):x(2))=1;
mask2(y(3):y(4),x(3):x(4))=1;
img1=img1&mask1;
img2=img2&mask2;
img=img1|img2;
se1 = strel('line',6,6);
se2 = strel('line',4,4);
erodedimg = imerode(img,se1);
dilatedimg = imdilate(erodedimg,se2);
finimg = imdilate(dilatedimg,se2);
first_curve_center_y=0;
second_curve_center_y=0;
for i=y(1):y(2)
for j=x(1):x(2)
if(finimg(i,j)==1)
first_curve_center_y=max(first_curve_center_y,i);
break
end
end
end
for i=y(3):y(4)
for j=x(3):x(4)
if(finimg(i,j)==1)
second_curve_center_y=max(second_curve_center_y,i);
break;
end
end
end
prev_first_curve_center_y=0;
prev_second_curve_center_y=0;
if(first_curve_center_y==0 || second_curve_center_y==0)
continue;
end
if(first_curve_center_y - prev_first_curve_center_y >5 && prev_first_curve_center_y~=0)
prev_first_curve_center_y=first_curve_center_y;
continue;
end
if(second_curve_center_y - prev_second_curve_center_y >5 && prev_second_curve_center_y~=0)
prev_second_curve_center_y=second_curve_center_y;
continue;
end
dis=abs(first_curve_center_y - second_curve_center_y)/one_cm;
disp(k)
disp(dis)
if(flag==1)
findistance=dis;
else
findistance=(dis+findistance*(flag-1))/flag;
end
flag=flag+1;
end
disp('The distance between the first curve and the second one in cm =')
disp(findistance);
The function Close.m:
function Close(source,ref)
global ending
ending=1;
end
Thanks a lot
  1 Comment
the cyclist
the cyclist on 18 Aug 2022
I can't really say specifically why your particular code is slower in one older version of MATLAB than another, but there was a graphics overhaul in MATLAB in version 2014b.

Sign in to comment.

Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!