简体   繁体   English

使用OpenCV从图像创建万花筒效果

[英]Creating a kaleidoscope effect from an image using opencv

I'm trying to create a kaleidoscope from an image using opencv and am trying to grasp the mathematics involved. 我正在尝试使用opencv从图像创建万花筒,并试图掌握所涉及的数学。 I understand that I have to segment the image into pieces, reflect it and rotate it 60 degrees creating a polygon of 6 triangles. 我知道我必须将图像分段,反射并旋转60度,以创建6个三角形的多边形。 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. 任何想法或链接将不胜感激。

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. 有了图案后,您要做的就是制作一个将图案中的每个像素映射到图像中的像素的映射 ,并使用OpenCV函数(例如remap)进行实际映射。

here you will find a sample implementation using OpenCV 在这里您将找到使用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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM