Main Menu

Menu

Monday 30 May 2011

Detect Red, Green & Blue in RGB format

Color detection is one of the most important image/video processing. Imaging in how many applications the color detection can be useful or critical. I implemented this color detection in Matlab. Offcourse someone can implement it in OpenCV. Maybe this code will come in some days.
Follow this link and you can experiment the RGB format and the colors that it gives for different values:
RGB color tester.


%clear everything previous
clear all;

% read image from desktop ,enter your path and an image
rgbImage = imread('c:/users/cex/desktop/color.jpg');

% display original image in the first figure (array of figures 2x2)
subplot(2,2, 1);
imshow(rgbImage);
title('Original RGB Image');

% split the image into the three color bands
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);

% Maximize the window of figures
set(gcf, 'Position', get(0, 'ScreenSize'));

%RED DETECTION
redthreshold = 100;
greenThreshold =100;
blueThreshold = 100;
redMask = (redBand > redthreshold);
greenMask = (greenBand < greenThreshold);
blueMask = (blueBand < blueThreshold);

% Combine the masks to find where all 3 are "true"
redObjectsMask = uint8(redMask & greenMask & blueMask);
% Initialize to black
maskedrgbImage = uint8(zeros(size(redObjectsMask)));
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* redObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
subplot(2, 2, 2);
imshow(maskedrgbImage);
title('Red');

%GREEN DETECTION
% Threshold each color band.
redthreshold = 100;
greenThreshold =100;
blueThreshold = 100;
redMask = (redBand < redthreshold);
greenMask = (greenBand > greenThreshold);
blueMask = (blueBand < blueThreshold);


% Combine the masks to find where all 3 are "true."
greenObjectsMask = uint8(redMask & greenMask & blueMask);
% Initialize to black
maskedrgbImage = uint8(zeros(size(greenObjectsMask)));
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* greenObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* greenObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* greenObjectsMask;
subplot(2, 2, 3);
imshow(maskedrgbImage);
title('Green');

%BLUE DETECTION
redthreshold = 100;
greenThreshold =100;
blueThreshold = 100;
redMask = (redBand < redthreshold);
greenMask = (greenBand < greenThreshold);
blueMask = (blueBand > blueThreshold);


% Combine the masks to find where all 3 are "true."
blueObjectsMask = uint8(redMask & greenMask & blueMask);
% Initialize to black
maskedrgbImage = uint8(zeros(size(blueObjectsMask)));
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* blueObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* blueObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* blueObjectsMask;
subplot(2, 2, 4);
imshow(maskedrgbImage);
title('Blue');

Saturday 28 May 2011

Computers as Components: Principles of Embedded Computing Systems Design

This book was the first to bring essential knowledge on embedded systems technology and techniques under a single cover. This second edition has been updated to the state-of-the-art by reworking and expanding performance analysis with more examples and exercises, and coverage of electronic systems now focuses on the latest applications. Researchers, students, and savvy professionals schooled in hardware or software design, will value Wayne Wolf's integrated engineering design approach.

The second edition gives a more comprehensive view of multiprocessors including VLIW and superscalar architectures as well as more detail about power consumption. There is also more advanced treatment of all the components of the system as well as in-depth coverage of networks, reconfigurable systems, hardware-software co-design, security, and program analysis. It presents an updated discussion of current industry development software including Linux and Windows CE. The new edition's case studies cover SHARC DSP with the TI C5000 and C6000 series, and real-world applications such as DVD players and cell phones.
Preview this Book

Embedded programming with the Microsoft .NET Micro Framework


Get the information you need for programming applications in the rich, managed-code environment of the Microsoft .NET Micro Framework. You'll learn how to extend your experience with the .NET Framework and Microsoft Visual C# through real-world examples, expert insights, and code samplesand efficiently build robust applications for the smallest devices.Discover how to: Use an object-oriented approach for programming embedded devices Create input and output port objects Develop detailed text and graphical displays that support complex user interactions Add Windows SideShow functionality into your application Implement functionality from existing applications to embedded applications Bind physical hardware events to Windows Presentation Foundation elements Establish embedded-network connections using TCP/IP Use emulation techniques for rapid-prototyping, experimentation, testing, and debugging Optimize performance of resource-constrained devicesPLUSGet code samples in Visual C# on the Web.
There is no Preview for this Book.

