User Tools

Site Tools


李宗樺:我的專研練習

week 2-1 (2010.7.6)

使用OpenCV套件

1.加裝OpenCV套件到我們熟悉的Dev-C++環境。
2."工具→編譯器選項→編譯器→連結器命令列"下加入Linker參數 -lcv -lcxcore -lhighgui
3."工具→編譯器選項→目錄→二進位檔"下新增目錄D:\Program Files\Dev-Cpp\bin\opencv

OpenCV 2.1 參數
OpenCV-2.1.0-win32-vs2008.exe
add PATH
-lc:\opencv2.1\include -lc:\opencv2.1\include\opencv
-LC:\OpenCV2.1\lib -lcv210 -lcxcore210 -lhighgui210 -lglut32 -lglu32 -lgli32

-L"C:\opencv2.1\include" -L"C:\opencv2.1\include\opencv"
-L"C:\OpenCV2.1\lib" -lcv210 -lcxcore210 -lhighgui210

編譯器:
-Ic:\OpenCV2.1\include -Ic:\OpenCV2.1\include\opencv
連結器:
-LC:\OpenCV2.1\lib -lcv210 -lcxcore210 -lhighgui210 -lglut32 -lglu32 -lgdi32

秀圖範例

#include <opencv/highgui.h>
 
int main()
{
	IplImage * img = cvLoadImage("c:/image.jpg");  //圖片與位置自行決定
	cvNamedWindow("test");
	cvShowImage("test", img);
	cvWaitKey(0);
	return 0;
}

視訊版本

#include <opencv/highgui.h>
 
int main()
{
	IplImage * img ;
	CvCapture * cap = cvCreateCameraCapture(0);
	cvNamedWindow("test");
	 
	while(1)
	{
		img=cvQueryFrame(cap);
		cvShowImage("test",img);
		cvWaitKey(33);
	}
	//cvReleaseCapture(&cap);
	//2011.03.03新增 ↑
	return 0;
}

week 4-1 (2010.7.19)

顯示多圖1

#include <opencv/highgui.h>
int main()
{
	IplImage * img = cvLoadImage("c:/t.jpg");
	IplImage * img2 = cvLoadImage("c:/t2.jpg");
	IplImage * img3 = cvLoadImage("c:/t3.jpg");
	IplImage * img4 = cvLoadImage("c:/t4.jpg");
	IplImage * img6 = cvLoadImage("c:/t6.jpg");
 
	cvNamedWindow("test1");
	cvNamedWindow("test2");
	cvNamedWindow("test3");
	cvNamedWindow("test4");
	cvNamedWindow("test6");
	
	cvShowImage("test1", img);
	cvShowImage("test2", img2);
	cvShowImage("test3", img3);
	cvShowImage("test4", img4);
	cvShowImage("test6", img6);
	
	cvMoveWindow("test1",10,50);
	cvMoveWindow("test2",410,50);
	cvMoveWindow("test3",810,50);
	cvMoveWindow("test4",10,450);
	cvMoveWindow("test6",810,450);
		
	cvWaitKey(0);	
	return 0;
}

顯示多圖2

#include <opencv/highgui.h>
int main()
{
	int x[5]={10,410,810,10,810};
	int y[5]={50,50,50,450,450};
	const char* n[5]={"test1","test2","test3","test4","test6"};
	const char* file[5]={"c:/t1.jpg","c:/t2.jpg","c:/t3.jpg","c:/t4.jpg","c:/t6.jpg"};
	
	for(int i=0;i<5;i++)
	{
		IplImage * img = cvLoadImage(file[i]);
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		//cvResizeWindow(n[i],200,200);	
	}
		
	cvWaitKey(0);	
	return 0;
}

加入滑鼠座標位置

#include <opencv/highgui.h>
#include <stdio.h>

void mouse(int event, int x, int y, int flags, void* param)
{
	printf("now x: %d y: %d\n", x,y);
}

int main()
{
	int x[5]={10,410,810,10,810};
	int y[5]={50,50,50,450,450};
	const char* n[5]={"test1","test2","test3","test4","test6"};
	const char* file[5]={"c:/t1.jpg","c:/t2.jpg","c:/t3.jpg","c:/t4.jpg","c:/t6.jpg"};
	
	for(int i=0;i<5;i++)
	{
		IplImage * img = cvLoadImage(file[i]);
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		//cvResizeWindow(n[i],200,200);	
		cvSetMouseCallback(n[i], mouse);
	}
		
	cvWaitKey(0);	
	return 0;
}

