Create a Photoviewer with GUI

Code :


clc;
imr=input('Enter the full path of the image frames:','s');
somefolder=imr;
filelist=dir(somefolder);
for i=1:size(filelist,1)
    if filelist(i).isdir ~=true
        fname=filelist(i).name;
        if strcmp(fname(size(fname,2)-3:size(fname,2)),'.jpg')==1
            tmp=imread([somefolder fname]);
            if size(tmp,3)==3;
                tmp=imresize(tmp,[400,400],'bilinear');
                imshow(tmp);
                pause(1);
                disp([fname,':loaded']);
            end
        end
    end
end

Building GUI :

If you dont know how create basic matlab GUI then you can see my previous post :

Building GUI with Matlab

Step 1 : I have made a very simple interface,drawn just a button  and an axes



Step 2 : Now open the GUI .m file by clicking view and then m-file editor



Step 3: Now paste the above code in the pushbutton_callback function and save

Step 4: Now run the code,press play button,then it should ask for the image folder path,write the path press          enter it will show the images

Vehicle Counting System program




1. Input a video of vehicles ex. test.avi
2. Now extract the frames and save in a specific folder
    Get the code here, one of my previous post
3. Detect the motion of the vehicle in the video
    Get the code here

Now if everything has gone alright,we can begin.

We just make very simple changes in the previous code to make the thing work perfectly.

Step 1 : Watch the redbox around the moving object.Now insert this code in there

             rectangle('Position',cen,'EdgeColor','r','LineWidth',5);
   
             Here 'cen' is the centroid

Step 2: Now the most important step,insert this code after that

        txt=text(no, strcat('car: ', num2str(round(no))));
        set(txt, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'black');

It will display the number of cars. 'no' is a variable which is set to '0' at first.But when ever a motion is detected in the video 'no' will be incremented by 1,that is how we get the vehicle count.

But the basic problem what you will face in this method that is,here we are considering frame after frame,so the 'no' variable gets incremented each time,but thats not what we want,we want the actual count,so we are gonna need a FLAG variable which indicates if 'no' is incremented by 1 for a particular vehicle then it wont increment for the specific one again.

like this,
FLAG=0;
if FLAG==0
   no=no+1;
   flag=1;
   frame=frame+1;
end
else
   frame=frame+1;
end

Face detecttion and count people program

close all;
clear all;
clc;
rgbInputImage = imread('photo.jpg');
labInputImage = applycform(rgbInputImage,makecform('srgb2lab'));
Lbpdfhe = fcnBPDFHE(labInputImage(:,:,1));
labOutputImage = cat(3,Lbpdfhe,labInputImage(:,:,2),labInputImage(:,:,3));
rgbOutputImage = applycform(labOutputImage,makecform('lab2srgb'));
figure, imshow(rgbInputImage);
figure, imshow(rgbOutputImage);
img=rgbOutputImage;
final_image = zeros(size(img,1), size(img,2));
if(size(img, 3) > 1)
for i = 1:size(img,1)
for j = 1:size(img,2)
R = img(i,j,1);
G = img(i,j,2);
B = img(i,j,3);
if(R > 92 && G > 40 && B > 20)
v = [R,G,B];
if((max(v) - min(v)) > 15)
if(abs(R-G) > 15 && R > G && R > B)
final_image(i,j) = 1;
end
end
end
end
end
end
binaryImage=im2bw(final_image,0.6);
figure, imshow(binaryImage);
binaryImage = imfill(binaryImage, 'holes');
figure, imshow(binaryImage);
binaryImage = bwareaopen(binaryImage,1890);
figure,imshow(binaryImage);
labeledImage = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, final_image, 'all');
numberOfPeople = size(blobMeasurements, 1)
imagesc(rgbInputImage); title('Outlines, from bwboundaries()');
hold on;
boundaries = bwboundaries(binaryImage);
for k = 1 : numberOfPeople
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
imagesc(rgbInputImage);
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople
thisBlobsBox = blobMeasurements(k).BoundingBox;
x1 = thisBlobsBox(1);
y1 = thisBlobsBox(2);
x2 = x1 + thisBlobsBox(3);
y2 = y1 + thisBlobsBox(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'LineWidth', 2);
end                          



fcnBPDFHE() function should be added

avialable in mathworks website link is here :