For people working in computer vision, openCv is godsend. The following is code implementation for creating an optical illusion. This involves creating a pencil sketch and some more simple filtering application. Check out the illusion here. Direct Access Header: It makes accessing individual pixels in openCv way easier
#ifndef DACCESS_H
#define DACCESS_H

//Wrapper class to access openCV pixel values in a easier way

template class Image
{
  private:
  IplImage* imgp;
  public:
  Image(IplImage* img=0) {imgp=img;}
  ~Image(){imgp=0;}
  void operator=(IplImage* img) {imgp=img;}
  inline T* operator[](const int rowIndx) {
    return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));}
};

typedef struct{
  unsigned char b,g,r;
} RgbPixel;

typedef struct{
  float b,g,r;
} RgbPixelFloat;

typedef Image       RgbImage;
typedef Image  RgbImageFloat;
typedef Image  BwImage;
typedef Image          BwImageFloat;

#endif
operation.h Blending operations made easy
#define ChannelBlend_Normal(A)     ((unsigned char)(A))
#define ChannelBlend_Invert(A)     ((unsigned char)(255-A))
#define ChannelBlend_Lighten(A,B)    ((unsigned char)((B > A) ? B:A))
#define ChannelBlend_Darken(A,B)     ((unsigned char)((B > A) ? A:B))
#define ChannelBlend_Multiply(A,B)   ((unsigned char)((A * B) / 255))
#define ChannelBlend_Average(A,B)    ((unsigned char)((A + B) / 2))
#define ChannelBlend_Add(A,B)        ((unsigned char)(min(255, (A + B))))
#define ChannelBlend_Subtract(A,B)   ((unsigned char)((A + B < 255) ? 0:(A + B - 255)))
#define ChannelBlend_Difference(A,B) ((unsigned char)(abs(A - B)))
#define ChannelBlend_Negation(A,B)   ((unsigned char)(255 - abs(255 - A - B)))
#define ChannelBlend_Screen(A,B)     ((unsigned char)(255 - (((255 - A) * (255 - B)) >> 8)))
#define ChannelBlend_Exclusion(A,B)  ((unsigned char)(A + B - 2 * A * B / 255))
#define ChannelBlend_Overlay(A,B)    ((unsigned char)((B < 128) ? (2 * A * B / 255):(255 - 2 * (255 - A) * (255 - B) / 255)))
#define ChannelBlend_SoftLight(A,B)  ((unsigned char)((B < 128)?(2*((A>>1)+64))*((float)B/255):(255-(2*(255-((A>>1)+64))*(float)(255-B)/255))))
#define ChannelBlend_HardLight(A,B)  (ChannelBlend_Overlay(B,A))
#define ChannelBlend_ColorDodge(A,B) ((unsigned char)((B == 255) ? B:min(255, ((A << 8 ) / (255 - B)))))
#define ChannelBlend_ColorBurn(A,B)  ((unsigned char)((B == 0) ? B:max(0, (255 - ((255 - A) << 8 ) / B))))
#define ChannelBlend_LinearDodge(A,B)(ChannelBlend_Add(A,B))
#define ChannelBlend_LinearBurn(A,B) (ChannelBlend_Subtract(A,B))
#define ChannelBlend_LinearLight(A,B)((unsigned char)(B < 128)?ChannelBlend_LinearBurn(A,(2 * B)):ChannelBlend_LinearDodge(A,(2 * (B - 128))))
#define ChannelBlend_VividLight(A,B) ((unsigned char)(B < 128)?ChannelBlend_ColorBurn(A,(2 * B)):ChannelBlend_ColorDodge(A,(2 * (B - 128))))
#define ChannelBlend_PinLight(A,B)   ((unsigned char)(B < 128)?ChannelBlend_Darken(A,(2 * B)):ChannelBlend_Lighten(A,(2 * (B - 128))))
#define ChannelBlend_HardMix(A,B)    ((unsigned char)((ChannelBlend_VividLight(A,B) < 128) ? 0:255))
#define ChannelBlend_Reflect(A,B)    ((unsigned char)((B == 255) ? B:min(255, (A * A / (255 - B)))))
#define ChannelBlend_Glow(A,B)       (ChannelBlend_Reflect(B,A))
#define ChannelBlend_Phoenix(A,B)    ((unsigned char)(min(A,B) - max(A,B) + 255))
#define ChannelBlend_Alpha(A,B,O)    ((unsigned char)(O * A + (1 - O) * B))
#define ChannelBlend_AlphaF(A,B,F,O) (ChannelBlend_Alpha(F(A,B),A,O))
Now the main code
#include 
#include 
#include 
#include 
#include 
#include "dAccess.h"
#include "operations.h"

