Skip to content

Commit 87bf6ff

Browse files
committedDec 23, 2016
OpenCV Filtro Twirl
Aplicar la tranformación twirl a una imagen con OpenCV.
1 parent 3a1fbdd commit 87bf6ff

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
 

‎filtro-twirl/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.7)
2+
3+
project( "OpenCV-Filtro_Twirl" )
4+
5+
find_package( OpenCV REQUIRED )
6+
7+
file(COPY ../data/lena.jpg DESTINATION data)
8+
9+
add_executable( ${PROJECT_NAME} twirl.cpp )
10+
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

‎filtro-twirl/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Flitro Twirl
2+
3+
En este tutorial aprenderemos a aplicar la transformación 2D llamada en inglés **Twirl**
4+
5+
Podemos aplicar el efecto a una imagen completa o a una región definida de la misma.
6+
7+
*Filtro Twirl en OpenCV*
8+
9+
![OpenCV Filtro Twirl](image/twirl-opencv.png "aplicar filtro twirl")
10+
11+
La documentación está disponible en [Tutor de Programación](http://acodigo.blogspot.com)

‎filtro-twirl/image/twirl-opencv.png

556 KB
Loading

‎filtro-twirl/twirl.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <opencv2/opencv.hpp>
4+
5+
using namespace cv;
6+
using namespace std;
7+
8+
void trackbar_callback(int value, void* userdata)
9+
{
10+
Mat* image = (Mat*)userdata;
11+
12+
const float width = (float)image->rows;
13+
const float height = (float)image->cols;
14+
15+
Mat result(image->rows, image->cols, image->type());
16+
17+
for (int i = 0; i < image->rows; i++) {
18+
for (int j = 0; j < image->cols; j++) {
19+
20+
float x = (j / height) - 0.5f;
21+
float y = (i / width) - 0.5f;
22+
23+
float angle = atan2f(y, x);
24+
float radius = sqrtf((x * x) + (y * y));
25+
26+
angle += radius * (value / 10.0f);
27+
28+
float xr = ((radius * sinf(angle)) + 0.5f) * width;
29+
float yr = ((radius * cosf(angle)) + 0.5f) * height;
30+
31+
int k = (int)std::min(width - 1, std::max(0.0f, xr));
32+
int m = (int)std::min(height - 1, std::max(0.0f, yr));
33+
34+
uchar* src = image->ptr<uchar>(k, m);
35+
uchar* out = result.ptr<uchar>(i, j);
36+
37+
out[0] = src[0];
38+
out[1] = src[1];
39+
out[2] = src[2];
40+
}
41+
}
42+
43+
imshow("Result Image", result);
44+
}
45+
46+
int main(int argc, char** argv)
47+
{
48+
Mat image = imread("data/lena.jpg", CV_LOAD_IMAGE_COLOR);
49+
50+
if (image.empty())
51+
{
52+
printf("No image data \n");
53+
return -1;
54+
}
55+
56+
try
57+
{
58+
const cv::String name_window = "Twirl Image";
59+
const cv::String name_trackbar = "Twirl";
60+
61+
namedWindow(name_window);
62+
createTrackbar(name_trackbar, name_window, NULL, 200, trackbar_callback, &image);
63+
setTrackbarPos(name_trackbar, name_window, 80);
64+
imshow(name_window, image);
65+
66+
waitKey(0);
67+
destroyAllWindows();
68+
}
69+
catch (cv::Exception& e)
70+
{
71+
const char* err_msg = e.what();
72+
std::cout << "exception caught: " << err_msg << std::endl;
73+
}
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)
Please sign in to comment.