Embedded signal processing with the Micro Signal Architecture


This is a real-time digital signal processing textbook using the latest embedded Blackfin processor Analog Devices, Inc (ADI).  20% of the text is dedicated to general real-time signal processing principles.  The remaining text provides an overview of the Blackfin processor, its programming, applications, and hands-on exercises for users.  With all the practical examples given to expedite the learning development of Blackfin processors, the textbook doubles as a ready-to-use user's guide.   The book is based on a step-by-step approach in which readers are first introduced to the DSP systems and concepts.  Although, basic DSP concepts are introduced to allow easy referencing, readers are recommended to complete a basic course on "Signals and Systems" before attempting to use this book.  This is also the first textbook that illustrates graphical programming for embedded processor using the latest LabVIEW Embedded Module for the ADI Blackfin Processors. A solutions manual  is available for adopters of the book from the Wiley editorial department.
Preview this Book

Embedded media processing


A key technology enabling fast-paced embedded media processing developments is the high-performance, low-power, small-footprint convergent processor, a specialized device that combines the real-time control of a traditional microcontroller with the signal processing power of a DSP. This practical guide is your one-stop shop for understanding how to implement this cutting-edge technology. You will learn how to: * Choose the proper processor for an application. * Architect your system to avoid problems at the outset. * Manage your data flows and memory accesses so that they line up properly * Make smart-trade-offs in portable applications between power considerations and computational performance. * Divide processing tasks across multiple cores. * Program frameworks that optimize performance without needlessly increasing programming model complexity. * Implement benchmarking techniques that will help you adapt a framework to best fit a target application, and much more! Covering the entire spectrum of EMP-related design issues, from easy-to-understand explanations of basic architecture and direct memory access (DMA), to in-depth discussions of code optimization and power management, this practical book will be an invaluable aid to every engineer working with EMP, from the beginner to the seasoned expert. * Comprehensive subject coverage with emphasis on practical application * Essential assembly language code included throughout text and on CD-ROM * Many real-world examples using Analog's popular Blackfin Processor architecture
Preview this Book

Programming embedded systems in C and C++


Embedded software is in almost every electronic device designed today. There is software hidden away inside our watches, microwaves, VCRs, cellular telephones, and pagers; the military uses embedded software to guide smart missiles and detect enemy aircraft; communications satellites, space probes, and modern medicine would be nearly impossible without it. Of course, someone has to write all that software, and there are thousands of computer scientists, electrical engineers, and other professionals who actually do. Each embedded system is unique and highly customized to the application at hand. As a result, embedded systems programming is a widely varying field that can take years to master. However, if you have some programming experience and are familiar with C or C++, you're ready to learn how to write embedded software. The hands-on, no-nonsense style of this book will help you get started by offering practical advice from someone who's been in your shoes and wants to help you learn quickly. The techniques and code examples presented here are directly applicable to real-world embedded software projects of all sorts. Even if you've done some embedded programming before, you'll still benefit from the topics in this book, which include: Testing memory chips quickly and efficiently Writing and erasing Flash memory Verifying nonvolatile memory contents with CRCs Interfacing to on-chip and external peripherals Device driver design and implementation Optimizing embedded software for size and speed So whether you're writing your first embedded program, designing the latest generation of hand-held whatchamacalits, or simply managing the people who do, this book is for you.
Preview this Book

Wednesday 25 May 2011

Video Edge Detection In Matlab

This is the code (c+p in the matlab command line):

vid = videoinput('winvideo')
set(vid,'TriggerRepeat',Inf);
vid.FrameGrabInterval = 5;
vid_src = getselectedsource(vid);
set(vid_src,'Tag','motion detection setup');
figure;
start(vid)
while(vid.FramesAcquired<=inf)
data = getdata(vid,2);
diff_im = imadd(data(:,:,:,1),-data(:,:,:,2));
diff_im = edge(rgb2gray(diff_im),'sobel');
imshow(diff_im);
end
stop(vid)

Edge Detection in Matlab through Visual Studio 2010

