分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
http://www.cnblogs.com/yingying0907/archive/2011/08/06/2129472.html
颜色提取
Ø 颜色直方图提取:
Code:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
IplImage * src= cvLoadImage("E:\\Download\\test1.jpg",1);
IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
/** H 分量划分为16个等级,S分量划分为8个等级*/
int h_bins = 16, s_bins = 8;
int hist_size[] = {h_bins, s_bins};
/** H 分量的变化范围*/
float h_ranges[] = { 0, 180 };
/** S 分量的变化范围*/
float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
/** 输入图像转换到HSV颜色空间*/
cvCvtColor( src, hsv, CV_BGR2HSV );
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
/** 创建直方图,二维, 每个维度上均分*/
CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
/** 根据H,S两个平面数据统计直方图*/
cvCalcHist( planes, hist, 0, 0 );
/** 获取直方图统计的最大值,用于动态显示直方图*/
float max_value;
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
/** 设置直方图显示图像*/
int height = 240;
int width = (h_bins*s_bins*6);
IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );
cvZero( hist_img );
/** 用来进行HSV到RGB颜色转换的临时单位图像*/
IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);
IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);
int bin_w = width / (h_bins * s_bins);
for(int h = 0; h < h_bins; h++)
{
for(int s = 0; s < s_bins; s++)
{
int i = h*s_bins + s;
/** 获得直方图中的统计次数,计算显示在图像中的高度*/
float bin_val = cvQueryHistValue_2D( hist, h, s );
int intensity = cvRound(bin_val*height/max_value);
/** 获得当前直方图代表的颜色,转换成RGB用于绘制*/
cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0));
cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);
CvScalar color = cvGet2D(rgb_color,0,0);
cvRectangle( hist_img, cvPoint(i*bin_w,height),
cvPoint((i+1)*bin_w,height - intensity),
color, -1, 8, 0 );
}
}
cvNamedWindow( "Source", 1 );
cvShowImage( "Source", src );
cvNamedWindow( "H-S Histogram", 1 );
cvShowImage( "H-S Histogram", hist_img );
cvWaitKey(0);
}
运行效果截图:
形状提取
Ø Candy算子对边缘提取:
Code:
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pImg = NULL;
IplImage* pCannyImg = NULL;
//载入图像,强制转化为Gray
pImg = cvLoadImage( "E:\\Download\\test.jpg", 0);
//为canny边缘图像申请空间
pCannyImg = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
//canny边缘检测
cvCanny(pImg, pCannyImg, 50, 150, 3);
//创建窗口
cvNamedWindow("src", 1);
cvNamedWindow("canny",1);
//显示图像
cvShowImage( "src", pImg );
cvShowImage( "canny", pCannyImg );
//等待按键
cvWaitKey(0);
//销毁窗口
cvDestroyWindow( "src" );
cvDestroyWindow( "canny" );
//释放图像
cvReleaseImage( &pImg );
cvReleaseImage( &pCannyImg );
return 0;
}
运行效果截图:
Ø 角点提取:
Code:
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#define MAX_CORNERS 100
int main(void)
{
int cornersCount=MAX_CORNERS;//得到的角点数目
CvPoint2D32f corners[MAX_CORNERS];//输出角点集合
IplImage *srcImage = 0,*grayImage = 0,*corners1 = 0,*corners2 = 0;
int i;
CvScalar color = CV_RGB(255,0,0);
cvNamedWindow("image",1);
//Load the image to be processed
srcImage = cvLoadImage("E:\\Download\\1.jpg",1);
grayImage = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
//copy the source image to copy image after converting the format
//复制并转为灰度图像
cvCvtColor(srcImage,grayImage,CV_BGR2GRAY);
//create empty images os same size as the copied images
//两幅临时位浮点图像,cvGoodFeaturesToTrack会用到
corners1 = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_32F,1);
corners2 = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_32F,1);
cvGoodFeaturesToTrack(grayImage,corners1,corners2,corners,&cornersCount,0.05,
30,//角点的最小距离是
0,//整个图像
3,0,0.4);
printf("num corners found: %d\n",cornersCount);
//开始画出每个点
if (cornersCount>0)
{
for (i=0;i<cornersCount;i++)
{
cvCircle(srcImage,cvPoint((int)(corners[i].x),(int)(corners[i].y)),2,color,2,CV_AA,0);
}
}
cvShowImage("image",srcImage);
cvSaveImage("imagedst.png",srcImage);
cvReleaseImage(&srcImage);
cvReleaseImage(&grayImage);
cvReleaseImage(&corners1);
cvReleaseImage(&corners2);
cvWaitKey(0);
return 0;
}
运行效果截图:
Ø Hough直线提取:
Code:
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{
IplImage* src = cvLoadImage( "E:\\Download\\2.jpg" , 0 );
IplImage* dst;
IplImage* color_dst;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* lines = 0;
int i;
if( !src )
return -1;
dst = cvCreateImage( cvGetSize(src), 8, 1 );
color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
cvCanny( src, dst, 50, 200, 3 );
cvCvtColor( dst, color_dst, CV_GRAY2BGR );
#if 0
lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );
for( i = 0; i < MIN(lines->total,100); i++ )
{
float* line = (float*)cvGetSeqElem(lines,i);
float rho = line[0];
float theta = line[1];
CvPoint pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, CV_AA, 0 );
}
#else
lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
for( i = 0; i < lines->total; i++ )
{
CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 );
}
#endif
cvNamedWindow( "Source", 1 );
cvShowImage( "Source", src );
cvNamedWindow( "Hough", 1 );
cvShowImage( "Hough", color_dst );
cvWaitKey(0);
return 0;
}
运行效果截图:
Ø Hough圆提取:
Code:
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
IplImage* img;
img=cvLoadImage("E:\\Download\\3.jpg", 1);
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 5, 15 );
// smooth it, otherwise a lot of false circles may be detected
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
int i;
for( i = 0; i < circles->total; i++ )
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
cout<<"圆心坐标x= "<<cvRound(p[0])<<endl<<"圆心坐标y= "<<cvRound(p[1])<<endl;
cout<<"半径="<<cvRound(p[2])<<endl;
}
cout<<"圆数量="<<circles->total<<endl;
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", img );
cvWaitKey(0);
return 0;
}
运行效果截图:
Ø Hough矩形提取:
Code:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
int thresh = 50;
IplImage* img = 0;
IplImage* img0 = 0;
CvMemStorage* storage = 0;
CvPoint pt[4];const char* wndname = "Square Detection Demo";
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 );
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 ));
// down-scale and upscale the image to filter out the noise
cvPyrDown( timg, pyr, 7 );
cvPyrUp( pyr, timg, 7 );
tgray = cvCreateImage( sz, 8, 1 );
// find squares in every color plane of the image
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;
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;
}
// the function draws all the squares in the image
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* rect = pt;
int count = 4;
memcpy( pt, reader.ptr, squares->elem_size );
CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
memcpy( pt + 1, reader.ptr, squares->elem_size );
CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
memcpy( pt + 2, reader.ptr, squares->elem_size );
CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
memcpy( pt + 3, reader.ptr, squares->elem_size );
CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
}
cvShowImage( wndname, cpy );