1/28/2015

Canny edge detector, example source code in opencv

Canny edge processing example



cpu version result



gpu version result






...
cpu version code.
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
//#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //video input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;
 namedWindow("origin",0);
 namedWindow("canny",0);

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  resize(o_frame, showMat_r, Size(RWIDTH, RHEIGHT) );  
  Canny(showMat_r, showMat_r2, 50, 100);

  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(5) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}

///

gpu version code
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 gpu::GpuMat o_frame_gpu;
 gpu::GpuMat r_frame_gpu;
 gpu::GpuMat rg_frame_gpu;
 gpu::GpuMat r_frame_gpu2;

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  //upload to gpumat
  o_frame_gpu.upload(o_frame);  
  gpu::resize(o_frame_gpu, r_frame_gpu, Size(RWIDTH, RHEIGHT) );  
  gpu::cvtColor(r_frame_gpu, rg_frame_gpu, CV_BGR2GRAY);
  gpu::Canny(rg_frame_gpu, r_frame_gpu2, 50, 100); //gray only

  //download to mat
  r_frame_gpu.download(showMat_r);  
  r_frame_gpu2.download(showMat_r2);  

  
  //show image
  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(10) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}



1/26/2015

Real-time yard trailer identification by detection of vehicle ID numbers

Real-time yard trailer identification by detection of vehicle ID numbers

Project period : (2013.09~2013.11)


*Introduction
• Y/T(Yard Trailer) Y/T(Yard Trailer) number identification solution using image processing.
• The solution using camera is easy to installation and maintance compare to the RFID. And It is more free from distance constraint.
• Machine learning methods - SVM (Support Vector Machine), MLP (Multi Layers Perceptron) are used to recognize the number ID
• A high-speed image processing through the GPU parallel programming


*Real-time pre-processing for features extraction
• The process of preprocessing for ID number extraction
-In the first step, we apply different filters, morphological operations, contour algorithms, and validations to retrieve those parts of the image that could have targeted region.
-Especially, we targeted to detect a ventilating opening instead of number ID, because that target is less shape change than the 3 characters of number ID.



*Vehicle Identification
• HOG(Histogram of gradient) feature extraction and SVM machine learning to detect a ventilating opening


• Each segmented character is to extract the features for training and classifying the MLP algorithm
• The feature is horizontal, vertical histogram values from 5x5 low resolution image.


*Experiment
• Recognition rate over the 95%
• Detection speed about 0.05 sec/frame (Image size : 1280x720, Intel® core™ i5-3570 cpu 3.40GHz, NVIDIA Geforce GTX 650)
• The trailer enter speed about 20~30 km/h


#include < stdio.h>

#include "ShinPortOCR.h"


void main()
{

 ShinPortOCR cShinPortOCR;

 //printf("μ—°μ†μœΌλ‘œ 읽을 이미지 파일 갯수? (ex:10 -> ./data/1.jpg, ./data/2.jpg ... ./data/10.jpg) \n");
 printf("How many images do you want to test? (ex:100, 500,  1630\n");
 int num;
 scanf_s("%d", &num);

 int p = 0, n = 0;
 char str[100];
 for (int i = 0; i< num; ++i)
 {
  printf("%d/%d\n", i, num);

  sprintf_s(str, "./data/%d.jpg", i + 1);
  Mat inImg = imread(str, 1);//, CV_LOAD_IMAGE_GRAYSCALE);
  Mat OutImg; 
  if (cShinPortOCR.GoGoXing(inImg, OutImg, 1) == -111) //1 is debug print, 0 is no dubug out
  {
   sprintf_s(str, ".\\Log\\fail\\%d.jpg", i + 1);
   imwrite(str, inImg);
  }
  else{
   sprintf_s(str, ".\\Log\\success\\%d.jpg", i + 1);
   imwrite(str, inImg);
  }


  sprintf_s(str, ".\\Log\\processing\\%d.jpg", i + 1);
  imwrite(str, OutImg);


  imshow("result", OutImg);
  waitKey(10);

 }
}


///

Source code is here
https://github.com/MareArts/Container-Yard-Trailer-ID-number-recognition

you can down opencv dll/lib/header files on here
opencv 249 64bit cuda 60
https://www.amazon.com/clouddrive/share/7bPR5HgbCbNZJHwG0ldq1gwHtydLXRxtQVYc5JYPlSF?ref_=cd_ph_share_link_copy


The method to check that camera is moving or not(stop) using dense optical flow. (applied opencv, gpu version)

This post introduces how to check if the camera(video) is moving or not.


This method can be applied various fields.
This approach also means what scene is important or not.