移入圖片左上角即可關閉

#include <opencv/highgui.h>
#include <stdio.h>

void mouse(int event, int x, int y, int flags, void* param)
{
	printf("now x: %d y: %d => %s\n", x,y,(const char*)param);
	if(x<10 && y<10)
		cvDestroyWindow((const char*)param);	
}

int main()
{
	int x[5]={10,410,810,10,810};
	int y[5]={50,50,50,450,450};
	char n[5][10]={"test1","test2","test3","test4","test6"};
	char file[5][10]={"c:/t1.jpg","c:/t2.jpg","c:/t3.jpg","c:/t4.jpg","c:/t6.jpg"};
	
	for(int i=0;i<5;i++)
	{
		IplImage * img = cvLoadImage(file[i]);
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		//cvResizeWindow(n[i],200,200);	
		cvSetMouseCallback(n[i], mouse ,n[i]);
	}
	
	cvWaitKey(0);	
	return 0;
}

week 5-1 (2010.7.26)

3*3電視牆

#include <opencv/highgui.h>
#include <stdio.h>

void mouse(int event, int x, int y, int flags, void* param)
{
	printf("now x: %d y: %d => %s\n", x,y,(const char*)param);
	if(x>190 && y<10)
		cvDestroyWindow((const char*)param);	
}

int main()
{
	int x[9]={10,210,410,10,210,410,10,210,410};
	int y[9]={50,50,50,250,250,250,450,450,450};
	char n[9][10]={"test1","test2","test3","test4","test5","test6","test7","test8","test9"};
	char file[9][10]={"c:/t1.jpg","c:/t2.jpg","c:/t3.jpg","c:/t4.jpg","c:/t5.jpg","c:/t6.jpg","c:/t4.jpg","c:/t5.jpg","c:/t6.jpg"};
	int a=10,b=10;
	
	for(int i=0;i<3;i++)
	{
		//IplImage * img = cvLoadImage(file[i]);
		IplImage * img = cvLoadImage("c:/a.jpg");
		cvSetImageROI(img,cvRect(a+200*i,b,200,200));
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		cvResizeWindow(n[i],200,200);	
		cvSetMouseCallback(n[i], mouse ,n[i]);
	}
	
	for(int i=3;i<6;i++)
	{
		//IplImage * img = cvLoadImage(file[i]);	
		IplImage * img = cvLoadImage("c:/a.jpg");
		cvSetImageROI(img,cvRect(10+200*(i-3),b+200,200,200));
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		cvResizeWindow(n[i],200,200);	
		cvSetMouseCallback(n[i], mouse ,n[i]);
	}
	
	for(int i=6;i<9;i++)
	{
		//IplImage * img = cvLoadImage(file[i]);	
		IplImage * img = cvLoadImage("c:/a.jpg");
		cvSetImageROI(img,cvRect(a+200*(i-6),400,200,200));
		//cvSetImageROI(img,cvRect(x[i],y[i],200,200));
		cvNamedWindow(n[i]);
		cvShowImage(n[i], img);
		cvMoveWindow(n[i],x[i],y[i]);
		cvResizeWindow(n[i],200,200);	
		cvSetMouseCallback(n[i], mouse ,n[i]);	
	}	
	cvWaitKey(0);	
	return 0;
}

動態個數電視牆

//動態個數電視牆
//若分割視窗總大小超出圖片太多時,會有錯誤訊息
//也就是有任一個視窗沒有顯示出圖片時,會有錯誤 
#include <iostream>
#include <opencv/highgui.h>
using namespace std;
 
class Win{
public:
	char name[10];
	int x,y;
};
//主要更改下一行的值,表示橫的有幾個視窗,與直的有幾個視窗
const int Wcount=4, Hcount=3;
Win w[Wcount*Hcount];
void mouse(int event, int x, int y, int flags, void * param)
{
	char * WinName=(char*)param;
	cvDestroyWindow(WinName);
}
int main()
{
	IplImage * img=cvLoadImage("c:/a.jpg");
 
	for(int i=0;i<Wcount*Hcount;i++){
		sprintf(w[i].name, "%d", i);
		w[i].x=(i%Wcount)*(200+10);
		w[i].y=(i/Wcount)*(200+40);
		cvNamedWindow(w[i].name);
		cvSetImageROI(img,cvRect(w[i].x, w[i].y, 200,200) );
		cvShowImage(w[i].name,img);
		cvMoveWindow(w[i].name, w[i].x, w[i].y);
		cvResizeWindow(w[i].name, 200,200);
		cvSetMouseCallback(w[i].name, mouse, w[i].name);
	}
	cvWaitKey(0);
	return 0;
}

