Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

i want a c code for the following program . can any one i do it for me.

Asked by sarala on 28 Feb 2013
clear all;
close all;
clc;
e=2
for e=1:e
vide01 = videoinput('winvideo',1,'YUY2_640x480')
avi011 = avifile('webf330.avi')% it creates an AVIFILE object AVIOBJ with the default parameter values.
vide01.loggingMode = 'disk&memory' 
vide01.Disklogger = avi011
vide01.TriggerRepeat = 1
preview(vide01) %PREVIEW Activate a live video preview window for video input object OBJ.
start(vide01)   %START(OBJ) starts the timer running, represented by the timer object, OBJ.
%t = timer('TimerFcn',@mycallback, 'Period', 1.0);
wait(vide01)    %WAIT(OBJ) blocks the MATLAB command line and waits until the 
%                          timer, represented by the timer object OBJ, stops running. 
img= getsnapshot(vide01)
closepreview(vide01)
if e<=1
imwrite(img,'imgr.jpg')
imgaa=imread('imgr.jpg')
figure(102),imshow(imgaa)
end 
% img=imread('F:\Documents and Settings\Semisys.SEMISYS-0D0A654\Desktop\cars\roadcar2.jpg');
% img=imresize(img,1.2);% B = IMRESIZE(A,M,METHOD) returns an image that is M times the
%                           size of A. If M is between 0 and 1.0, B is smaller than A. If
%                           M is greater than 1.0, B is larger than A. 
figure(1);imshow(img);
img11=rgb2gray(img)
figure(100),imshow(img11)
imge11=im2bw(img11)
figure(101),imshow(imge11)
% if e<=1
% imwrite(imge11,'imgr.png')
% imgaa=imread('imgr.png')
% figure(102),imshow(imgaa)
% end    
% imgaa=imread('imgr.png')
% imwrite(I1, F:\Documents and Settings\Semisys.SEMISYS-0D0A654\Desktop\saveimage\empty.png)
% figure(102),imshow(I1)
% imsave(h)
% [img2, user_canceled] = imsave(…)
% background = imopen(img,strel('disk',15));
b(:,:)=img(:,:,1);
% imview(r)
g(:,:)=img(:,:,2);
% imview(g)
r(:,:)=img(:,:,3);
% imview(b)
r1=medfilt2(r,[9 9]);%B = MEDFILT2(A,[M N]) performs median filtering of the matrix A in two dimensions.
% imview(r1)
g1=medfilt2(g,[9 9]);
% imview(g1)
b1=medfilt2(b,[9 9]);
% imview(b1)
%r1=medfilt2(r);
%g1=medfilt2(g);
%b1=medfilt2(b);
img1=cat(3,r1,g1,b1);%CAT Concatenate arrays.CAT(DIM,A,B) concatenates the arrays A and B along the dimension DIM.
figure(2);
imshow(img1);
title('After filter');
I=rgb2hsv(img1);% RGB2HSV Convert red-green-blue colors to hue-saturation-value.
% imview(I)
%                    H = RGB2HSV(M) converts an RGB color map to an HSV color map.
h1(:,:)=I(:,:,1);
figure(3);
imshow(h1);
title('Hue component');
figure(4);
imhist(h1);
% imsteq(g);
%IMHIST Display histogram of image data.
%            IMHIST(I) displays a histogram for the intensity image I whose number of
%            bins are specified by the image type.  If I is a grayscale image, IMHIST
%           uses 256 bins as a default value. If I is a binary image, IMHIST uses
%           only 2 bins.
s1(:,:)=I(:,:,2);
figure(5);
imshow(s1);
title('saturation  component');
figure(6);
imhist(s1);
% imsteq(s1);
[m n]=size(h1);%[M,N] = SIZE(X) for matrix X, returns the number of rows and 
%                        columns in X as separate output variables. 
for x=1:m
   for y=1:n
        if 0.3<h1(x,y)<0.75
            hT(x,y)=1;%it is a object pixel
        else
            hT(x,y)=0;%it is a non-object pixel
        end
   end
end
figure(7);
imshow(hT);
title('Hue threshold');
for x=1:m
    for y=1:n
        if 0.295<s1(x,y)<0.52
            sT(x,y)=1;%it is a object pixel
        else
            sT(x,y)=0;%it is a non-object pixel
        end
    end
end
figure(8);imshow(sT);
title('Saturation threshold');
for x=1:m
    for y=1:n
        if(hT(x,y)==1 & sT(x,y)==0)
            hsT(x,y)=1;%it is a object pixel
        else
            hsT(x,y)=0;%it is a non-object pixel
        end
    end
end
if((cat(3,sT(x,y),hsT(x,y))) == 0)
     figure(9);imshow(hsT);
     title(' Not Detected vehicle  image');
 else