To solve this problem, I used a dense optical flow.
I introduce dense optical flow on youtube.
http://www.youtube.com/watch?v=yAz1qrN6T_o
http://www.youtube.com/watch?v=iRMqH6y6JKU

and on my blog
http://feelmare.blogspot.kr/search/label/dense%20optical%20flow

In example source code..
The algorithm is checking that how much percent of area is moving?
And, check distance of each pixel movement. Smaller than the threshold value does not include in the movement percent.

Note, the example source code is made by gpu version.
#include < stdio.h>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2\gpu\gpu.hpp>
#include < opencv2\nonfree\features2d.hpp >    



#ifdef _DEBUG        
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing
#pragma comment(lib, "opencv_objdetect249d.lib") //HOGDescriptor
#pragma comment(lib, "opencv_gpu249d.lib")
#pragma comment(lib, "opencv_features2d249d.lib")
#pragma comment(lib, "opencv_highgui249d.lib")
#else
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_imgproc249.lib")
#pragma comment(lib, "opencv_objdetect249.lib")
#pragma comment(lib, "opencv_gpu249.lib")
#pragma comment(lib, "opencv_features2d249.lib")
#pragma comment(lib, "opencv_highgui249.lib")
#endif 

using namespace std;
using namespace cv;

#define WIDTH_DENSE (80)
#define HEIGHT_DENSE (60)

#define DENSE_DRAW 0 //dense optical flow arrow drawing or not
#define GLOBAL_MOTION_TH1 1
#define GLOBAL_MOTION_TH2 70


float drawOptFlowMap_gpu (const Mat& flow_x, const Mat& flow_y, Mat& cflowmap, int step, float scaleX, float scaleY, int drawOnOff);


int main()
{
 //stream /////////////////////////////////////////////////
 VideoCapture stream1("C:\\videoSample\\medical\\HUV-03-14.wmv"); 

 //variables /////////////////////////////////////////////
 Mat O_Img; //Mat
 gpu::GpuMat O_Img_gpu; //GPU
 gpu::GpuMat R_Img_gpu_dense; //gpu dense resize
 gpu::GpuMat R_Img_gpu_dense_gray_pre; //gpu dense resize gray
 gpu::GpuMat R_Img_gpu_dense_gray; //gpu dense resize gray
 gpu::GpuMat flow_x_gpu, flow_y_gpu;
 Mat flow_x, flow_y;

 //algorithm *************************************
 //dense optical flow
 gpu::FarnebackOpticalFlow fbOF;
 

 //running once //////////////////////////////////////////
 if(!(stream1.read(O_Img))) //get one frame form video
 {
  printf("Open Fail !!\n");
  return 0; 
 }

  //for rate calucation
 float scaleX, scaleY;
 scaleX = O_Img.cols/WIDTH_DENSE;
 scaleY = O_Img.rows/HEIGHT_DENSE;

 O_Img_gpu.upload(O_Img); 
 gpu::resize(O_Img_gpu, R_Img_gpu_dense, Size(WIDTH_DENSE, HEIGHT_DENSE));
 gpu::cvtColor(R_Img_gpu_dense, R_Img_gpu_dense_gray_pre, CV_BGR2GRAY);


 //unconditional loop   ///////////////////////////////////
 while (true) {
  //reading
  if( stream1.read(O_Img) == 0) //get one frame form video   
   break;

  // ---------------------------------------------------
  //upload cou mat to gpu mat
  O_Img_gpu.upload(O_Img); 
  //resize
  gpu::resize(O_Img_gpu, R_Img_gpu_dense, Size(WIDTH_DENSE, HEIGHT_DENSE));
  //color to gray
  gpu::cvtColor(R_Img_gpu_dense, R_Img_gpu_dense_gray, CV_BGR2GRAY);
  
  //calculate dense optical flow using GPU version
  fbOF.operator()(R_Img_gpu_dense_gray_pre, R_Img_gpu_dense_gray, flow_x_gpu, flow_y_gpu);
  flow_x_gpu.download( flow_x );
  flow_y_gpu.download( flow_y );


  //calculate motion rate in whole image
  float motionRate = drawOptFlowMap_gpu(flow_x, flow_y, O_Img, 1, scaleX, scaleY, DENSE_DRAW);
  //update pre image
  R_Img_gpu_dense_gray_pre = R_Img_gpu_dense_gray.clone();



  //display "moving" or "stop" message on the image.
  if(motionRate > GLOBAL_MOTION_TH2 ) //if motion generate over than 70%, this algorithm consider that video is moving.
  {
   char TestStr[100] = "Moving!!";
   putText(O_Img, TestStr, Point(30,60), CV_FONT_NORMAL, 2, Scalar(0,0,255),3,2); //OutImg is Mat class;   
  }else{
   char TestStr[100] = "Stop!!";
   putText(O_Img, TestStr, Point(30,60), CV_FONT_NORMAL, 2, Scalar(255,0,0),3,2); //OutImg is Mat class; 
  }


  // show image ----------------------------------------
  imshow("Origin", O_Img);   

  // wait key
  if( cv::waitKey(100) > 30)
   break;
 }
}



