ตัวอย่างโค๊ด Digital image Processing ด้วย VC++ (OpenCV)

การทำ Gray Scale, Thresholding, Edge Detection, และ Dilation โดยการใช้ VC++
// header ที่ต้องประกาศเพิ่ม
#include “stdafx.h”
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BOOL CtestwwwDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// ประกาศตัวแปรภาพ
IplImage* NewImg;
IplImage* GlayImg;
IplImage* NewImg1;
IplImage* NewImg2;
IplImage* GlayImg1;
IplImage* GlayImg2;
    IplImage* edge;
CvCapture* CAP = cvCaptureFromCAM(0);
cvSetCaptureProperty(CAP,CV_CAP_PROP_FRAME_HEIGHT,350);
cvSetCaptureProperty(CAP,CV_CAP_PROP_FRAME_WIDTH,350);
CvCapture* CAP1 = cvCaptureFromCAM(1);
cvSetCaptureProperty(CAP1,CV_CAP_PROP_FRAME_HEIGHT,350);
cvSetCaptureProperty(CAP1,CV_CAP_PROP_FRAME_WIDTH,350);
do
{
NewImg = cvQueryFrame(CAP);
// แปลงภาพเป็น Gray scale ของกล้องที่ 1
GlayImg = cvCreateImage(cvSize(NewImg->width,NewImg->height),IPL_DEPTH_8U, 1);
cvCvtColor(NewImg, GlayImg, CV_BGR2GRAY);
// Thresholding ของกล้องที่ 1
                cvThreshold(GlayImg, GlayImg, 65, 130, CV_THRESH_BINARY);
// Canny Detection ของกล้องที่ 1
cvCanny(GlayImg, GlayImg, (float)1, (float)1*3, 3);
// Dilation ของกล้องที่ 1
cvDilate(GlayImg, GlayImg, 0, 18);
NewImg1 = cvQueryFrame(CAP1);
// แปลงภาพเป็น Gray scale ของกล้องที่ 2
GlayImg1 = cvCreateImage(cvSize(NewImg1->width,NewImg1->height),IPL_DEPTH_8U, 1);
cvCvtColor(NewImg1, GlayImg1, CV_BGR2GRAY);
// Thresholding ของกล้องที่ 2
cvThreshold(GlayImg1, GlayImg1, 65, 130, CV_THRESH_BINARY);
// Canny Detection ของกล้องที่ 2
                cvCanny(GlayImg1, GlayImg1, (float)1, (float)1*3, 3);
// Dilation ของกล้องที่ 2
                cvDilate(GlayImg1, GlayImg1, 0, 18);
// แสดงภาพ
cvShowImage(“NewImg”, NewImg);
cvShowImage(“GlayImg”, GlayImg);
cvShowImage(“NewImg1″, NewImg1);
cvShowImage(“GlayImg1″, GlayImg1);
} while ( cvWaitKey( 1 ) == -1 );
cvDestroyWindow(“NewImg”);
cvDestroyWindow(“GlayImg”);
cvReleaseImage( &NewImg );
cvReleaseImage( &GlayImg );
cvDestroyWindow(“NewImg1″);
cvDestroyWindow(“GlayImg1″);
cvReleaseImage( &NewImg1 );
cvReleaseImage( &GlayImg1 );
return TRUE;  // return TRUE  unless you set the focus to a control
}
ตรวจสอบหาสี่เหลี่ยมในภาพ (Squares Detection using VC++2008)
#include “stdafx.h”
#include <stdio.h>
#include <math.h>
#include <string.h>
int thresh = 50;
IplImage* img = 0;
IplImage* img0 = 0;
CvMemStorage* storage = 0;
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
double dx1 = pt1->x – pt0->x;
double dy1 = pt1->y – pt0->y;
double dx2 = pt2->x – pt0->x;
double dy2 = pt2->y – pt0->y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
{
CvSeq* contours;
int i, c, l, N = 11;
CvSize sz = cvSize( img->width & -2, img->height & -2 );
IplImage* timg = cvCloneImage( img ); // make a copy of input image
IplImage* gray = cvCreateImage( sz, 8, 1 );
IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
IplImage* tgray;
CvSeq* result;
double s, t;
CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));
cvPyrDown( timg, pyr, 7 );
cvPyrUp( pyr, timg, 7 );
tgray = cvCreateImage( sz, 8, 1 );
for( c = 0; c < 3; c++ )
{
cvSetImageCOI( timg, c+1 );
cvCopy( timg, tgray, 0 );
for( l = 0; l < N; l++ )
{
if( l == 0 )
{
cvCanny( tgray, gray, 0, thresh, 5 );
cvDilate( gray, gray, 0, 1 );
}
else
{
cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
}
cvFindContours( gray, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
while( contours )
{
result = cvApproxPoly( contours, sizeof(CvContour), storage,
CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
if( result->total == 4 &&
fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 1000 &&
cvCheckContourConvexity(result) )
{
s = 0;
printf(“ciclo for annidato fino a 5\t\n”);
for( i = 0; i < 5; i++ )
{
if( i >= 2 )
{
t = fabs(angle(
(CvPoint*)cvGetSeqElem( result, i ),
(CvPoint*)cvGetSeqElem( result, i-2 ),
(CvPoint*)cvGetSeqElem( result, i-1 )));
s = s > t ? s : t;
}
}
if( s < 0.3 )
for( i = 0; i < 4; i++ )
cvSeqPush( squares,
(CvPoint*)cvGetSeqElem( result, i ));
}
contours = contours->h_next;
}
}
}
cvReleaseImage( &gray );
cvReleaseImage( &pyr );
cvReleaseImage( &tgray );
cvReleaseImage( &timg );
return squares;
}
void drawSquares( IplImage* img, CvSeq* squares )
{
CvSeqReader reader;
IplImage* cpy = cvCloneImage( img );
int i;
cvStartReadSeq( squares, &reader, 0 );
for( i = 0; i < squares->total; i += 4 )
{
CvPoint pt[4], *rect = pt;
int count = 4;
CV_READ_SEQ_ELEM( pt[0], reader );
CV_READ_SEQ_ELEM( pt[1], reader );
CV_READ_SEQ_ELEM( pt[2], reader );
CV_READ_SEQ_ELEM( pt[3], reader );
cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
}
cvSaveImage(“squares.jpg”,cpy);
cvReleaseImage( &cpy );
}
int _tmain(int argc, _TCHAR* argv[])
{
int key = 0;
IplImage* frame =0;
IplImage* squares=0;
storage = cvCreateMemStorage(0);
CvCapture *camera = cvCreateCameraCapture(CV_CAP_ANY);
frame = cvQueryFrame(camera);
frame = cvQueryFrame(camera);
frame = cvQueryFrame(camera);
while(key!=’q'){
frame = cvQueryFrame(camera);
frame = cvQueryFrame(camera);
if(frame!=NULL){
printf(“Got frame\t\n”);
cvSaveImage(“frame.jpg”, frame);
/*img0*/ img = cvLoadImage(“frame.jpg”);
//img = cvCloneImage( img0 );
cvNamedWindow( “img0″, CV_WINDOW_AUTOSIZE);
cvShowImage(“img0″,/*img0*/img);
drawSquares( img, findSquares4( img, storage ) );
squares = cvLoadImage(“squares.jpg”);
cvNamedWindow( “main”, CV_WINDOW_AUTOSIZE);
cvShowImage(“main”, squares);
/* wait for key.
Also the function cvWaitKey takes care of event processing */
key = cvWaitKey(0);
}
}
cvReleaseImage( &img );
cvReleaseImage( &img0 );
cvReleaseCapture(&camera);
cvDestroyWindow(“main”);
cvDestroyWindow(“img0″);
cvClearMemStorage( storage );
return 0;
}
การทำ Disparity Map ในงาน Stereo Matching
/*———————————————————————————*/
IplImage* srcLeft = cvLoadImage(“D:/img_Right22.jpg”,1);
IplImage* srcRight = cvLoadImage(“D:/img_Right24.jpg”,1);
IplImage* leftImage = cvCreateImage(cvGetSize(srcLeft), IPL_DEPTH_8U, 1);
IplImage* rightImage = cvCreateImage(cvGetSize(srcRight), IPL_DEPTH_8U, 1);
IplImage* depthImage = cvCreateImage(cvGetSize(srcRight), IPL_DEPTH_8U, 1);
cvCvtColor(srcLeft, leftImage, CV_BGR2GRAY);
cvCvtColor(srcRight, rightImage, CV_BGR2GRAY);
CvSize size = cvGetSize(leftImage);
CvMat* disparity_left = cvCreateMat( size.height, size.width, CV_16S );
CvMat* disparity_right = cvCreateMat( size.height, size.width, CV_16S );
CvStereoGCState* state = cvCreateStereoGCState( 16, 2 );
cvFindStereoCorrespondenceGC( leftImage, rightImage, disparity_left, disparity_right, state, 0 );
cvReleaseStereoGCState( &state );
CvMat* disparity_left_visual = cvCreateMat( size.height, size.width, CV_8U );
cvConvertScale( disparity_left, disparity_left_visual, -16 );
//cvSave( “disparity.bmp”, rightImage );
cvNamedWindow(“disparity_left”);
cvShowImage(“disparity_left”, disparity_left_visual);

Comments

Popular Posts