2020-02-01 11:58:37 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include "OpenCV/cv.h"
|
|
|
|
#include "OpenCV/highgui.h"
|
|
|
|
#include "Opencv2/opencv.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
CvSeq * znajdz_kontury(IplImage* obrazek)
|
|
|
|
{
|
|
|
|
// czytamy obraz
|
|
|
|
IplImage* obraz;
|
|
|
|
cvCvtColor(obrazek, obraz, CV_RGB2GRAY);
|
|
|
|
if (obraz == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CvSeq * kontur;
|
|
|
|
// pamiec na obliczenia
|
|
|
|
CvMemStorage * mem = cvCreateMemStorage(0);
|
|
|
|
// operacja progrowania
|
|
|
|
cvThreshold(obraz, obraz, 100, 255, CV_THRESH_BINARY_INV);
|
|
|
|
// szukanie konturow
|
|
|
|
cvFindContours(obraz, mem, &kontur);
|
|
|
|
// nakladanie konturow
|
|
|
|
kontur = cvApproxPoly(kontur, sizeof(CvContour), mem, CV_POLY_APPROX_DP, cvContourPerimeter(kontur) * 0.035);
|
|
|
|
// sprzatanie
|
|
|
|
cvReleaseImage(&obraz);
|
|
|
|
|
|
|
|
return kontur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void okresl_kontury2(IplImage* kolorowyObraz, CvSeq * kontur1, CvSeq * kontur2,
|
|
|
|
CvScalar kolor1 = cvScalar(0.0, 255.0, 255.0, 0.0), CvScalar kolor2 = cvScalar(255.0, 0.0, 255.0, 0.0))
|
|
|
|
{
|
|
|
|
// czytamy obraz
|
|
|
|
IplImage * obraz = kolorowyObraz;
|
|
|
|
if (obraz == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// przystowowywanie obrazu
|
|
|
|
IplImage * do_analizy = cvCreateImage(cvSize(obraz->width, obraz->height), 8, 1);
|
|
|
|
cvCvtColor(obraz, do_analizy, CV_BGR2GRAY);
|
|
|
|
|
|
|
|
// nasz kontur
|
|
|
|
CvSeq * kontur;
|
|
|
|
// pamiec na obliczenia
|
|
|
|
CvMemStorage * mem = cvCreateMemStorage(0);
|
|
|
|
// operacja progrowania
|
|
|
|
cvThreshold(do_analizy, do_analizy, 100, 255, CV_THRESH_BINARY_INV);
|
|
|
|
// szukanie konturow
|
|
|
|
cvFindContours(do_analizy, mem, &kontur);
|
|
|
|
|
|
|
|
// tworzymy histogramy
|
|
|
|
int rozmiary[2] = { 2,2 };
|
|
|
|
CvHistogram * hist_analiz = cvCreateHist(2, rozmiary, CV_HIST_ARRAY, NULL);
|
|
|
|
CvHistogram * hist_kontur1 = cvCreateHist(2, rozmiary, CV_HIST_ARRAY, NULL);
|
|
|
|
CvHistogram * hist_kontur2 = cvCreateHist(2, rozmiary, CV_HIST_ARRAY, NULL);
|
|
|
|
// obliczamy histogramy geometryczne
|
|
|
|
cvCalcPGH(kontur1, hist_kontur1);
|
|
|
|
cvCalcPGH(kontur2, hist_kontur2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; kontur != NULL; kontur = kontur->h_next)
|
|
|
|
{
|
|
|
|
CvSeq* temp_kontur = cvApproxPoly(kontur, sizeof(CvContour), mem, CV_POLY_APPROX_DP,
|
|
|
|
cvContourPerimeter(kontur));
|
|
|
|
cvCalcPGH(temp_kontur, hist_analiz);
|
|
|
|
|
|
|
|
cvCompareHist(hist_analiz, hist_kontur1, CV_COMP_CORREL);
|
|
|
|
// badanie podobieństwa konturow
|
|
|
|
double match1 = cvCompareHist(hist_analiz, hist_kontur1, CV_COMP_CORREL);
|
|
|
|
double match2 = cvCompareHist(hist_analiz, hist_kontur2, CV_COMP_CORREL);
|
|
|
|
// badanie korelacji, jezeli histogramy sa podobne to korelacja jest wieksza (1,0 dla identycznych i -1,0 calkowicie roznych)
|
|
|
|
if (match1 > match2)
|
|
|
|
cvDrawContours(obraz, temp_kontur, kolor1, kolor1, 0, 2, CV_AA);
|
|
|
|
else
|
|
|
|
cvDrawContours(obraz, temp_kontur, kolor2, kolor2, 0, 2, CV_AA);
|
|
|
|
}
|
|
|
|
|
|
|
|
cvNamedWindow("kontury", CV_WINDOW_AUTOSIZE);
|
|
|
|
cvShowImage("kontury", obraz);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int l = cvWaitKey(100);
|
|
|
|
if (l == 27)
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
cvDestroyWindow("kontury");
|
|
|
|
cvReleaseImage(&do_analizy);
|
|
|
|
cvReleaseImage(&obraz);
|
|
|
|
cvReleaseMemStorage(&mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
IplImage* obrazDoanalizy = cvLoadImage("animal.jpg", CV_LOAD_IMAGE_COLOR);
|
|
|
|
IplImage* wzorJeden = cvLoadImage("wzor1.jpg", CV_LOAD_IMAGE_COLOR);
|
|
|
|
IplImage* wzorDwa = cvLoadImage("wzor2.jpg", CV_LOAD_IMAGE_COLOR);
|
|
|
|
okresl_kontury2(obrazDoanalizy, znajdz_kontury(wzorJeden), znajdz_kontury(wzorDwa));
|
|
|
|
return 0;
|
|
|
|
}
|