float drawOptFlowMap_gpu (const Mat& flow_x, const Mat& flow_y, Mat& cflowmap, int step, float scaleX, float scaleY, int drawOnOff)
{
 double count=0;

 float countOverTh1 = 0;
 int sx,sy;
 for(int y = 0; y < HEIGHT_DENSE; y += step)
 {
  for(int x = 0; x < WIDTH_DENSE; x += step)
  {
   
   if(drawOnOff)
   {
    Point2f fxy;    
    fxy.x = cvRound( flow_x.at< float >(y, x)*scaleX + x*scaleX );   
    fxy.y = cvRound( flow_y.at< float >(y, x)*scaleY + y*scaleY );   
    line(cflowmap, Point(x*scaleX,y*scaleY), Point(fxy.x, fxy.y), CV_RGB(0, 255, 0));   
    circle(cflowmap, Point(fxy.x, fxy.y), 1, CV_RGB(0, 255, 0), -1);   
   }

   float xx = fabs(flow_x.at< float >(y, x) );
   float yy = fabs(flow_y.at< float >(y, x) );

   float xxyy = sqrt(xx*xx + yy*yy);
   if( xxyy > GLOBAL_MOTION_TH1 )
    countOverTh1 = countOverTh1 +1;
   
   count=count+1;
  }
 }
 return (countOverTh1 / count) * 100;

}


1/25/2015

MIL, Boosting tracker test in opencv 3.0

I have tested MIL, Boosting tracking algorithm in opencv 3.0

Please refer to official reference here -> http://docs.opencv.org/trunk/modules/tracking/doc/tracking.html

Firstly, to use tracker algorithm, you have to set the value which is tracking module path of OPENCV_EXTRA_MODUALES_PATH option, when you build the opencv 3.0.



We can download tracking module in Github. -> https://github.com/Itseez/opencv_contrib
"opencv_contrib" is developing separately with opencv full version.
In setting cmake, I checked only tracking option. because other module may be unstable version yet.
And I need just tracking module.


The example source code is referenced in here -> https://github.com/lenlen/opencv/blob/tracking_api/samples/cpp/tracker.cpp

In source code, you can select Mil and Boosting algorithm option in the function of creation.

At the result, MIL is faster than boosting.
But both algorithm are very slow and performance is also not good yet.
So they can not use in industrial application.


MIL result video

Boosting result video


The example source code is here





#include < stdio.h>  
#include < opencv2\video\tracker.hpp>
#include < opencv2\opencv.hpp>


 
#ifdef _DEBUG          
#pragma comment(lib, "opencv_core300d.lib")  
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib") 
#pragma comment(lib, "opencv_imgproc300d.lib") 
#pragma comment(lib, "opencv_cuda300d.lib")   
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_tracking300d.lib")
#define _DEBUG_RRRR  
#else  
#pragma comment(lib, "opencv_core300.lib")  
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_videoio300.lib") 
#pragma comment(lib, "opencv_imgproc300.lib") 
#pragma comment(lib, "opencv_cuda300.lib")   
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_tracking300d.lib")
#endif  


using namespace cv;
using namespace std;

static Mat image;
static bool selectObject = false;
static bool startSelection = false;
static Rect2d boundingBox;
static bool paused;