Here we will see how someone can execute matalb instructions through Visual Studio 2010 (C#).
First of all you have to open a Console Application project in VS2010. To execute this code you have to have installed Matlab in your PC. Also, you have to include the :
using System.Reflection;
and finally save on your desktop (or whereever you like) a JPEG image and load it from there (you'll need the whole path of your image).


The code below in C# is leading in the same results as executing in the Matlab Command line the following:
I = rgb2gray(imread('C:\users\cex\desktop\1.jpg'));imshow(I);O = edge(I,'sobel');imshow(O);
Processed JPEG

Original JPEG







So, the code in C#, in the Console Application will be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace matlab1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get the type info

            Type matlabtype;
            matlabtype = Type.GetTypeFromProgID("matlab.application");

            //Create an instance of MATLAB

            object matlab;
            matlab = Activator.CreateInstance(matlabtype);

            //Prepare input as an object

            object[] arrayInput = new Object[] { "I = rgb2gray(imread('C:/users/cex/desktop/1.jpg'));imshow(I);O = edge(I,'sobel');imshow(O);" };

            //Call MATLAB method

            matlabtype.InvokeMember("Execute", BindingFlags.InvokeMethod, null, matlab, arrayInput);

            Console.ReadKey();


        }
    }
}
debugging this piece of code we will take open the matlab command window and the processed figure as shown below:

Monday 23 May 2011

Image Dilation & Erosion using OpenCV

In this example I am using the OpenCV libraries from the Visual Studio 2010 programming in C++.
In this application we will erode and dilate an image. The code is clear and commented.

#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "cxcore.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
        int iterations=1;

       //initialise the 3 images
      
        IplImage* source_image = NULL;
        IplImage* dilated_image = NULL;
        IplImage* eroded_image = NULL;

        //create 3 windows and their names
        cvNamedWindow("Source Image", 1);
        cvNamedWindow("Dilated Image",1);
        cvNamedWindow("Eroded Image",1);

        //load original image
        //if the name of your project is pr1
        //put a JPEG image in the pr1/pr1 folder
        //that the vs2010 will create
        source_image = cvLoadImage("1.jpg",1);
        cvShowImage( "Source Image", source_image );

        //make a copy of the original image
        dilated_image=cvCloneImage( source_image );
        eroded_image=cvCloneImage( source_image );

        //dilate image
        cvDilate(source_image,dilated_image,NULL,iterations);

        //erode image
        cvErode(source_image,eroded_image,NULL,iterations);

        //Present the processed images
        cvShowImage( "Dilated Image", dilated_image );
        cvShowImage( "Eroded Image", eroded_image );

        //Waits for a pressed key (0 delay)
        cvWaitKey(0);

        //destroys the window with the given name
        cvDestroyWindow( "Source Image" );
        cvDestroyWindow( "Dilated Image" );
        cvDestroyWindow( "Eroded Image" );

        //Deallocates the image header and the image data
        cvReleaseImage( &source_image );
        cvReleaseImage( &dilated_image );
        cvReleaseImage( &eroded_image );

        return 0;
}

The output of the above code:



4 iterations applied
One iteration applied

Sunday 22 May 2011

Call Matlab from Visual C# 2010

First of all you have to include:
using System.Diagnostics;

and then add a button:







Double clicking the new button Matlab, the automated code in red will be produced and it is time to add the following code (in blue):

        private void button16_Click(object sender, EventArgs e)
        {

            Process myProcess = new Process();
            //Here you use your own application with full path
            myProcess.StartInfo.FileName = @"C:\Program Files\MATLAB\R2010a\bin\matlab.exe";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
        }

and that is all folks.

Sunday 15 May 2011

A little of history: Intel 4004