figure(10);imshow(hsT);
title('Combining the thresholds');
end
se=strel('disk',2);%SE = STREL('disk',R,N) creates a flat disk-shaped structuring element
%                    with the specified radius, R.  R must be a nonnegative integer. 
imgo=imopen(hsT,se);%IM2 = IMOPEN(IM,SE) performs morphological opening on the grayscale
%                            or binary image IM with the structuring element SE.  SE must be a
%                             single structuring element object, as opposed to an array ofobjects.
figure(11);
imshow(imgo);
title('Open holes');
imgc=imclose(imgo,se);%IM2 = IMCLOSE(IM,SE) performs morphological closing on the
%                               grayscale or binary image IM with the structuring element SE.
figure(12);imshow(imgc);
title('Close holes');
imgfl=imfill(imgc,'holes');% BW2 = IMFILL(BW1,'holes') fills holes in the input image.  A hole is
%     a set of background pixels that cannot be reached by filling in the
%     background from the edge of the image.
figure(13);imshow(imgfl);
title('filling after close holes');
imgfl=bwareaopen(imgfl,650)
if e<=1
    imgf1=~imgfl
    B = bwboundaries(imgfl);
    figure(20),imshow(imgfl)
    text(10,10,strcat('\color{blue}EMPTY ROAD NO.OF VEHICLES FOUND IS:',num2str(length(B))))
    hold on
    for k = 1:length(B)
          boundary = B{k};
          plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 0.2)
      end
  %     imwrite(imgf1,'imgk.jpg')
  %     imga=imread('imgr.jpg')
  end
  imgf1=~imgfl
  B = bwboundaries(imgfl);
  % BWBOUNDARIES Trace region boundaries in binary image.
  %     B = BWBOUNDARIES(BW) traces the exterior boundary of objects, as well
  %     as boundaries of holes inside these objects. It also descends into the
  %     outermost objects (parents) and traces their children (objects 
  %     completely enclosed by the parents). BW must be a binary image where 
  %     nonzero pixels belong to an object and 0-pixels constitute the 
  %     background. B is a P-by-1 cell array, where P is the number of objects 
  %     and holes. Each cell contains a Q-by-2 matrix, where Q is the number of
  %     boundary pixels for the corresponding region. Each row of these Q-by-2
  %     matrices contains the row and column coordinates of a boundary pixel.
  %     The coordinates are ordered in a clockwise direction.
figure(14),imshow(imgfl)
text(10,10,strcat('\color{blue}ON ROAD NO.OF VEHICLES FOUND IS:',num2str(length(B))))
%  TEXT   Text annotation.
%     TEXT(X,Y,'string') adds the text in the quotes to location (X,Y)
%     on the current axes, where (X,Y) is in units from the current
%     plot. If X and Y are vectors, TEXT writes the text at all locations
%     given. If 'string' is an array the same number of rows as the
%     length of X and Y, TEXT marks each point with the corresponding row
%     of the 'string' array.
% STRCAT Concatenate strings.
%     COMBINEDSTR = STRCAT(S1, S2, ..., SN) horizontally concatenates strings
%     in arrays S1, S2, ..., SN. Inputs can be combinations of single
%     strings, strings in scalar cells, character arrays with the same number
%     of rows, and same-sized cell arrays of strings. If any input is a cell 
%     array, COMBINEDSTR is a cell array. Otherwise, COMBINEDSTR is a 
%     character array.
hold on
for k = 1:length(B)
%     LENGTH   Length of vector.
%     LENGTH(X) returns the length of vector X.  It is equivalent
%     to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.
      boundary = B{k};
      plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 0.2)
  end
  avi011= close(vide01.Disklogger)
  end
  figure(15),imshow(imgaa)
  figure(16),imshow(img)
  img33 = imgaa - img;
  figure(17),imshow(img33)

1 Comment

Yogesh Shukla on 3 Apr 2013

This program always displays "vehicle on empty road= 1" even there is nothing in video (just a plain surface). How can i correct it?

Thanks for this helpful program.

sarala

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 28 Feb 2013

No. The C language does not include any graphics, so it is not possible to turn that code into pure C.

0 Comments

Walter Roberson
Answer by sarala on 28 Feb 2013

i want a c code for the above program so that i can process it to dsp kit tms320c6713

2 Comments

Walter Roberson on 28 Feb 2013

I would have to research that particular DSP kit to see whether it supported video input at all. (Some kits do; I do not know that one.)

I am certain, though, that the dsp does not support imshow() or imhist() when called in that form, or plot() or title() or text().

Walter Roberson on 3 Apr 2013

That board is not documented as having any video peripherals.

http://www.ti.com/lit/ds/symlink/tms320c6713.pdf

sarala

Contact us