展示圖:
程式碼:
- (void)viewDidLoad {
[super viewDidLoad];
//由A圖比對B圖
//A圖
UIImage *mImage = [UIImage imageNamed:@"pic.JPG"];
[self.imageView setImage:mImage];
srcIpl = [self convertToIplImage:[self useImage:mImage]];
IplImage *dscIpl = cvCreateImage(cvGetSize(srcIpl),
srcIpl->depth, 1);
IplImage *dscIplNew = cvCreateImage(cvGetSize(srcIpl), IPL_DEPTH_8U, 3);
cvCvtColor(dscIpl, dscIplNew, CV_GRAY2BGR);
//B圖
UIImage *tempImage = [UIImage imageNamed:@"tmpPic.JPG"];
[self.tmpImageView setImage:tempImage];
iplTempImage = [self convertToIplImage:[self
useImage:tempImage]];
// Do any additional setup after loading
the view from its nib.
}
-(IplImage*)cropIplImage:(IplImage*)srcIpl
withStartPoint:(CvPoint)mPoint withWidth:(int)width withHeight:(int)height
{
//裁切後的圖片
IplImage *cropImage;
cvSetImageROI(srcIpl, cvRect(mPoint.x,
mPoint.y, width, height));
cropImage = cvCreateImage(cvGetSize(srcIpl),
IPL_DEPTH_8U, 3);
cvCopy(srcIpl, cropImage);
cvResetImageROI(srcIpl);
return cropImage;
}
//
多通道彩色圖片的直方圖比對
-(double)CompareHist:(IplImage*)image1
withParam2:(IplImage*)image2
{
int hist_size = 256;
IplImage *gray_plane = cvCreateImage(cvGetSize(image1),
8, 1);
cvCvtColor(image1, gray_plane, CV_BGR2GRAY);
CvHistogram *gray_hist = cvCreateHist(1,
&hist_size, CV_HIST_ARRAY);
cvCalcHist(&gray_plane, gray_hist);
IplImage *gray_plane2 = cvCreateImage(cvGetSize(image2),
8, 1);
cvCvtColor(image2, gray_plane2, CV_BGR2GRAY);
CvHistogram *gray_hist2 = cvCreateHist(1,
&hist_size, CV_HIST_ARRAY);
cvCalcHist(&gray_plane2, gray_hist2);
double rst =cvCompareHist(gray_hist,
gray_hist2, CV_COMP_BHATTACHARYYA);
printf("比對結果=%f\n",rst);
return rst;
}
結論:
1.關於圖片比對,看來只用於人臉辨識比較有用,不適用在一般風景圖等的圖片上使用.
2.這部分只針對單機使用,您可以把圖片B換成你喜歡的 ,我只做換圖片A來做測試,但如果遇到情況是需要跟雲端上圖片做比對,來顯示圖片資料的情況,我覺得乾脆把圖片上傳到雲端做完比對,再回傳手機上可能比較快吧,如果前輩們有更好的方法,還請賜教啊,這邊實驗到這邊XD.
範例檔案:下載
參考範例:
沒有留言:
張貼留言