//Parameters
#define SIGMA1			3.0
#define SIGMA2			6.0
#define LINEWIDTH       4

using namespace std;

int main(int argc, char *argv[])
{
  //original image
  IplImage* usrImg = NULL;
  //Resized image
  IplImage *img = NULL;
  //grayscale versions
  IplImage* gimg1 = NULL;
  IplImage* gimg2 = NULL;

  //Initialize fonts
  CvFont font;
  cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1.0,1.0);

  if(argcwidth/usrImg->height;

  if(usrImg->width height width height width,usrImg->height),usrImg->depth, usrImg->nChannels);
  else if(targetAspect > sourceAspect)
	img = cvCreateImage(cvSize((int)(800*sourceAspect),600),usrImg->depth, usrImg->nChannels);
  else
	img = cvCreateImage(cvSize(800,(int)(600/sourceAspect)),usrImg->depth, usrImg->nChannels);

  cvResize(usrImg,img);
  cvReleaseImage(&usrImg);

  //Convert to pencil art--------------------------------------------------------------------------
  //Convert to grayscale
  gimg1 = cvCreateImage(cvSize(img->width,img->height), IPL_DEPTH_8U, 1);
  cvCvtColor(img,gimg1,CV_BGR2GRAY);

  //Make a copy
  gimg2 = cvCreateImage(cvSize(img->width,img->height), IPL_DEPTH_8U, 1);
  cvCopy(gimg1,gimg2);
  //Create direct access to pixel values
  BwImage  gimg1Daccess(gimg1);
  BwImage  gimg2Daccess(gimg2);

  //Apply Gaussian filter
  cvSmooth(gimg2,gimg2,CV_GAUSSIAN,0,0,SIGMA1);
  //Invert it; gimp2 = 1-gimp2;
  for(int i=0;iheight;i++)
	  for(int j=0;jwidth;j++)
		  gimg2Daccess[i][j] = ChannelBlend_Invert(gimg2Daccess[i][j]);
  //Merge at 50% opacity; gimg1 = 0.5*gimg1 + 0.5*gimg2;
  for(int i=0;iheight;i++)
	  for(int j=0;jwidth;j++)
		  gimg1Daccess[i][j] = ChannelBlend_Alpha(gimg1Daccess[i][j],gimg2Daccess[i][j],0.5);

  //Color Dodge gimg1 by itself
  for(int i=0;iheight;i++)
	  for(int j=0;jwidth;j++)
 		  gimg1Daccess[i][j] = ChannelBlend_ColorDodge(gimg1Daccess[i][j],gimg1Daccess[i][j]);
  //Done
  //Create a grid of lines------------------------------------------------------------------------
  for(int i=0;iheight;i++)
	  for(int j=0;jwidth;j++)
		  if(i > img->height - WATERMARK_H || ((int)(j/LINEWIDTH))%2 == 0)
			gimg2Daccess[i][j] = 255;
		  else
			gimg2Daccess[i][j] = 0;
  //Gaussian blur gimg1
  cvSmooth(gimg1,gimg1,CV_GAUSSIAN,0,0,SIGMA2);
  //Overlay both images
  for(int i=0;iheight;i++)
	  for(int j=0;jwidth;j++)
 		  gimg1Daccess[i][j] = ChannelBlend_Multiply(gimg1Daccess[i][j],gimg2Daccess[i][j]);

  //Save the image
  cvSaveImage("illusion.png",gimg1);

  system("illusion.htm");

  // wait for a key
  cout<
Print This Post Print This Post
Tagged with:  
  • I didn’t understand anything in this post. You should describe your information. 

    • Piterblood

      lamer

  • Sujairrigation

    DRIP EMITTERS
    Drip irrigation is one such technology, which has assumed great importance in the recent times. In the present situation when the irrigated area is increasing, ground water table is dwindling and availability of surface water for irrigation is getting reduced, drip irrigation is the only answer to meet the growing demand of water. Drip Irrigation is the most modern technique of applying water directly to the root zone of the plant in right quantity at the right time and at the right place.For More Details visit http://sujayirrigations.com/

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives