图像相似度检测之C++代码(8位--256色)
2008-07-04 09:17
405 查看
本代码属于原创,如需引用,请声明来源
下面是直方图相交的代码,同种图片的识别率达90%以上,性能非常稳定。
程序的例子是8位(256色)位图,其他位图类似。
下面是直方图相交的代码,同种图片的识别率达90%以上,性能非常稳定。
程序的例子是8位(256色)位图,其他位图类似。
#include <fstream> #include <map> #include <string> #include <iostream> using namespace std; typedef unsigned char uvar8; typedef unsigned int uvar32; typedef unsigned short int uvar16; typedef struct { uvar8 Blue; /*蓝色所占比重*/ uvar8 Green; /*绿色所占比重*/ uvar8 Red; /*红色所占比重*/ uvar8 Reserved; } Palette; //定义bmp结构 uvar8 bfType[2]; /* 类型标志,总是BM */ typedef struct { uvar32 bfSize; /* 文件大小 */ uvar32 bfReserved1; uvar32 bfOffBits; /* 位图点阵偏移量*/ }HEAD; typedef struct { uvar32 biSize; /* 结构体字节总数 */ uvar32 biWidth; /* 图像宽度 */ uvar32 biHeight; /*图像高度 */ uvar16 biPlanes; /* 必须为1 */ uvar16 biBitCount; /* 每个像素所占二进制位数,可能是1,4,8或 24 */ uvar32 biCompress; /*压缩方式*/ uvar32 biSizeImage; /*像素点阵大小 */ uvar32 biXPelsPerMeter; /* 水平像素数*/ uvar32 biYPelsPerMeter; /* 垂直像素数 */ uvar32 biClrUsed; /*使用的颜色数 */ uvar32 biClrImportant; /*重要颜色数 */ }INFO; bool ReadBMP(string strFileUrl,uvar8* & pImgData,int& iSum) { HEAD strHead; INFO strInfo; uvar8 type[2]; //uvar32 imdata[256][256]; Palette Rgbquad; ifstream fin; fin.open(strFileUrl.c_str(),ios_base::in | ios_base::binary); /*以二进制读方式打开该文件,一定要二进制的!*/ if(!fin) { cout<<"No this file!/n"; return false; } fin.read((char*)&bfType,2); fin.read((char*)&strHead,sizeof(strHead)); fin.read((char*)&strInfo,sizeof(strInfo)); fin.seekg(strHead.bfOffBits,ios::beg); if (strInfo.biBitCount == 8) { //pImgData = (uvar8*)pImgData; pImgData = new uvar8[strInfo.biWidth*strInfo.biHeight]; } fin.read((char*)(pImgData),strInfo.biWidth * strInfo.biHeight* sizeof uvar8); fin.close(); iSum = strInfo.biWidth * strInfo.biHeight; return true; } int main() { uvar8 *pImgData1 =NULL ,*pImgData2 = NULL; int i,iSum1,iSum2; while(1) { string url1; string url2; cout<<"cin bmp file name"<<endl; cin>> url1 >> url2; ReadBMP(url1,pImgData1,iSum1); ReadBMP(url2,pImgData2,iSum2); map<int,int> Map1,Map2; for (i=0;i<iSum1;i++) { if (Map1.find(pImgData1[i])!=Map1.end()) Map1[(uvar8)pImgData1[i]]++; else Map1[(uvar8)pImgData1[i]] = 0; } for (i=0;i<iSum2;i++) { if (Map2.find(pImgData2[i])!=Map2.end()) Map2[(uvar8)pImgData2[i]]++; else Map2[(uvar8)pImgData2[i]] = 0; } int iSum = 0; for (map<int,int>::iterator it = Map1.begin(); it!= Map1.end(); it++) { iSum += Map1[(uvar8)it->first] < Map2[(uvar8)it->first] ? Map1[(uvar8)it->first] : Map2[(uvar8)it->first]; } double dResult = (double) iSum / iSum1 > (double) iSum / iSum2 ? (double) iSum / iSum1:(double) iSum / iSum2; //----------------- cout<<endl; cout<<"special dots the sum of pic1 the sum of pic2"<<endl; for (map<int,int>::iterator it1 = Map1.begin(); it1!= Map1.end(); it1++) { cout<<it1->first<<" "<<it1->second<<" "<<Map2[it1->first]<<endl; } cout<<"******************************************"<<endl; cout<<"conculsion:"<<endl; cout<< "The sum of dots in Pic1: "<< iSum1<<endl; cout<< "The sum of dots in Pic2: "<< iSum2<<endl; cout<< "The similar dots betwwen pic1 and pic2 : "<<iSum<<endl; cout<<"The rate of simliar betwwen the above two pictures: " << dResult <<endl<<endl; delete[] pImgData1; delete[] pImgData2; } getchar(); return 0; }
相关文章推荐
- 图像相似度检测之c++代码(24位)
- 基于C++和OpenCv的SIFT_图像局部特征检测算法代码的实现
- Opencv检测交通中红色标识轮廓c++代码实例及运行结果
- 交通标识检测c++代码实例及运行结果
- 国外的一个图像显著区域检测代码及其效果图 saliency region detection
- 图像边缘检测之拉普拉斯(Laplacian)C++实现
- 检查C++中的内存泄漏-通过添加代码来检测
- MFC下实现 灰度图像显示函数代码 C++
- 图像显著性检测-谱残差方法 c++ opencv实现
- OpenCV人脸检测(C++代码)
- C++代码 检测某进程是否开启
- 图像处理之其他杂项(一)之MeanShift的目标跟踪算法opencv c++代码 VS2015+opencv3.2
- 计算图像的梯度及梯度直方图相似度(Matlab代码)
- 【python + PIL】图像相似度检测
- 打印字符图像c++代码实例及运行结果
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法实现边缘检测代码(2)
- 【图像特征提取12】OpenCv的SIFT图像局部特征提取描述算法C++代码的实现
- 图像放大并进行BiCubic插值 Matlab/C++代码
- TIF图像文件的读取(c++代码)
- opencv图像融合c++代码实例及运行结果