The Intel 4004 was a 4-bit central processing unit (CPU) released by Intel Corporation in 1971. It was the first complete CPU on one chip, and also the first commercially available microprocessor.
(http://en.wikipedia.org/wiki/Intel_4004)

Background Subtraction Using OpenCV libraries and DevC++ Compiler

I implemented the background Subtraction using the ready functions of the OpenCV.
From the code below you can notice details in the code. Also, I implement the background Subtraction
using a two stage of erosion and dilation in the end of the processing.


#ifdef _CH_
#pragma package <opencv>
#endif

#define CV_NO_BACKWARD_COMPATIBILITY

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif

IplImage *image = 0, *frameTime1=0, *frameTime2=0, *frameForeground=0, *img1=0, *img2=0;

int main( int argc, char** argv )
{
    printf("Press ESC to Close.\n");
    CvCapture* capture = 0;    //Video capturing structure
    capture = cvCaptureFromCAM( -1 );    //Initializes capturing a video from a camera   
    if( !capture )
    {
        fprintf(stderr,"Could not initialize capturing...\n");
        return -1;
    }
    cvNamedWindow( "Camera", 1 );     //create the window for the Camera Output (Directly)
    cvNamedWindow( "frameForeground", 1 );
    while(1)
    {
        IplImage* frame = 0;   //every time create/initialize an image (which name is frame) to process
        int  c;                //integer to exit program
        frame = cvQueryFrame( capture );   //grabs and returns a frame from a camera input
       
        if( !frame )    //if there is no frame exit the while(1)
            break;
        if( !image )    //if there is no image, do the followings
        {
            /* allocate all the buffers */
            image = cvCreateImage( cvGetSize(frame), 8, 3 ); 
            frameTime1 = cvCreateImage( cvGetSize(frame), 8, 1 );
            frameTime2 = cvCreateImage( cvGetSize(frame), 8, 1 );  
            frameForeground = cvCreateImage( cvGetSize(frame), 8, 1 );
            img1 = cvCreateImage( cvGetSize(frame), 8, 1 );  
            img2 = cvCreateImage( cvGetSize(frame), 8, 1 );    
        }
        cvCopy( frame, image, 0 );  
        cvCvtColor( image, img1, CV_BGR2GRAY ); 
        cvCopy( img1, frameTime1, 0 );    //currently frame in grayscale
        cvAbsDiff(
                  frameTime1,
                  frameTime2,
                  frameForeground
                  );
                 
        cvThreshold(
                  frameForeground,
                  frameForeground,
                  10,
                  255,
                  CV_THRESH_BINARY);       
        cvErode(
                frameForeground,
                frameForeground,
                0,
                1);     
        cvDilate(
                frameForeground,
                frameForeground,
                0,
                1);      
        cvDilate(
                frameForeground,
                frameForeground,
                0,
                1);      
        cvErode(
                frameForeground,
                frameForeground,
                0,
                1);
        cvShowImage( "Camera", image );  //displays the image in the specified window 
        cvShowImage( "frameForeground", frameForeground );

        cvCopy( frameTime1, frameTime2, 0 );

        c = cvWaitKey(10);     //waits for a pressed key
        if( (char) c == 27 )  //if key==ESC (27 ESC button) then break
            break;
    }
    cvReleaseCapture( &capture );  //Releases the CvCapture
structure
    cvDestroyWindow("Camera");
    cvDestroyWindow("frameForeground");
    return 0;
}
#ifdef _EiC
main(1,"camshiftdemo.c");
#endif



Snapshots of the processed video:

Image/Video Processing using OpenCV and DevC++ Compiler

Follow OpenCV Installation Guide http://opencv.willowgarage.com/wiki/InstallGuide (more general instructions)
and find out how to configure Dev-Cpp under windows for compiling C programs using the OpenCV Library
(http://opencv.willowgarage.com/wiki/DevCpp).
 From the main page you can access helpful documentation for using effectively the libraries.
Very useful also is the book :

Learning OpenCV: Computer Vision with the OpenCV Library

 Be careful is a little tricky to make it work but I am sure that in the end you will make it.

Once you are done it's time to play! I am sure that you are gona find it very easy and handy to use!

P.S. You can also make the OpenCV work with other compilers (like Visual Studio, Eclipse etc)!

Read WebCam in Matlab

The following code inputs the WebCam stream to Matlab in a new figure:

video = videoinput('winvideo')
set(video,'TriggerRepeat',Inf);
video.FrameGrabInterval = 5;
vid_src = getselectedsource(video);
set(vid_src,'Tag','motion detection setup');
figure;
start(video)
while(video.FramesAcquired<=inf)
data = getdata(video,2);
diff_im = imadd(data(:,:,:,1),-data(:,:,:,2));
imshow(diff_im);
end
stop(video)

Search each command in Matlab for more details (help command)!



or just

video1 = videoinput('winvideo');
preview(video1);

to preview the input:





Edge Detection using Sobel Method in Simulink

Processing a video/image using Simulink is an easy job, especially when compared to
real-time processing of embedded DSP systems. By knowing the functionality of each block
you can reach the disired result quickly.
An example is following (using Matlab's Simulink):