static void onMouse( int event, int x, int y, int, void* ) 
{ 
 if( !selectObject ) 
 { 
  switch ( event ) 
  { 
  case EVENT_LBUTTONDOWN: 
   //set origin of the bounding box 
   startSelection = true; 
   boundingBox.x = x; 
   boundingBox.y = y; 
   break; 
  case EVENT_LBUTTONUP: 
   //sei with and height of the bounding box 
   boundingBox.width = std::abs( x - boundingBox.x ); 
   boundingBox.height = std::abs( y - boundingBox.y ); 
   paused = false; 
   selectObject = true; 

   printf("Object Rect Size(left, right, width, height) %.1f %.1f %.1f %.1f\n", boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
   break; 
  case EVENT_MOUSEMOVE: 
   if( startSelection && !selectObject ) 
   { 
    //draw the bounding box 
    Mat currentFrame; 
    image.copyTo( currentFrame ); 
    rectangle( currentFrame, Point( boundingBox.x, boundingBox.y ), Point( x, y ), Scalar( 255, 0, 0 ), 2, 1 ); 
    imshow( "Tracking API", currentFrame ); 
   } 
   break; 
  } 
 } 
} 




int main()
{
 unsigned long AAtime=0, BBtime=0;

 VideoCapture cap;
 cap.open("C:\\videoSample\\tracking\\sample2.avi");

 if( !cap.isOpened() ) 
 { 
  printf("Video File Open Fail! \n");
  return -1; 
 } 


 Mat frame; 
 paused = false; 
 namedWindow( "Tracking", 0 ); 
 setMouseCallback( "Tracking", onMouse, 0 );


 //instantiates the specific Tracker 
 //MIL : TrackerMIL
 //BOOSTING : TrackerBoosting
 Ptr< Tracker> tracker = Tracker::create("BOOSTING" ); //"MIL");//
 if( tracker == NULL ) 
 { 
  printf("Error in the instantiation of the tracker..\n");
  return -1; 
 } 

 //get the first frame 
 cap >> frame; 
 frame.copyTo( image ); 
 imshow( "Tracking", image ); 

 bool initialized = false;
 while(1)
 { 
  if( !paused ) 
  { 
   cap >> frame; 
   frame.copyTo( image ); 


   if( !initialized && selectObject ) 
   { 
    //initializes the tracker 
    AAtime = getTickCount();
    if( !tracker->init( frame, boundingBox ) ) 
    { 
     printf("Could not initialize tracker\n"); 
     return -1; 
    } 
    BBtime = getTickCount();
    double fps = (BBtime - AAtime)/getTickFrequency();
    double sec = 1/fps;
    printf("Tracking Initial time = %.2lffsp, %.2lfsec \n",  fps, sec );

    initialized = true; 
   } 
   else if( initialized ) 
   { 
    AAtime = getTickCount();
    
    //updates the tracker 
    if( tracker->update( frame, boundingBox ) ) 
    { 
     rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 ); 
    } 

    BBtime = getTickCount();
    double fps = (BBtime - AAtime)/getTickFrequency();
    double sec = 1/fps;
    printf("Tracking update time = %.2lffsp, %.2lfsec \n",  fps, sec );

   } 
   imshow( "Tracking", image ); 
  } 


  char c = (char) waitKey( 2 ); 
  if( c == 'q' ) 
   break; 
  if( c == 'p' ) 
   paused = !paused; 


 } 
}

1/12/2015

example source code similar color matching in represent 20 colors (opencv, hsv to rgb, rgb to hsv, ycbcr to rgb, rgb to ycbcr, hsv to conic, hsv to cylindric)

What is the best way to find a similar color in 20 represent colors, when we have any color?

My represent 20 colors are bring from paint app in window.



RGB value of 20 colors are composed..

{0,0,0},  //0
 {127,127,127},  //1
 {136,0,21},
 {237,28,36},
 {255127,39},
 {255,242,0},
 {34,177,76},
 {0,162,232},
 {63,72,204},
 {163,73,164},
 {255,255,255}, //10
 {195,195,195}, //11
 {185,122,87},
 {255,174,201},
 {255,201,14},
 {239,228,176},
 {181,230,29},
 {153,217,234},
 {112,146,190},
 {200,191,231}

If any rgb color is input, firstly examine the saturation of the input color.
The lower saturation value means that close to gray family color.
Our sample code is determined to be gray when the saturation value is smaller than 20.
Input color is examined that standard deviation is lower than threshold.
The lower STD value means that close to gray family color.
Our sample code is determined to be gray when the STD value is smaller than 4.
The value of 4 is heuristic value.

When the input color is gray scale, the input color was matched only 0, 1, 10, 11 index in color table.

When the input color is not gray scale, out sample find the similarity in the following five methods.

1.
input rgb -> hsv -> conic   VS   represent 20 conic values

2.
input rgb -> hsv -> cylindric  VS  represent 20 cylidric values

3.
input rgb -> YCbCr   VS   represent 20 YCbCr values

4.
input rgb -> CbCr   VS   represent 20 CbCr values 

5.
input rgb VS represent 20 rgb

VS means uclidean distance.
The minimum distance was best mathicng color.

The input color is seletect by random.
The matching result is expressed by jpg file and imshow.



I hope  you determine what is the the best way in 5 method.
And there can be a better way.

More detail refer to example source code.

...
#define SAT_TH 20

