⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paint.c

📁 Dev c++, opencv, image segmentation and paint type image generation
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include "paint.h"



node *f1, *r1;  
int main(int argc, char *argv[])
{
    const char* filename = argc >=2 ? argv[1] : "input.jpg";
    
  IplImage* img;
  IplImage* img1;
  IplImage* img2; 
  IplImage* img3;
  IplImage* img4;
  IplImage* img5;
  IplImage* img6;
  IplImage* img7;
  IplImage* img8;
  IplImage* img9;
  int height;
  int width;
  int step;
  int channels;
  uchar *data;
 
 r1=f1=NULL;
 
  img= cvLoadImage( filename, CV_LOAD_IMAGE_COLOR );// load an image 
  //img=cvLoadImage(argv[1]);
  
  if(!img){
    printf("Could not load image file: %s\n",argv[1]);
    exit(0);
  }

  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  printf("Processing a %dx%d image with %d channels\n",height,width,channels); 

  // create a windows
  cvNamedWindow("Original", CV_WINDOW_AUTOSIZE); 
  cvNamedWindow("Original Painting", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Sobel Painting", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Laplace Painting", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Average Painting", CV_WINDOW_AUTOSIZE);
  cvNamedWindow("Half Toning", CV_WINDOW_AUTOSIZE);
  cvShowImage("Original", img);
 
 //apply transforms
  img2 = segmentation(img);
  img5 = laplace(img);
  img3 = sobel(img);
  img4 = segmentation(img3);
  img6 = segmentation(img5);
  img7 = average(img);
  img8 = segmentation(img7);
  img9 = halfToning(img);
  
  //show images
  cvShowImage("Original Painting", img2);
  cvShowImage("Sobel Painting", img4);
  cvShowImage("Laplace Painting", img6);
  cvShowImage("Average Painting", img8);
  cvShowImage("Half Toning", img9);
  
  // wait for a key to release the image
  cvWaitKey(0);
  cvDestroyWindow( "Original");
  cvDestroyWindow( "Original Painting");
  cvDestroyWindow( "Sobel Painting");
  cvDestroyWindow( "Laplace Painting");
  cvDestroyWindow( "Average Painting");
  cvDestroyWindow( "Half Toning");
  
  //destroy the image
  cvReleaseImage(&img );
  cvReleaseImage(&img2 );
  cvReleaseImage(&img3 );
  cvReleaseImage(&img4 );
  cvReleaseImage(&img5 );
  cvReleaseImage(&img6 );
  cvReleaseImage(&img7 );
  cvReleaseImage(&img8 );
  cvReleaseImage(&img9 );
  return 0;
}

//convert rgb to grayscale
IplImage* rgb2gray(IplImage* img)
{
 IplImage* grayScaleImage;
 int height,width,step,channels;
 uchar *data, *data1;
 int i = 0, j = 0, r, g, b;
 height    = img->height;
 width     = img->width;
 step      = img->widthStep;
 channels  = img->nChannels;
 data      = (uchar *)img->imageData;
  
  grayScaleImage = cvCreateImage(cvSize(width, height),8,1);
  data1     = (uchar *)grayScaleImage->imageData;
 for(i=0;i<height;i++) 
  {
     for(j=0;j<width;j++) 
     {
           b = data[i*step+j*channels+0];
           r = data[i*step+j*channels+1];
           g = data[i*step+j*channels+2];
           
           data1[i*grayScaleImage->widthStep+j+0] = ( r + g + b ) / 3;
     }
  }
  
  return grayScaleImage;  
}

//sobel operator
IplImage* sobel(IplImage* img)
{
          
   unsigned int	X, Y;
   int	        I, J;
   long			sumX, sumY;
   int			SUM;
   int			GX[3][3];
   int			GY[3][3];
   
   GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
   GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
   GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;

   
   GY[0][0] =  1; GY[0][1] =  2; GY[0][2] =  1;
   GY[1][0] =  0; GY[1][1] =  0; GY[1][2] =  0;
   GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;       
          
 IplImage* sobelImage;
 int height,width,step,channels;
 uchar *data, *data1;
 int i = 0, j = 0, r, g, b, x1, y1;
 
 height    = img->height;
 width     = img->width;
 step      = img->widthStep;
 channels = img->nChannels;
 data      = (uchar *)img->imageData;
   
  sobelImage = cvCreateImage(cvSize(width, height), img->depth, channels);
  data1     = (uchar *)sobelImage->imageData;
  
   struct rgb rgbval[height][width];
   double intensity[height][width];
  
   for(y1=0;y1<height;y1++) {
      for(x1=0;x1<width;x1++) {
          
          intensity[y1][x1] = (data[y1*step+x1*channels+0] + 
                                  data[y1*step+x1*channels+1] + 
                                     data[y1*step+x1*channels+2])/3;
      }
   }
                                  
          
 for(Y=0;Y<height; Y++)  {
	for(X=0; X<width; X++)  {
	     sumX = 0;
	     sumY = 0;

// image boundaries 
	     if(Y==0 || Y==height-1)
		  SUM = 0;
	     else if(X==0 || X==width-1)
		  SUM = 0;

// Convolution starts here 
	     else   {

//------X GRADIENT APPROXIMATION------
	       for(I=-1; I<=1; I++)  {
		   for(J=-1; J<=1; J++)  {
		   sumX = sumX + (int)(intensity[Y+I][X+J] * GX[I+1][J+1]);
           }
	       }
	       if(sumX>255)  sumX=255;
	       if(sumX<0)    sumX=0;

//-------Y GRADIENT APPROXIMATION-------
	       for(I=-1; I<=1; I++)  {
		   for(J=-1; J<=1; J++)  {
		       sumY = sumY + (int)(intensity[Y+I][X+J] * GX[I+1][J+1]);
           }
	       }
	       if(sumY>255)   sumY=255;
	       if(sumY<0)     sumY=0;

	       SUM = abs(sumX) + abs(sumY); //---GRADIENT MAGNITUDE APPROXIMATION----
             }
          struct rgb rgbtemp;
          struct rgb rgbtemp1;
          rgbtemp.blue = data[Y*step+X*channels+0];
          rgbtemp.green = data[Y*step+X*channels+1];
          rgbtemp.red = data[Y*step+X*channels+2];
          struct hsi hsitemp = rgb2hsi(rgbtemp);
          
          hsitemp.intensity = SUM;
          rgbtemp1 = hsi2rgb(hsitemp);
          data1[Y*step+X*channels+0] = rgbtemp1.blue*0.3 + rgbtemp.blue*0.7;
          data1[Y*step+X*channels+1] = rgbtemp1.green*0.3 + rgbtemp.green*0.7;
          data1[Y*step+X*channels+2] = rgbtemp1.red*0.3 + rgbtemp.red*0.7;            
	}
   }
 return sobelImage;
}
   
//laplacean operator
 IplImage* laplace(IplImage* img)
{
          
   unsigned int	X, Y;
   int	        I, J;
   int			SUM;
   int          MASK[5][5]; 
    /* 5x5 Laplace mask.  Ref: Myler Handbook p. 135 */
   MASK[0][0] = -1; MASK[0][1] = -1; MASK[0][2] = -1; MASK[0][3] = -1; MASK[0][4] = -1;
   MASK[1][0] = -1; MASK[1][1] = -1; MASK[1][2] = -1; MASK[1][3] = -1; MASK[1][4] = -1;
   MASK[2][0] = -1; MASK[2][1] = -1; MASK[2][2] = 24; MASK[2][3] = -1; MASK[2][4] = -1;
   MASK[3][0] = -1; MASK[3][1] = -1; MASK[3][2] = -1; MASK[3][3] = -1; MASK[3][4] = -1;
   MASK[4][0] = -1; MASK[4][1] = -1; MASK[4][2] = -1; MASK[4][3] = -1; MASK[4][4] = -1;              
 IplImage* laplaceImage;
 int height,width,step, channels;
 uchar *data, *data1;
 int i = 0, j = 0, r, g, b;
 height    = img->height;
 width     = img->width;
 step      = img->widthStep;
 channels = img->nChannels;
 data      = (uchar *)img->imageData;
   
  laplaceImage = cvCreateImage(cvSize(width, height), img->depth, channels);  
  data1     = (uchar *)laplaceImage->imageData;
          
   struct rgb rgbval[height][width];
   double intensity[height][width];
   int y1, x1;
   for(y1=0;y1<height;y1++) {
      for(x1=0;x1<width;x1++) {
          
          intensity[y1][x1] = (data[y1*step+x1*channels+0] + 
                                  data[y1*step+x1*channels+1] + 
                                     data[y1*step+x1*channels+2])/3;
      }
   }
 
 for(Y=0; Y<=height-1; Y++)  {
	for(X=0; X<=width-1; X++)  {
	     SUM = 0;

	  // image boundaries 
	  if(Y==0 || Y==1 || Y==height-2 || Y==height-1)
		  SUM = 0;
	  else if(X==0 || X==1 || X==width-2 || X==width-1)
		  SUM = 0;

	  // Convolution starts here 
	  else   {
	     for(I=-2; I<=2; I++)  {
		 for(J=-2; J<=2; J++)  {
                   SUM = SUM + (int)(intensity[Y+I][X+J] * MASK[I+2][J+2]);
		 }
	     }
	  }
	     if(SUM>255)  SUM=255;
	     if(SUM<0)    SUM=0;

          struct rgb rgbtemp;
          struct rgb rgbtemp1;
          rgbtemp.blue = data[Y*step+X*channels+0];
          rgbtemp.green = data[Y*step+X*channels+1];
          rgbtemp.red = data[Y*step+X*channels+2];
          struct hsi hsitemp = rgb2hsi(rgbtemp);
          
          hsitemp.intensity = SUM;
          rgbtemp1 = hsi2rgb(hsitemp);
          data1[Y*step+X*channels+0] = rgbtemp1.blue*0.3 + rgbtemp.blue*0.7;
          data1[Y*step+X*channels+1] = rgbtemp1.green*0.3 + rgbtemp.green*0.7;
          data1[Y*step+X*channels+2] = rgbtemp1.red*0.3 + rgbtemp.red*0.7;  
	}
   }
     return   laplaceImage;  
}

//rgb to hsi conversion
struct hsi rgb2hsi(struct rgb rgbval)
{
  struct hsi hsival;      
  double theta;
  int r = rgbval.red;
  int g = rgbval.green;
  int b = rgbval.blue;
  double h;
  double s;
  int i;
  
  
  if(r == g && g == b && r == b)
  {
       h = M_PI_2;
       s = 0;
  }
  else
  {    
       theta = acos((0.5*(r-g+r-b))/sqrt(((r-g)*(r-g))+(r-b)*(g-b)));
       if(b <= g)
            h = theta;
       else
            h = 2*M_PI - theta;
      
       if(r <= g && r <= b)
            s = 1.0 - ((3.0*r)/(double)(r+g+b));
       else if(g <= r && g <= b)
            s = 1.0 - ((3.0*g)/(double)(r+g+b));
       else
           s = 1.0 - ((3.0*b)/(double)(r+g+b));
  }
  i = (r+g+b)/3;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -