Last active
February 29, 2016 00:21
-
-
Save SammyVimes/c513cfd946078b4f6ea0 to your computer and use it in GitHub Desktop.
OPENCV1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// пример базовых морфологических преобразований | |
// cvErode() и cvDilate() | |
// | |
#include <opencv\cv.h> | |
#include <opencv\highgui.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdarg.h> | |
IplImage* image = 0; | |
IplImage* erode = 0; | |
IplImage* dilate = 0; | |
IplImage* gradient = 0; | |
IplImage *bigImage = 0; | |
int radius = 1; | |
int radius_max = 10; | |
// | |
// функция-обработчик ползунка - | |
// радиус ядра | |
void myTrackbarRadius(int pos) { | |
radius = pos; | |
} | |
int iterations = 1; | |
int iterations_max = 10; | |
// | |
// функция-обработчик ползунка - | |
// число итераций | |
void myTrackbarIterations(int pos) { | |
iterations = pos; | |
} | |
void cvShowManyImages(int nArgs, ...) { | |
IplImage *img; | |
int size; | |
int i; | |
int m, n; | |
int x, y; | |
// w - количество картинок в строке | |
// h - количество картинок в столбце | |
int w, h; | |
// scale - // scale - во сколько раз нужно изменить (уменьшить/увеличить) изображения | |
float scale; | |
int max; | |
// показываем только от 1 до 12 изображений | |
if (nArgs <= 0) { | |
printf("Number of arguments too small....\n"); | |
return; | |
} | |
else if (nArgs > 12) { | |
printf("Number of arguments too large....\n"); | |
return; | |
} | |
// определяем размер большой картинки, | |
// и количество картинок в строках и столбках | |
else if (nArgs == 1) { | |
w = h = 1; | |
size = 300; | |
} | |
else if (nArgs == 2) { | |
w = 2; h = 1; | |
size = 300; | |
} | |
else if (nArgs == 3 || nArgs == 4) { | |
w = 2; h = 2; | |
size = 300; | |
} | |
else if (nArgs == 5 || nArgs == 6) { | |
w = 3; h = 2; | |
size = 200; | |
} | |
else if (nArgs == 7 || nArgs == 8) { | |
w = 4; h = 2; | |
size = 200; | |
} | |
else { | |
w = 4; h = 3; | |
size = 150; | |
} | |
bigImage = cvCreateImage(cvSize(100 + size*w, 60 + size*h), 8, 3); | |
va_list args; | |
va_start(args, nArgs); | |
// цикл по varargs'ам | |
for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size)) { | |
img = va_arg(args, IplImage*); | |
// не рисуем NULL | |
if (img == 0) { | |
printf("Invalid arguments"); | |
cvReleaseImage(&bigImage); | |
return; | |
} | |
// получаем размеры изображения | |
x = img->width; | |
y = img->height; | |
// определяем какую сторону надо скейлить | |
max = (x > y) ? x : y; | |
scale = (float)((float)max / size); | |
// считаем отступы | |
if (i % w == 0 && m != 20) { | |
m = 20; | |
n += 20 + size; | |
} | |
// выставляем куда рисовать картинку | |
cvSetImageROI(bigImage, cvRect(m, n, (int)(x / scale), (int)(y / scale))); | |
// рисуем картинку | |
cvResize(img, bigImage); | |
cvResetImageROI(bigImage); | |
} | |
va_end(args); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
char* filename = argc == 2 ? argv[1] : "C:\\image.jpg"; | |
image = cvLoadImage(filename, 1); | |
// для каждого преобразования клонируем картинку | |
erode = cvCloneImage(image); | |
dilate = cvCloneImage(image); | |
gradient = cvCloneImage(image); | |
printf("[i] image: %s\n", filename); | |
assert(image != 0); | |
cvNamedWindow("original", CV_WINDOW_AUTOSIZE); | |
cvCreateTrackbar("Kernel radius", "original", &radius, radius_max, myTrackbarRadius); | |
cvCreateTrackbar("Transoforms count", "original", &iterations, iterations_max, myTrackbarIterations); | |
while (1) { | |
IplConvKernel* Kern = cvCreateStructuringElementEx(radius * 2 + 1, radius * 2 + 1, radius, radius, CV_SHAPE_ELLIPSE); | |
IplImage* Temp = 0; | |
Temp = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1); | |
cvErode(image, erode, Kern, iterations); | |
cvDilate(image, dilate, Kern, iterations); | |
cvMorphologyEx(image, gradient, Temp, Kern, CV_MOP_GRADIENT, iterations); | |
//создаём одну картинку из четырёх | |
cvShowManyImages(4, image, erode, dilate, gradient); | |
// показываем результат | |
cvShowImage("original", bigImage); | |
cvReleaseStructuringElement(&Kern); | |
cvReleaseImage(&Temp); | |
char c = cvWaitKey(33); | |
if (c == 27) { // выходим по ESC | |
break; | |
} | |
// удаляем большую картинку | |
cvReleaseImage(&bigImage); | |
} | |
// удаляем картинки | |
cvReleaseImage(&image); | |
cvReleaseImage(&gradient); | |
cvReleaseImage(&erode); | |
cvReleaseImage(&dilate); | |
// удаляем окно | |
cvDestroyWindow("original"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment