opencv的使用例子

显示图片的例子:

#include 
#include 
using namespace std;

int main()
{
	IplImage * test;
	test = cvLoadImage("D:\\cvImg\\1024.jpg");
	cvNamedWindow("test_demo", 1);
	cvShowImage("test_demo", test);
	cvWaitKey(0);
	cvDestroyWindow("test_demo");
	cvReleaseImage(&test);
	return 0;
}

图像相似度ORB算法

#define _CRT_SECURE_NO_DEPRECATE

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

//OpenCV图像相似度ORB算法(图像特征比对) http://jingyan.baidu.com/article/6f2f55a185efa1b5b93e6c1d.html
int getORB(char * imagePatha, char * imagePathb) {
	double t;
	t = getTickCount();
	Mat img_1 = imread(imagePatha);
	Mat img_2 = imread(imagePathb);
	if (!img_1.data || !img_2.data) {
		cout << "error reading images " << endl;      return -1;
	}
	ORB orb;
	vector keyPoints_1, keyPoints_2;
	Mat descriptors_1, descriptors_2;
	orb(img_1, Mat(), keyPoints_1, descriptors_1);
	orb(img_2, Mat(), keyPoints_2, descriptors_2);
	BruteForceMatcher matcher;
	vector matches;
	matcher.match(descriptors_1, descriptors_2, matches);
	double max_dist = 0; double min_dist = 100;
	for (int i = 0; i < descriptors_1.rows; i++) {
		double dist = matches[i].distance;
		if (dist < min_dist) min_dist = dist;
		if (dist > max_dist) max_dist = dist;
	}
	printf("-- Max dist : %f \n", max_dist);
	printf("-- Min dist : %f \n", min_dist);
	std::vector< DMatch > good_matches;
	for (int i = 0; i < descriptors_1.rows; i++) {
		if (matches[i].distance < 0.6*max_dist) {
			good_matches.push_back(matches[i]);
		}
	}
	t = getTickCount() - t;
	t = t * 1000 / getTickFrequency();
	Mat img_matches;
	drawMatches(img_1, keyPoints_1, img_2, keyPoints_2, good_matches, img_matches,
		Scalar::all(-1), Scalar::all(-1), vector(),
		DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	imshow("Match", img_matches);
	printf("%f ms\n", t);
	cvWaitKey(0);
	return 0;
}

int main(int argc, char* argv[])
{


	//getORB("D:\\cvImg\\1023.jpg", "D:\\cvImg\\1025.jpg");
	getORB("D:\\cvImg\\1023.jpg", "D:\\cvImg\\1026.jpg");

	
	cout << "ok" << endl;


	return 0;
}

Google图像对比基本算法
#include 
#include 
#include 

//http://www.sharejs.com/codes/cpp/8012		Google图像对比基本算法

//计算图像感知hash值。详情看:http://blog.csdn.net/lhfslhfs/article/details/9157845
int64 CalcImagePerceptualHashKey(const IplImage* pImage)
{
	IplImage* pTheImage8X8 = cvCreateImage(cvSize(8, 8), pImage->depth, pImage->nChannels);
	IplImage* pGrayscaleImage = cvCreateImage(cvSize(8, 8), 8, 1);

	cvResize(pImage, pTheImage8X8, CV_INTER_AREA);
	cvConvertImage(pTheImage8X8, pGrayscaleImage);
	cvReleaseImage(&pTheImage8X8);

	//计算平均值
	float fElementMean = 0;
	for (int y = 0; y < 8; ++y)
	{
		for (int x = 0; x < 8; ++x)
		{
			unsigned char& cElem = *(unsigned char*)(pGrayscaleImage->imageData + x + y * pGrayscaleImage->widthStep);
			cElem = (unsigned char)(cElem / (float)255 * 64);
			fElementMean += cElem;
		}
	}

	fElementMean /= 64;
	unsigned char cElementKey = 0;
	int64 key = 0;
	unsigned char* pKeyPtr = (unsigned char*)&key;

	for (int y = 0; y < 8; ++y)
	{
		for (int x = 0; x < 8; ++x)
		{
			if (fElementMean > *(unsigned char*)(pGrayscaleImage->imageData + x + y * pGrayscaleImage->widthStep))
			{
				//小于平均值即为0。
				cElementKey <<= 1;
			}
			else
			{
				//否则即为1
				cElementKey <<= 1;
				cElementKey |= 1;
			}
		}

		pKeyPtr[y] = cElementKey;
	}

	cvReleaseImage(&pGrayscaleImage);
	return key;
}

//比较2个key的相似度,结果以1为最相似,0为不相似。
float CompareImageKeySimilarity(int64 key1, int64 key2)
{
	int64 keyResult = key1 ^ key2;
	int nOneCount = 0;
	int i = 64;
	while (i--)
	{
		if ((keyResult & 1) == 1)
			nOneCount++;

		keyResult >>= 1;
	}

	printf("nOneCount = %dn", nOneCount);
	return nOneCount == 0 ? 1 : (64 - nOneCount) / (float)64;
}

int main(int argc, char* argv[])
{
	IplImage* pSrc1 = cvLoadImage("D:\\cvImg\\1023.jpg");
	IplImage* pSrc2 = cvLoadImage("D:\\cvImg\\1026.jpg");
	int64 key1 = CalcImagePerceptualHashKey(pSrc1);
	int64 key2 = CalcImagePerceptualHashKey(pSrc2);

	printf("\nkey1=0x%llX,key2 = 0x%llX, Similarity = %.2fn", key1, key2, CompareImageKeySimilarity(key1, key2));
	return 0;
}

模板匹配:

 
  
/**
* @file MatchTemplate_Demo.cpp
* @brief Sample code to use the function MatchTemplate
* @author OpenCV team
*/

//#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod(int, void*);

/**
* @function main
*/
int main(int, char** argv)
{
	/// Load image and template
	img = imread(argv[1], 1);
	templ = imread(argv[2], 1);

	/// Create windows
	namedWindow(image_window, WINDOW_AUTOSIZE);
	namedWindow(result_window, WINDOW_AUTOSIZE);

	/// Create Trackbar
	const char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
	createTrackbar(trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod);

	MatchingMethod(0, 0);

	waitKey(0);
	return 0;
}

/**
* @function MatchingMethod
* @brief Trackbar callback
*/
void MatchingMethod(int, void*)
{
	/// Source image to display
	Mat img_display;
	img.copyTo(img_display);

	/// Create the result matrix
	int result_cols = img.cols - templ.cols + 1;
	int result_rows = img.rows - templ.rows + 1;

	result.create(result_rows, result_cols, CV_32FC1);

	/// Do the Matching and Normalize
	matchTemplate(img, templ, result, match_method);
	normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());

	/// Localizing the best match with minMaxLoc
	double minVal; double maxVal; Point minLoc; Point maxLoc;
	Point matchLoc;

	minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());


	/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
	if (match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED)
	{
		matchLoc = minLoc;
	}
	else
	{
		matchLoc = maxLoc;
	}

	/// Show me what you got
	rectangle(img_display, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);
	rectangle(result, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);

	imshow(image_window, img_display);
	imshow(result_window, result);

	return;
}