#include < opencv2\opencv.hpp>
#include < string>
#include < stdio.h>
#include < time.h>

#ifdef _DEBUG        
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing
#pragma comment(lib, "opencv_highgui249d.lib")
#define _DEBUG_RRRR
#else
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_imgproc249.lib")
#pragma comment(lib, "opencv_highgui249.lib")
//#define _DEBUG_RRRR
#endif   

using namespace std;
using namespace cv;

struct RGB_QUAD{  
  int r, g, b;
};

RGB_QUAD base_rgb[20] = {
 {0,0,0}, //0
 {127,127,127}, //1
 {136,0,21},
 {237,28,36},
 {255127,39},
 {255,242,0},
 {34,177,76},
 {0,162,232},
 {63,72,204},
 {163,73,164},
 {255,255,255}, //10
 {195,195,195}, //11
 {185,122,87},
 {255,174,201},
 {255,201,14},
 {239,228,176},
 {181,230,29},
 {153,217,234},
 {112,146,190},
 {200,191,231}
};





string rgb_name[20] = 
 {"black",
 "gray",
 "Brown",
 "Red",
 "Orange",
 "Yellow",
 "Green",
 "Sky Blue",
 "Blue",
 "Violet",
 "White",
 "Light Gray",
 "Ocher",
 "Pink",
 "Light Orange",
 "Light Yellow",
 "Light Green",
 "Light Sky Blue",
 "Light Blue",
 "Light Violet"};


struct hsv_color {
    unsigned char h;        // Hue: 0 ~ 255 (red:0, gree: 85, blue: 171)
    unsigned char s;        // Saturation: 0 ~ 255
    unsigned char v;        // Value: 0 ~ 255
};

 hsv_color base_hsv[20] = 
 {{0, 0, 0},
 {0, 0, 127},

 {250, 255, 136},
 {255, 224, 237},
 {17, 216, 255},
 {40, 255, 255},
 {97, 206, 177},
 {141, 255, 232},
 {169, 176, 204},
 {213, 141, 164},

 {0, 0, 255},
 {0, 0, 195},

 {15, 135, 185},
 {242, 81, 255},
 {33, 241, 255},
 {35, 67, 239},
 {53, 222, 230},
 {138, 88, 234},
 {153, 104, 190},
 {180, 44, 231}};

 

struct my_xyz {
    float x; float y; float z;
};
 
my_xyz base_hsv_conic[20] = {
 {0.00, 0.00, 0.00},
 {0.00, 0.00, 127.00},
 {134.97, -16.74, 136.00},
 {208.19, -0.04, 237.00},
 {197.33, 87.85, 255.00},
 {140.86, 212.56, 255.00},
 {-104.47, 97.63, 177.00},
 {-219.29, -75.73, 232.00},
 {-73.40, -120.16, 204.00},
 {46.29, -77.98, 164.00},
 {0.00, 0.00, 255.00},
 {0.00, 0.00, 195.00},
 {91.33, 35.38, 185.00},
 {76.88, -25.52, 255.00},
 {165.63, 175.07, 255.00},
 {40.86, 47.69, 239.00},
 {52.43, 193.25, 230.00},
 {-78.07, -20.65, 234.00},
 {-62.70, -45.54, 190.00},
 {-10.91, -38.34, 231.00}};

my_xyz base_hsv_cylindric[20] = {
 {0.00, 0.00, 0.00},
 {0.00, 0.00, 127.00},
 {253.06, -31.38, 136.00},
 {224.00, -0.04, 237.00},
 {197.33, 87.85, 255.00},
 {140.86, 212.56, 255.00},
 {-150.50, 140.66, 177.00},
 {-241.03, -83.24, 232.00},
 {-91.75, -150.19, 204.00},
 {71.98, -121.24, 164.00},
 {0.00, 0.00, 255.00},
 {0.00, 0.00, 195.00},
 {125.88, 48.77, 185.00},
 {76.88, -25.52, 255.00},
 {165.63, 175.07, 255.00},
 {43.59, 50.88, 239.00},
 {58.13, 214.26, 230.00},
 {-85.07, -22.51, 234.00},
 {-84.14, -61.12, 190.00},
 {-12.05, -42.32, 231.00}
};

struct my_ycbcr {
    float Y; float Cb; float Cr;
};

