Reputation: 39
I'm trying to create a kaleidoscope from an image using opencv and am trying to grasp the mathematics involved. I understand that I have to segment the image into pieces, reflect it and rotate it 60 degrees creating a polygon of 6 triangles. What I am not understanding is how to break the image up and which pieces of the image to use and which to discard. Any ideas or links would be greatly appreciated.
Upvotes: 0
Views: 1225
Reputation: 3550
here you will find a sample implementation using OpenCV
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img,img2,kal1(400,400,CV_8UC3,Scalar(0,0,0));
Mat kal2,add;
VideoCapture cap(0);
int i=0,j=0;
for(;;)
{
cap>>img;
resize(img,img,Size(400,400));
for(i=0;i<600;i++)
{
for(j=0;j<i/3;j++)
{
kal1.at<uchar>(Point(i,j))=img.at<uchar>(Point(i,j));//gives 1/8 of img
if(j>i)
{
continue;
}
}
}
flip(kal1,kal2,0);
addWeighted(kal1,1,kal2,1,0,add);
flip(kal2,kal2,1);
addWeighted(add,1,kal2,1,0,add);
flip(kal2,kal2,0);
addWeighted(add,1,kal2,1,0,add);
transpose(kal2,kal2);
addWeighted(add,1,kal2,1,0,add);
flip(kal2,kal2,0);
addWeighted(add,1,kal2,1,0,add);
flip(kal2,kal2,1);
addWeighted(add,1,kal2,1,0,add);
flip(kal2,kal2,0);
addWeighted(add,1,kal2,1,0,add);
line(img,Point(200,0),Point(200,200),Scalar(0,255,255),1);
line(img,Point(0,0),Point(200,200),Scalar(0,255,255),1);
moveWindow("img",30,30);
moveWindow("Kaleidoscope",500,30);
imshow("img",img);
imshow("Kaleidoscope",add);
waitKey(10);
}
return 1;
}
Upvotes: 1
Reputation: 1412
The math part seems to be the important part of your assignment, so I'll leave that up to you. However, if you think of the problem backwards, it might be slightly easier. Start with the kaleidoscope fractal pattern and determine how each segment in that pattern is created by the original image.
Once you have the pattern, all you have to do is make a map that maps each pixel in the pattern to a pixel in the image and use an OpenCV function such as remap to do the actual mapping.
Upvotes: 0