相似性对比

#include 
#include 
#include 
#include 
#include 


/*
*函数功能:获取输入图像 的轮廓
*
*输入:
*srcIn : 单通道图像
*
*返回:
*CvSeq* : 存储轮廓信息
*
*/

CvSeq* getImageContour(IplImage* srcIn) {

	IplImage* src;
	src = cvCreateImage(cvGetSize(srcIn), 8, 1);

	//拷贝图像 
	cvCopy(srcIn, src);

	//创建空间 
	CvMemStorage* mem = cvCreateMemStorage(0);
	//结构体 (mem和seq属于同一个地址<当释放的时候只要释放一个就行了>)
	CvSeq* seq;

	if (!mem) {
		printf("mem is null");
	}

	//二值化图像
	cvThreshold(src, src, 100, 255, 0);

	//计算图像轮廓 计算后的轮廓信息不能释放 在后面需要用到
	cvFindContours(src, mem, &seq, sizeof(CvContour), CV_RETR_CCOMP);

	//释放图像空间
	cvReleaseImage(&src);

	//返回轮廓信息
	return seq;

}




int main(int argc, char* argv[]) {

	//加载图像1
	IplImage* src1 = cvLoadImage("D:\\cvImg\\1022.jpg", CV_LOAD_IMAGE_GRAYSCALE);
	//获取图像1的轮廓
	CvSeq* contour1;
	contour1 = getImageContour(src1);


	//加载图像2
	IplImage* src2 = cvLoadImage("D:\\cvImg\\1025.jpg", CV_LOAD_IMAGE_GRAYSCALE);
	//获取图像2的轮廓
	CvSeq* contour2;
	contour2 = getImageContour(src2);


	//进行比对  -- 比较这两个轮廓所对应的HU矩的距离 有多远
	double result;
	//cvMatchShapes函数参数(轮廓1,轮廓2,方法)
	//计算第一个轮廓的普通矩和中心距 , 然后计算这个矩形的HU矩(HU距的7个数值保存在ma的数组里)
	//的哥个轮廓同上(HU距保存在mb中) 
	//然后比较这两个HU距的相似性 
	//方法参数有3个 参数分别是1,2,3 三种计算方法
	result = cvMatchShapes(contour1, contour2, 1);

	//输出结果 结果越小 相似度越高
	printf("result is %f \n", result);

	//释放轮廓存储空间
	cvReleaseMemStorage(&contour1->storage);
	cvReleaseMemStorage(&contour2->storage);

	//释放图像空间
	cvReleaseImage(&src1);
	cvReleaseImage(&src2);

	return 0;

}


附上相关链接:

http://jingyan.baidu.com/article/75ab0bcbee4b47d6864db2fc.html

http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/histograms/template_matching/template_matching.html#template-matching

http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/table_of_content_imgproc/table_of_content_imgproc.html#table-of-content-imgproc


你可能感兴趣的:(c/c++)