Tutorial:Inroduction to OpenCv
If you want to use OpenCV with Visual Studio than follow my Previous post “Tutorial:How to Build OpenCV in VS 2008“
Some Important Command:
Allocate an image:
IplImage* cvCreateImage(CvSize size, int depth, int channels);
size: cvSize(width,height);
depth: pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F
channels: Number of channels per pixel. Can be 1, 2, 3 or 4. The channels are interleaved. The usual data layout of a color image is b0 g0 r0 b1 g1 r1 …
Examples:
// Allocate a 1-channel byte image of 640 X 480 resolution.
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
Release an image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);//creates image
cvReleaseImage(&img);// releases image
Set/get the region of interest:
void cvSetImageROI(IplImage* image, CvRect rect);
void cvResetImageROI(IplImage* image);
vRect cvGetImageROI(const IplImage* image);
Accessing image elements
-
Assume that you need to access the k-th channel of the pixel at the i-th row and j-th column. The row index i is in the range [0, height-1]. The column index j is in the range [0, width-1]. The channel index kis in the range [0, nchannels-1].
-
For a single-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
CvScalar s;
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf(“intensity=%fn”,s.val[0]);
s.val[0]=111;
cvSet2D(img,i,j,s); // set the (i,j) pixel value
-
For a multi-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
CvScalar s;
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf(“B=%f, G=%f, R=%fn”,s.val[0],s.val[1],s.val[2]);
s.val[0]=111;
s.val[1]=111;
s.val[2]=111;
cvSet2D(img,i,j,s); // set the (i,j) pixel value
-
Direct access:
-
For a multi-channel byte image:
-
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
Note : If float image than replace uchar by float
Simple Program:
RGB to Grayscale And than Binary:
//loading a 3 channel bgr image
IplImage *im_rgb = cvLoadImage(“image.jpg”);
//image.jpg should be copied in ur project folder.
//creating a 1 channel grayscale image
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
//converting bgr image to grayscale image.
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);
//creating a single channel image for using it as a binary image.
IplImage* img_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
//Thresholding and converting the grayscale image into a binary image
cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//saving the image
cvSaveImage(“image_bw.jpg”,img_bw);
Use your Webcam to get image:
#include "stdafx.h"
#include <cxcore.h>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <stdio.h>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
int key = 0;
//capturing image from the camera
CvCapture* capture = cvCaptureFromCAM(0);
//if image is not captured properly an error msg is displayed
if(!cvQueryFrame(capture)){
cout<<"Video capture failed, please check the camera."<<endl;
}
else{
cout<<"Video camera capture successful!"<<endl;
};
//getting the size of the captured image. cvQueryFrame() retrieves the image
CvSize sz = cvGetSize(cvQueryFrame(capture));
IplImage* image = cvCreateImage(sz, 8, 3);
//Create Window to display image
cvNamedWindow("Image Source",1);
printf( “Hot keys: n”
“tq- quit the programn”);
while( key != ‘q’ )
{
image = cvQueryFrame(capture);//get image from webcam”e.g.getdata of matlab”
cvShowImage("Image Source", image);//show image in Image Source window
key=cvWaitKey(1);
}
cvReleaseImage(&image);
cvReleaseCapture(&capture);
cvDestroyAllWindows();
}