my_ycbcr base_ycbcr[20] = {
 {0.00, 128.00, 128.00},
 {127.00, 128.00, 128.00},
 {43.00, 115.59, 112.31},
 {91.00, 96.96, 88.77},
 {68.00, 89.63, 79.50},
 {218.00, 4.98, -27.48},
 {122.00, 102.04, 95.19},
 {121.00, 190.64, 207.17},
 {84.00, 195.72, 213.58},
 {110.00, 158.47, 166.51},
 {255.00, 128.00, 128.00},
 {195.00, 128.00, 128.00},
 {136.00, 100.35, 93.05},
 {201.00, 128.00, 128.00},
 {195.00, 25.86, -1.09},
 {225.00, 100.35, 93.05},
 {192.00, 36.02, 11.75},
 {199.00, 147.75, 152.96},
 {140.00, 156.21, 163.66},
 {198.00, 146.62, 151.54}
};





#define SAT_TH 20
#define STD_TH 5

#define MININ3(x,y,z)  ( (y) <= (z) ? ((x) <= (y) ? (x) : (y)) : ((x) <= (z) ? (x) : (z)) )
#define MAXIN3(x,y,z)  ( (y) >= (z) ? ((x) >= (y) ? (x) : (y)) : ((x) >= (z) ? (x) : (z)) )


int findColsetColorIndexInHSV_conic(int R, int G, int B);
int findColsetColorIndexInHSV_cylindric(int R, int G, int B);
int findColsetColorIndexInYCbCr(int R, int G, int B);
int findColsetColorIndexInYCbCr2(int R, int G, int B);

int findColsetColorIndexInRGB(int R, int G, int B);
int findColsetColorIndexInGRAYScale(int R, int G, int B);
float uDist(float a1, float b1, float c1, float a2, float b2, float c2);
float uDist2(float a1, float b1, float a2, float b2);


hsv_color RGB2HSV(unsigned char r, unsigned char g, unsigned char b);
my_ycbcr rgb2ycbcr(unsigned char r, unsigned char g, unsigned char b);
my_xyz HSV2conic(unsigned char h, unsigned char s, unsigned char v);
my_xyz HSV2Cylindric(unsigned char h, unsigned char s, unsigned char v);
void matchingColorinTable(int inR, int inG, int inB);
float getSTDrgb(int R, int G, int B);

void main()
{

 

 srand( time(0) );
 printf( "%d \n", rand( ) );

 
 //draw represent 20 colors 
 int colorN = 20;
 Mat ColorTable(50,50*colorN, CV_8UC3);
 for(int i=0; i< colorN; ++i)
 {
  rectangle(ColorTable, Rect(i*50, 0, i*50+50, 50), CV_RGB(base_rgb[i].r, base_rgb[i].g, base_rgb[i].b),-1 );
 }
 imshow("ColorTable", ColorTable );
 imwrite("colortable.jpg", ColorTable);

 

 for(int i=0; i< 200; ++i)
 {
  int r = rand() %255;
  int g = rand() %255;
  int b = rand() %255;
  printf("%d %d %d \n", r,g,b);

  //matching
  matchingColorinTable(r, g, b);

  waitKey(0);
 }
 

 

 
}