鍵盤控制畫面與視窗移動

#include <opencv/highgui.h>
#include <stdio.h>
 
int main()
{
	IplImage * img = cvLoadImage("c:/a.jpg");
	cvNamedWindow("a");
	cvShowImage("a", img);
	cvResizeWindow("a", 200,200);
	cvWaitKey(0);	
	int i=0,j=0;
	
	while(1)
	{
		int key=cvWaitKey(0);
		if(key=='a')
		{
			cvSetImageROI(img, cvRect(i-=10,j, 200,200) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='s')
		{
			cvSetImageROI(img, cvRect(i,j+=10, 200,200) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='d')
		{
			cvSetImageROI(img, cvRect(i+=10,j, 200,200) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='w')
		{
			cvSetImageROI(img, cvRect(i,j-=10, 200,200) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='q')
			break;

		//加入偵測視窗位置
		printf("now x: %d y: %d \n", i,j);
		if(i==250 && j==250)
			printf("OK~~~~~~~~~~~~~~~~\n");
	}
	return 0;
}

實作走迷宮小遊戲

#include <opencv/highgui.h>
#include <stdio.h>
 
int main()
{
	IplImage * img = cvLoadImage("c:/test.jpg");
	cvNamedWindow("a");
	cvShowImage("a", img);
	cvResizeWindow("a", 100,100);
	cvWaitKey(0);	
	int i=0,j=0,t=10,y=10,o=100,p=100;;
	
	while(1)
	{
		int key=cvWaitKey(0);
		if(key=='a')
		{	
			cvSetImageROI(img, cvRect(i-=10,j, 100,100) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='s')
		{	
			cvSetImageROI(img, cvRect(i,j+=10, 100,100) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='d')
		{	
			cvSetImageROI(img, cvRect(i+=10,j, 100,100) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='w')
		{	
			cvSetImageROI(img, cvRect(i,j-=10, 100,100) );
			cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='e')
		{	
			cvSetImageROI(img, cvRect(i,j, o+=t,p+=y) );
			//cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='r')
		{		
			cvSetImageROI(img, cvRect(i,j, o-=t,p-=y) );
			//cvMoveWindow("a",i,j);
			cvShowImage("a", img);
			cvWaitKey(0);
		}
		else if(key=='q')
			break;
		printf("now x: %d y: %d \n", i,j);
		if(i>500 && i<540 && j<510 && j>440)
		{
			printf("OK~~~~~~~~~~~~~~~~\n");
			IplImage * img = cvLoadImage("c:/c.jpg");
			cvNamedWindow("a");
			cvShowImage("a", img);
		}
	}
	return 0;
}

葉正聖老師: 相當地不錯,能把新學到的工具或玩具,隨時隨地發揮想像力,做出超乎別人想像的應用,很優秀。

week 6-1 (2010.8.9)

黑底彩圖→黑底白圖

#include <opencv/cv.h>
#include <opencv/highgui.h>
int main()
{
	char s[30]={"C:/3.bmp"};
	int threshold_type;
	IplImage * img = cvLoadImage(s);
	IplImage * r=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
	IplImage * g=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
	IplImage * b=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
	IplImage * a=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
	cvSplit(img,r,g,b,NULL);
	cvNamedWindow("a");
	/*
	cvShowImage("a",b);
	cvWaitKey(0);
	
	cvShowImage("a",g);
	cvWaitKey(0);
	
	cvShowImage("a",r);
	cvWaitKey(0);
	*/
	cvThreshold(b,a,0,255,CV_THRESH_BINARY); 
	
	cvShowImage("a",a);
	cvMoveWindow("a",0,0);
	cvWaitKey(0);
	return 0;
}

week 7-2 (2010.8.19)

複習進度

cvNamedWindow("test");  //"test" 後面沒加參數則為預設值1
cvShowImage("test", img);  //0819-1.jpg
cvResizeWindow("test",240,144);  //只顯示圖片左上角的區域,且不能縮放大小

cvNamedWindow("test",0);  //"test" 後面加參數0,則可以將整張圖做縮放
cvShowImage("test", img);  //0819-2.jpg
cvResizeWindow("test",240,144);  //整張圖做縮放為240*144,且能用滑鼠改變大小

兩圖相減去背景

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
 
int main()
{
	IplImage * imgyes = cvLoadImage("c:/yes.jpg");
	IplImage * imgno = cvLoadImage("c:/no.jpg");
	cvNamedWindow("yes");
	cvNamedWindow("no");
	cvShowImage("yes", imgyes);  //yes.jpg
	cvShowImage("no", imgno);  //no.jpg
	cvWaitKey(0);
	/*
	cvSub(imgyes,imgno,imgyes);
	cvShowImage("yes", imgyes);
	cvShowImage("no", imgno);	
	cvWaitKey(0);
	*/
	cvSub(imgno,imgyes,imgyes);
	cvShowImage("yes", imgyes);  //yes2.jpg
	cvShowImage("no", imgno);	
	cvWaitKey(0);
	
	cvNot(imgyes,imgyes);
	cvShowImage("yes", imgyes);  //yes3.jpg
	cvShowImage("no", imgno);
	cvWaitKey(0);
	return 0;
}

使用cvThreshold

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
 
int main()
{
	IplImage * imgno =cvLoadImage("C:/no.jpg");
	IplImage * imgyes=cvLoadImage("C:/yes.jpg");
 
	cvNamedWindow("yes");
	cvNamedWindow("no");
	cvShowImage("yes", imgyes);  //yes.jpg
	cvShowImage("no", imgno);  //no.jpg
	cvWaitKey(0);
 
	cvSub(imgno,imgyes,imgyes);	//相減的圖片 
	cvNot(imgyes,imgyes);		//再做出反白效果 
	cvShowImage("yes", imgyes);  //yes3.jpg
	cvShowImage("no", imgno);
	cvWaitKey(0);

	//cvThreshold用在彩色圖上效果不好,需先轉成灰階 
	cvThreshold(imgyes, imgyes, 230, 0, CV_THRESH_TOZERO_INV);
	cvShowImage("yes", imgyes);  //yes4.jpg
	cvShowImage("no", imgno);
	cvWaitKey(0);
	return 0;
}

使用cvSplit(轉灰階1)

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h> 
int main()
{
	IplImage * imgno =cvLoadImage("d:/0819.jpg");
	IplImage * imgyes=cvLoadImage("d:/0819-yes.jpg");
 
	cvNamedWindow("yes",0);
	cvNamedWindow("no",0);
	cvShowImage("yes", imgyes);  //0819-y.JPG
	cvShowImage("no", imgno);    //0819-2.JPG
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	cvSub(imgno,imgyes,imgyes);
	cvNot(imgyes,imgyes);
	cvShowImage("yes", imgyes);  //0819-y2.JPG
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144); 
	cvWaitKey(0);
 
	IplImage * imggrey = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgR = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgG = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgB = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
 
	cvSplit(imgyes, imgR, imgG, imgB, NULL);
	cvShowImage("yes", imgR);  //0819-yr.JPG
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);
	cvShowImage("yes", imgG);  //0819-yg.JPG
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);
	cvShowImage("yes", imgB);  //0819-yb.JPG
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);
 
	return 0;
}

使用cvCvtColor(轉灰階2)

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
 
int main()
{
	IplImage * imgno =cvLoadImage("d:/0819.jpg");
	IplImage * imgyes=cvLoadImage("d:/0819-yes.jpg");
 
	cvNamedWindow("yes",0);
	cvNamedWindow("no",0);
	cvShowImage("yes", imgyes);  //0819-y.JPG
	cvShowImage("no", imgno);    //0819-2.JPG
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	cvSub(imgno,imgyes,imgyes);
	cvNot(imgyes,imgyes);
	cvShowImage("yes", imgyes);  //0819-y2.JPG
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	IplImage * imggrey = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgR = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgG = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	IplImage * imgB = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
 
	cvCvtColor(imgyes, imggrey, CV_BGR2GRAY);
	cvShowImage("yes",imggrey);  //0819-ygrey.JPG
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);

	return 0;
}

使用cvCreateTrackbar

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
 
IplImage * imggrey, *imgR, *imgG, *imgB;  //宣告要拉出來才能共用!
void mychange(int position)  //每次移動 Trackbar 時, 呼叫 mychange 做對映的修正。
{
	cvThreshold(imggrey, imgR, position, 0, CV_THRESH_TOZERO_INV);
	cvShowImage("yes", imgR);
}
int main()
{
	IplImage * imgno =cvLoadImage("d:/0819.jpg");
	IplImage * imgyes=cvLoadImage("d:/0819-yes.jpg");
 
	cvNamedWindow("yes",0);
	cvNamedWindow("no",0);
	cvShowImage("yes", imgyes);
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	cvSub(imgno,imgyes,imgyes);
	cvNot(imgyes,imgyes);
	cvShowImage("yes", imgyes);
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	int cut=0;
	cvCreateTrackbar("threshold", "yes", &cut, 255, mychange);
 
	imggrey = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgR = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgG = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgB = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
 
	cvCvtColor(imgyes, imggrey, CV_BGR2GRAY);
	cvShowImage("yes",imggrey);
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);
 
	return 0;
}

葉正聖老師: 宗樺,老師對於你能實際應用上課所教的內容進行練習並能真的拿實際的圖進行驗證,覺得非常地不錯。相信你主動的練習,將會加深你的學習印象,並能運用想像力做巧妙地應用。很不錯哦!(2010-08-21)

week 8-1 (2010.8.23)

使用cvSmooth去雜訊

#include <opencv/highgui.h>
#include <opencv/cv.h>
int main()
{
	IplImage * img = cvLoadImage("d:/0823.jpg");  //0823.jpg為原始圖片
	cvNamedWindow("test");
	cvShowImage("test", img);  //0823-1.jpg為原始圖片經Snoop放大
	cvWaitKey(0);
	
	cvSmooth(img,img);
	cvShowImage("test", img);  //0823-2.jpg為前圖經cvSmooth處理
	cvWaitKey(0);
	return 0;
	//0823-2.jpg有去除白色背景的雜訊與整體模糊化
}

將cvSmooth應用在上週進度

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <stdio.h> 
IplImage * imggrey, *imgR, *imgG, *imgB;  //宣告要拉出來才能共用!
void mychange(int position)  //每次移動 Trackbar 時, 呼叫 mychange 做對映的修正。
{
	cvThreshold(imggrey, imgR, position, 0, CV_THRESH_TOZERO_INV);
	cvShowImage("yes", imgR);
	
	cvCopy(imggrey, imgG);
	cvSmooth(imgG,imgG);
	cvThreshold(imgG, imgG, position, 0, CV_THRESH_TOZERO_INV);
	cvShowImage("no", imgG);

	printf("p is %d\n",position);	
}

void myfunction(int p)
{
	printf("p is %d\n",p);	
}
int main()
{
	IplImage * imgno =cvLoadImage("c:/0819-2.jpg");
	IplImage * imgyes=cvLoadImage("c:/0819-y.jpg");
 
	cvNamedWindow("yes",0);
	cvNamedWindow("no",0);
	cvShowImage("yes", imgyes);
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	cvSub(imgno,imgyes,imgyes);
	cvNot(imgyes,imgyes);
	cvShowImage("yes", imgyes);
	cvShowImage("no", imgno);
	cvResizeWindow("yes",240,144);  
	cvResizeWindow("no",240,144);  
	cvWaitKey(0);
 
	int cut=0;
	cvCreateTrackbar("threshold", "yes", &cut, 255, mychange);
 	cvCreateTrackbar("printf cut", "yes", &cut, 255, myfunction);
	imggrey = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgR = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgG = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
	imgB = cvCreateImage(cvGetSize(imgyes), IPL_DEPTH_8U, 1);
 
	cvCvtColor(imgyes, imggrey, CV_RGB2GRAY);
	cvShowImage("yes",imggrey);
	cvResizeWindow("yes",240,144);  
	cvWaitKey(0);
 
	return 0;
}

ps:“yes”為cvSmooth處理前,”no”為處理後

比較cvSmooth,cvErode,cvDilate

#include <opencv/highgui.h>
#include <opencv/cv.h>
int main()
{
	IplImage * img = cvLoadImage("d:/0823.jpg");
	cvNamedWindow("test");
	cvShowImage("test", img);
	cvWaitKey(0);
	//以下函數分開執行
	cvSmooth(img,img);
	cvErode(img, img);
	cvDilate(img, img);
	cvShowImage("test", img);
	cvWaitKey(0);
	return 0;
	//0823-2.JPG 只使用cvSmooth
	//0823-erode.JPG 只使用cvErode
	//0823-dilate.JPG 只使用cvDilate
	//0823-ed.JPG 使用cvErode & cvDilate
}

使用facedetect人臉偵測

在C:\WINDOWS\system32目錄加入以下檔案
highgui100.dll , cxcore100.dll , libguide40.dll
以上檔案位於D:\Program Files\Dev-Cpp\bin\opencv

在cmd命令提示字元下執行
Microsoft Windows XP [版本 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd d:/

C:\Documents and Settings\Administrator>d:

D:\>cd D:\Program Files\Dev-Cpp\Examples\opencv\samples\c

D:\Program Files\Dev-Cpp\Examples\opencv\samples\c>facedetect --cascade="../../d
ata/haarcascades/haarcascade_frontalface_alt.xml" d:\0823-people.jpg
detection time = 357.849ms

week 9-1 (2010.8.30)

編譯OpenCV範例程式

OpenCV範例程式目錄為 "D:\Program Files\Dev-Cpp\Examples\opencv\samples\c"

在C:\WINDOWS\system32目錄加入以下檔案
highgui100.dll , cxcore100.dll , libguide40.dll
cv100.dll , cvaux100.dll , ml100.dll
以上檔案位於D:\Program Files\Dev-Cpp\bin\opencv

"工具→編譯器選項→目錄→C++引入檔(Includes)"下加入目錄
D:\Program Files\Dev-Cpp\include\opencv

最後將副檔名".c"改為".cpp"即可使用Dev-cpp進行編譯執行

contours.cpp 抓輪廓線

紅線圈的範圍是=>紅線內為白色、紅線外為黑色
綠線圈的範圍是=>綠線內為黑色、綠線外為白色
由紅線與綠線來區分是外輪廓或內輪廓

cvCreateTrackbar所調整的levels
levels初始值為3
levels越高為向外找輪廓、levels越低為向內找輪廓

葉正聖老師: 宗樺相當不錯,把集訓時所試過的範例全部自己重做一次。更重要的是,還能把沒有提過的範例也都試過一次,相當好。(2010-09-01)

convexhull.cpp 圈最大範圍

在所有的紅點範圍中,
找出最外面的紅點,
以綠線將最外面的紅點連起,
使綠線所圍成的範圍能包含所有紅點。

葉正聖老師: ConvexHull 是演算法中, Divided and Conquer 的典型例子,只要 O(nlogn)的時間複雜度,即能完成。(2010-09-01)

delaunay.cpp

葉正聖老師: Delaunary Graph 有人俗稱烏龜圖,也就是可以算出像龜殼的結果,也是常見 O(nlogn)的演算法。而在圖像追蹤時,有時會有妙用,因為它等於是勢力範圍的中垂線劃分圖,所以分割得很好。(2010-09-01)

demhist.cpp

葉正聖老師: Histogram 在圖像比對(Pattern Recognition, PR) 會有簡單又快速地比對效果,而且以此為基礎,可以做出許多的變型,讓比對的效果更好。(2010-09-01)

dft.cpp

fitellipse.cpp

修正家中webcam問題(2010.9.13)

因為直接開啟是抓系統預設的視訊裝置,
所以會直接抓一個無法執行的虛擬裝置。
會看到全綠色的畫面。
使用AMCap視訊程式來找系統預設裝置,
為"ASUSTeK Virtual Capturs Device"
在裝置管理員選擇為停用,
再次執行程式即可選擇視訊來源裝置。

葉正聖老師: 恭喜宗樺經過系統的調整,順利地在裝置管理員中調整後,讓 OpenCV 在抓 webcam 時能順利地自動挑到適合的 webcam。相信有了實體 webcam 的使用, 可以有更多的實作及嘗試,可以進步得更快。(2010-09-14)
另外使用 OpenCV 的時候, CvCapture * cap = cvCreateCameraCapture(參數) 時,這個參數可以做一些調整,有機會讓你的程式更具有可調整性 (可參考 “學習OpenCv”一書的第四章)。

李宗樺/我的專研練習.txt · Last modified: 2013/07/09 23:35 by 120.117.159.22