void matchingColorinTable(int inR, int inG, int inB)
{
 

 //matching
 //step 1. get rgb standard deviation
 hsv_color hsv;
 hsv = RGB2HSV(inR, inG, inB);

 float std = getSTDrgb(inR, inG, inB);
 //printf("std = %lf \n", std );

 int findClosetColorIndex=-1;
 if(STD_TH > std)
 //if(SAT_TH > hsv.s )
 {
  //input rgb can be gray scale color.
  findClosetColorIndex = findColsetColorIndexInGRAYScale(inR, inG, inB);

  Mat inColor(50,50*2, CV_8UC3);
  rectangle(inColor, Rect(0, 0, 50, 50), CV_RGB(inR, inG, inB),-1 );
  rectangle(inColor, Rect(50, 0, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  imshow("your color and matched color", inColor );
  char str[100];
  sprintf(str,"gray_%d_%d_%d.jpg", inR, inG, inB );
  imwrite( str, inColor);
  

 }else{
  //step 2. find closest color by uclidiean distant
  Mat inColor(50*5,50*2, CV_8UC3);
  //#1 hsv conic
  rectangle(inColor, Rect(0, 0, 50, 50), CV_RGB(inR, inG, inB),-1 );
  findClosetColorIndex = findColsetColorIndexInHSV_conic(inR, inG, inB);
  rectangle(inColor, Rect(50, 0, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  rectangle(inColor, Rect(0, 0, 100, 50), CV_RGB(0,0,0),1 );

  //#2 hsv cylidric
  rectangle(inColor, Rect(0, 50*1, 50, 50), CV_RGB(inR, inG, inB),-1 );
  findClosetColorIndex = findColsetColorIndexInHSV_cylindric(inR, inG, inB);
  rectangle(inColor, Rect(50, 50*1, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  rectangle(inColor, Rect(0, 50, 100, 50), CV_RGB(0,0,0),1 );

  //#3 ycbcr
  rectangle(inColor, Rect(0, 50*2, 50, 50), CV_RGB(inR, inG, inB),-1 );
  findClosetColorIndex = findColsetColorIndexInYCbCr(inR, inG, inB);
  rectangle(inColor, Rect(50, 50*2, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  rectangle(inColor, Rect(0, 100, 100, 50), CV_RGB(0,0,0),1 );


  //#4 cbcr
  rectangle(inColor, Rect(0, 50*3, 50, 50), CV_RGB(inR, inG, inB),-1 );
  findClosetColorIndex = findColsetColorIndexInYCbCr2(inR, inG, inB);
  rectangle(inColor, Rect(50, 50*3, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  rectangle(inColor, Rect(0, 150, 100, 50), CV_RGB(0,0,0),1 );


  //#5 rgb
  rectangle(inColor, Rect(0, 50*4, 50, 50), CV_RGB(inR, inG, inB),-1 );
  findClosetColorIndex = findColsetColorIndexInRGB(inR, inG, inB);
  rectangle(inColor, Rect(50, 50*4, 50, 50), CV_RGB(base_rgb[findClosetColorIndex].r, base_rgb[findClosetColorIndex].g, base_rgb[findClosetColorIndex].b),-1 );
  rectangle(inColor, Rect(0, 200, 100, 50), CV_RGB(0,0,0),1 );

  
  imshow("your color and matched color", inColor );
  char str[100];
  sprintf(str,"%d_%d_%d.jpg", inR, inG, inB );
  imwrite( str, inColor);
  

 }
 

 //draw input color and matched color
 printf("find color index = %d\n", findClosetColorIndex);
 //print input hsv, match hsv 
 printf("input hsv=%d, %d, %d => match %d, %d, %d \n", hsv.h, hsv.s, hsv.v,
  base_hsv[findClosetColorIndex].h, 
  base_hsv[findClosetColorIndex].s, 
  base_hsv[findClosetColorIndex].v );



}



float uDist(float a1, float b1, float c1, float a2, float b2, float c2)
{
 float dist = sqrt( (a1-a2)*(a1-a2)+(b1-b2)*(b1-b2)+(c1-c2)*(c1-c2) );

 return dist;
}


float uDist2(float a1, float b1, float a2, float b2)
{
 float dist = sqrt( (a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
 return dist;
}

int findColsetColorIndexInGRAYScale(int R, int G, int B)
{
 
 int index=-1;
 float minDist=1000000;
 for(int i=0; i< 20; ++i)
 {
  //non gray scale pass
  if( !( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;

  float dist = uDist(R, G, B, base_rgb[i].r, base_rgb[i].g, base_rgb[i].b);
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }


 return index;
}

int findColsetColorIndexInHSV_conic(int R, int G, int B)
{
 int index=-1;
 float minDist=1000000;
 for(int i=0; i< 20; ++i)
 {
  //gray scale pass
  if( ( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;
  //rgb to hsv
  hsv_color hsv;
  hsv = RGB2HSV(R, G, B);
  //hsv to conic
  my_xyz xyz;
  xyz = HSV2conic(hsv.h, hsv.s, hsv.v);
  //distance
  float dist = uDist(xyz.x, xyz.y, xyz.z, base_hsv_conic[i].x, base_hsv_conic[i].y, base_hsv_conic[i].z);
  //min dist
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }
 return index;
}

int findColsetColorIndexInHSV_cylindric(int R, int G, int B)
{
 int index=-1;
 float minDist=1000000;
 for(int i=0; i< 20; ++i)
 {
  //gray scale pass
  if( ( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;
  //rgb to hsv
  hsv_color hsv;
  hsv = RGB2HSV(R, G, B);
  //hsv to conic
  my_xyz xyz;
  xyz = HSV2Cylindric(hsv.h, hsv.s, hsv.v);
  //distance
  float dist = uDist(xyz.x, xyz.y, xyz.z, base_hsv_cylindric[i].x, base_hsv_cylindric[i].y, base_hsv_cylindric[i].z);
  //min dist
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }
 return index;
}

int findColsetColorIndexInYCbCr(int R, int G, int B)
{
 int index=-1;
 float minDist=1000000;
 
 for(int i=0; i< 20; ++i)
 {
  //gray scale pass
  if( ( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;
  //rgb to ycbcr
  my_ycbcr ycbcr = rgb2ycbcr(R, G, B);
  //distance
  float dist = uDist(ycbcr.Y, ycbcr.Cb, ycbcr.Cr, base_ycbcr[i].Y, base_ycbcr[i].Cb, base_ycbcr[i].Cr);
  //min dist
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }
 
 return index;
}


int findColsetColorIndexInYCbCr2(int R, int G, int B)
{
 int index=-1;
 float minDist=1000000;
 
 for(int i=0; i< 20; ++i)
 {
  //gray scale pass
  if( ( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;
  //rgb to ycbcr
  my_ycbcr ycbcr = rgb2ycbcr(R, G, B);
  //distance
  float dist = uDist2(ycbcr.Cb, ycbcr.Cr,  base_ycbcr[i].Cb, base_ycbcr[i].Cr);
  //min dist
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }
 
 return index;
}



int findColsetColorIndexInRGB(int R, int G, int B)
{
 int index=-1;
 float minDist=1000000;
 
 for(int i=0; i< 20; ++i)
 {
  //gray scale pass
  if( ( (base_rgb[i].r == base_rgb[i].b) && (base_rgb[i].b == base_rgb[i].g ) ) )
   continue;
  //rgb to ycbcr
  
  //distance
  float dist = uDist(R, G, B, base_rgb[i].r, base_rgb[i].g, base_rgb[i].b);

  //min dist
  if(dist < minDist)
  {
   minDist = dist;
   index = i;
  }
 }

 return index;
}







hsv_color RGB2HSV(unsigned char r, unsigned char g, unsigned char b)
{
 unsigned char rgb_min, rgb_max;
 rgb_min = MININ3(b, g, r);
 rgb_max = MAXIN3(b, g, r);

 hsv_color hsv;
 hsv.v = rgb_max;
 if (hsv.v == 0) {
  hsv.h = hsv.s = 0;
  return hsv;
 }

 hsv.s = 255*(rgb_max - rgb_min)/hsv.v;
 if (hsv.s == 0) {
  hsv.h = 0;
  return hsv;
 }

 if (rgb_max == r) {
  hsv.h = 0 + 43*(g - b)/(rgb_max - rgb_min);
 } else if (rgb_max == g) {
  hsv.h = 85 + 43*(b - r)/(rgb_max - rgb_min);
 } else /* rgb_max == rgb.b */ {
  hsv.h = 171 + 43*(r - g)/(rgb_max - rgb_min);
 }

 return hsv;
}

my_xyz HSV2Cylindric(unsigned char h, unsigned char s, unsigned char v)
{
 my_xyz xyz;

 xyz.x = s*cos(2*3.1415*h/255.);
 xyz.y = s*sin(2*3.1415*h/255.);
 xyz.z = v;

 return xyz;

}


my_xyz HSV2conic(unsigned char h, unsigned char s, unsigned char v)
{
 my_xyz xyz;

 xyz.x = s*cos(2*3.1415*h/255.)*v/255.;
 xyz.y = s*sin(2*3.1415*h/255.)*v/255.;
 xyz.z = v;

 return xyz;
}


my_ycbcr rgb2ycbcr(unsigned char r, unsigned char g, unsigned char b)
{
 my_ycbcr ycbcr;

 ycbcr.Y = (299*r + 587*g + 114*b)/1000; //y
 ycbcr.Cb = 0.5643*(b - ycbcr.Y) + 128; //cb
 ycbcr.Cr = 0.7132*(b - ycbcr.Y) + 128; //cr
 


 return ycbcr;
}

float getSTDrgb(int R, int G, int B)
{
 float std;
 float mean = (R+G+B)/3;
 std = sqrt( (R-mean)*(R-mean)+(G-mean)*(G-mean)+(B-mean)*(B-mean) ) / 3;
 return std;
}


...

1/06/2015

nvcc : fatal error : Could not set up the environment for Microsoft Visual Studio using 'C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/Bin/../../VC/Bin/vcvars32.bat

I met the error message when I build opencv+cuda.

In past, I am solved by this method -> http://feelmare.blogspot.kr/2014/08/nvcc-fatal-error-could-not-set-up.html

But this is different method.

In conclusion, the error is because PATH problem in the environment variable.

vcvars32.bat do not work properly because using of wrong PATH value.

So nvcc environment error is occurred.




Try checking the value of the PATH in the user and system variables

Normal format is path;path;path.... .
(In my case, " caused the error C:\Python27\Lib";C:\Program Files (x86)\Google\google_appengine\ )

If the value of PATH in system variables did not included "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE" and 
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin", add these path.

and in cmd window, run vcvars32.bat. If there is no response text, it is normal.

and check one more, run cl.exe in cmd window.

this is wrong result. 


good luck.