Beispiel #1
0
/*----------------------------------------------------------------*/
void HSV2RGB (float _h, float _s, float _v, int& _r, int& _g, int& _b)
{
    float r, g, b;
    HSV2RGB (_h, _s, _v, r, g, b);

    _r = (int)(r*255.0);
    _g = (int)(g*255.0);
    _b = (int)(b*255.0);
}
Beispiel #2
0
RGBColor GetRGBColor(double val)
{
	RGBColor rgb;
	HSVColor hsv;
	hsv.hue = (SmallFract)Fix2SmallFract(FixRatio(32768*val*.8,32768));
	//hsv.hue = (SmallFract)Fix2SmallFract(FixRatio(32767*val*.8,32767));	// tweak to change color scheme
	hsv.saturation = (SmallFract)0xFFFF;
	hsv.value = (SmallFract)0xFFFF;
	HSV2RGB(&hsv,&rgb);
	return rgb;
}
Beispiel #3
0
static int DrawSelSpace (HDC hdc, int x, int y, int w, int h, PSCOLORDIA scld)
{
    Uint8 r, g, b;
    HDC mdc;
    
    mdc = CreateCompatibleDCEx (hdc, w, h);
    HSV2RGB (scld->clrh, scld->clrs, scld->clrv, &r, &g, &b);
    SetBrushColor (mdc, RGB2Pixel(mdc, r, g, b));
    FillBox (mdc, 0, 0, w, h);
    BitBlt (mdc, 0, 0, w, h, hdc, x, y, 0);
    DeleteMemDC (mdc);
    return 0;
}
Beispiel #4
0
void FMConvertHSBToRGB(CGFloat h, CGFloat s, CGFloat b, CGFloat rgb[3])
{
	// yeah, so this is the only function Mac OS X provides in pure C for HSL/RGB conversion, argh.
	HSVColor hsvColor;
	
	hsvColor.hue = floor(h*(CGFloat)kMaximumSmallFract);
	hsvColor.saturation = floor(s*(CGFloat)kMaximumSmallFract);
	hsvColor.value = floor(b*(CGFloat)kMaximumSmallFract);
	
	RGBColor rgbColor;
	HSV2RGB(&hsvColor, &rgbColor);
	
	rgb[0] = (CGFloat)rgbColor.red/(CGFloat)kMaximumSmallFract;
	rgb[1] = (CGFloat)rgbColor.green/(CGFloat)kMaximumSmallFract;
	rgb[2] = (CGFloat)rgbColor.blue/(CGFloat)kMaximumSmallFract;
}
Beispiel #5
0
static void UpdateValue (HWND hDlg, int id, PSCOLORDIA scld)
{
    char str[8];
    HDC dc = GetClientDC (hDlg);
    Uint8 r, g, b;
    
    HSV2RGB (scld->clrh, scld->clrs, scld->clrv, &r, &g, &b);
    switch (id) {
    case IDC_VALUE_Y:  
        GetDlgItemText (hDlg, id, str, 8);
        scld->clrh = atoi (str);
        scld->clrh = MIN (359, scld->clrh);
        SetValue (hDlg, scld);
        break;
    case IDC_VALUE_U:  
        GetDlgItemText (hDlg, id, str, 8);
        scld->clrs = atoi (str);
        SetValue (hDlg, scld);
        break;
    case IDC_VALUE_V:  
        GetDlgItemText (hDlg, id, str, 8);
        scld->clrv = atoi (str);
        SetValue (hDlg, scld);
        break;
    case IDC_VALUE_R:  
        GetDlgItemText (hDlg, id, str, 8);
        r = atoi (str);
        RGB2HSV (r, g, b, &scld->clrh, &scld->clrs, &scld->clrv);
        SetValue (hDlg, scld);
        break;
    case IDC_VALUE_G:  
        GetDlgItemText (hDlg, id, str, 8);
        g = atoi (str);
        RGB2HSV (r, g, b, &scld->clrh, &scld->clrs, &scld->clrv);
        SetValue (hDlg, scld);
        break;
    case IDC_VALUE_B:  
        GetDlgItemText (hDlg, id, str, 8);
        b = atoi (str);
        RGB2HSV (r, g, b, &scld->clrh, &scld->clrs, &scld->clrv);
        SetValue (hDlg, scld);
        break;
    }
    DrawAllSpace (dc, scld);
    ReleaseDC (dc);
}
Beispiel #6
0
static void SetValue (HWND hDlg, PSCOLORDIA scld)
{
    char str[8];
    Uint8 r, g, b;

    sprintf (str, "%d", scld->clrh);
    SetDlgItemText (hDlg, IDC_VALUE_Y, str);
    sprintf (str, "%d", scld->clrs);
    SetDlgItemText (hDlg, IDC_VALUE_U, str);
    sprintf (str, "%d", scld->clrv);
    SetDlgItemText (hDlg, IDC_VALUE_V, str);

    HSV2RGB (scld->clrh, scld->clrs, scld->clrv, &r, &g, &b);
    sprintf (str, "%d", r);
    SetDlgItemText (hDlg, IDC_VALUE_R, str);
    sprintf (str, "%d", g);
    SetDlgItemText (hDlg, IDC_VALUE_G, str);
    sprintf (str, "%d", b);
    SetDlgItemText (hDlg, IDC_VALUE_B, str);
}
Beispiel #7
0
CImage *CImageHSVtoRGB(CImage *cimg)
{
  CImage *ncimg=NULL;
  int p,n,i;


  ncimg = CreateCImage(cimg->C[0]->ncols,cimg->C[0]->nrows);
  n    = ncimg->C[0]->ncols*ncimg->C[0]->nrows;

  for (p=0; p < n; p++){

    i = triplet(cimg->C[0]->val[p],cimg->C[1]->val[p],cimg->C[2]->val[p]);
    i = HSV2RGB(i);
    ncimg->C[0]->val[p]=t0(i);
    ncimg->C[1]->val[p]=t1(i);
    ncimg->C[2]->val[p]=t2(i);
  }

  return(ncimg);
}
Beispiel #8
0
void _drawNode(carve::geom::RTreeNode<3, data_t> *rtree_node, size_t depth, size_t depth_max) {
  for (carve::geom::RTreeNode<3, data_t> *child = rtree_node->child; child != NULL; child = child->sibling) {
    _drawNode(child, depth+1, depth_max);
  }

  float r = depth / float(depth_max);
  float H = .7 - r * .2;
  float S = .4 + r * .6;
  float V = .2 + r * .8;
  cRGB col;

  col = HSV2RGB(H, S, V/2);
  glColor4f(col.r, col.g, col.b, .1 + r * .5);
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  drawCube(rtree_node->bbox.min(), rtree_node->bbox.max());

  // col = HSV2RGB(H, S, V);
  // glColor4f(col.r, col.g, col.b, .1 + r * .25);
  // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  // drawCube(rtree_node->aabb.min(), rtree_node->aabb.max());
}
Beispiel #9
0
RGBColor GetRGBColor(double val)
{

	return HSV2RGB(val*270,1.0,1.0);
}
int main(void) {
  gdouble h,s,v,r,g,b;
  uint16_t H,S,V,R,G,B;
  TColor RGB;
  TColor HSV;
  int Hi,Si,Vi,Ri,Gi,Bi;
  int T;
  int Errors = 0;
  int Error;
  int TotalError = 0;

#ifdef TEST_HSV2RGB
  // Test HSV2RGB
  for (Vi = 0; Vi <= 100; Vi++) {
    v = Vi*1.0/100.0;
    for (Si = 0; Si <= 100; Si++) {
      s = Si*1.0/100.0;
      for (Hi = 0; Hi < 360; Hi++) {
        h = Hi*1.0/360.0;
        gtk_hsv_to_rgb(h,s,v,&r,&g,&b);
        H = round(h*65535.0);
        S = round(s*65535.0);
        V = round(v*65535.0);
        R = round(r*65535.0);
        G = round(g*65535.0);
        B = round(b*65535.0);
        HSV.HSV.H = H;
        HSV.HSV.S = S;
        HSV.HSV.V = V;
        HSV2RGB(&HSV,&RGB);
        Error = abs((int)RGB.RGB.R-(int)R) + abs((int)RGB.RGB.G-(int)G) + abs((int)RGB.RGB.B-(int)B);
        if (Error > MAXDIFF) {
          printf("HSV2RGB %3d %3d %3d: %5d %5d %5d -> %5d %5d %5d",
              Hi,Si,Vi,
              H,S,V,
              RGB.RGB.R,RGB.RGB.G,RGB.RGB.B);
          printf(" (should be %5d %5d %5d, Error = %d)",R,G,B,Error);
          printf("\n");
          TotalError += Error;
          Errors++;
        }
      }
    }
  }
#endif // TEST_HSV2RGB

#ifdef TEST_RGB2HSV
  // Test RGB2HSV
  for (Bi = 0; Bi <= 100; Bi++) {
    b = Bi*1.0/100.0;
    for (Gi = 0; Gi <= 100; Gi++) {
      g = Gi*1.0/100.0;
      for (Ri = 0; Ri <= 100; Ri++) {
        r = Ri*1.0/100.0;
        gtk_rgb_to_hsv(r,g,b,&h,&s,&v);
        R = round(r*65535.0);
        G = round(g*65535.0);
        B = round(b*65535.0);
        H = round(h*65535.0);
        S = round(s*65535.0);
        V = round(v*65535.0);
        RGB.RGB.R = R;
        RGB.RGB.G = G;
        RGB.RGB.B = B;
        RGB2HSV(&RGB,&HSV);
        Error = abs((int)HSV.HSV.H-(int)H) + abs((int)HSV.HSV.S-(int)S) + abs((int)HSV.HSV.V-(int)V);
        if (Error > MAXDIFF) {
          printf("RGB2HSV %3d %3d %3d: %5d %5d %5d -> %5d %5d %5d",
              Ri,Gi,Bi,
              R,G,B,
              HSV.HSV.H,HSV.HSV.S,HSV.HSV.V);
          printf(" (should be %5d %5d %5d, Error = %d)",H,S,V,Error);
          printf("\n");
          TotalError += Error;
          Errors++;
        }
      }
    }
  }
#endif // TEST_RGB2HSV

#ifdef TEST_WHITE2RGB
  printf("<pre>\n");
  for (T = 1000; T <= 40000; T+=100) {
    White2RGB(T,&RGB);
    printf("<span style=\"background:#%02x%02x%02x\"> %5d K -> %5d %5d %5d - #%02x%02x%02x</span>\n",
        RGB.RGB.R >> 8,RGB.RGB.G >> 8,RGB.RGB.B >> 8,
        T,
        RGB.RGB.R,RGB.RGB.G,RGB.RGB.B,
        RGB.RGB.R >> 8,RGB.RGB.G >> 8,RGB.RGB.B >> 8);
  }
  printf("</pre>\n");
#endif

  printf("%d errors found, total deviation is %d.\n",Errors,TotalError);
  return (Errors == 0 ? 0 : 1);
}
Beispiel #11
0
static int ColorDlgProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
    PSCOLORDIA scld;

    switch (message) {
    case MSG_INITDIALOG:
        {
            int i, j;
            Uint8 r, g, b;
            HDC hdc, mdc;
            
            scld = (PSCOLORDIA)lParam;
            SetWindowAdditionalData (hDlg, (LPARAM)scld);

            hdc = GetClientDC (hDlg);
            scld->SpaceDC = CreateCompatibleDCEx(hdc, scld->sp_w, scld->sp_h);
            mdc = CreateCompatibleDCEx (hdc, 360, 256);
            for (i =  0; i < 360; i ++) {
                for (j = 0; j < 256; j ++) {
                    HSV2RGB (i, j, 200, &r, &g, &b);
                    SetPixelRGB(mdc, i, 256-j, r, g, b);
                }
            }
            
            StretchBlt(mdc, 0, 0, 360, 256, scld->SpaceDC, 0, 0, scld->sp_w, scld->sp_h, 0);
            DeleteMemDC(mdc);
            ReleaseDC(hdc);
        }        
        break;

    case MSG_CLOSE:
        EndDialog (hDlg, SELCOLOR_CANCEL);
        break;

    case MSG_DESTROY:
        scld = (PSCOLORDIA)GetWindowAdditionalData (hDlg);
        DeleteMemDC (scld->SpaceDC);
        break;

    case MSG_PAINT:
    {
        HDC hdc;

        scld = (PSCOLORDIA)GetWindowAdditionalData (hDlg);
        hdc = BeginPaint (hDlg);
        DrawAllSpace (hdc, scld);
        EndPaint (hDlg, hdc);
        return 0;
    }

    case MSG_COMMAND:
    {
        int msg = HIWORD(wParam);
        int id = LOWORD(wParam);

        scld = (PSCOLORDIA)GetWindowAdditionalData (hDlg);

        if (msg == EN_CONTCHANGED) {
            UpdateValue (hDlg, id, scld);
        }

        switch(id) {
        case IDC_CANCEL:
            EndDialog (hDlg, SELCOLOR_CANCEL);
            break;
        case IDC_OK:
            scld->H = scld->clrh;
            scld->S = scld->clrs;
            scld->V = scld->clrv;
            HSV2RGB (scld->clrh, scld->clrs, scld->clrv, &scld->R, &scld->G, &scld->B);
            scld->PIXEL = RGB2Pixel (HDC_SCREEN, scld->R, scld->G, scld->B);
            EndDialog (hDlg, SELCOLOR_OK);
            break;
        }
        break;
    }

    case MSG_LBUTTONDOWN:
    {
        int x = LOSWORD (lParam);
        int y = HISWORD (lParam);

        scld = (PSCOLORDIA)GetWindowAdditionalData (hDlg);

        if (PtInRect (&scld->rcSpace, x, y)) {
            HDC dc = GetClientDC (hDlg);
            scld->clrh = (x-scld->rcSpace.left)*360/RECTW(scld->rcSpace);
            scld->clrs = 256-(y-scld->rcSpace.top)*256/RECTH(scld->rcSpace);
            DrawAllSpace (dc, scld);
            SetValue (hDlg, scld);
            ReleaseDC (dc);
        }

        if (PtInRect(&scld->rcYSpace, x,y)) {
            HDC dc = GetClientDC (hDlg);
            scld->clrv = (y-scld->rcYSpace.top)*256 / RECTH(scld->rcYSpace);
            DrawAllSpace (dc, scld);
            SetValue (hDlg, scld);
            ReleaseDC (dc);
        }
    }
    }
    return DefaultDialogProc (hDlg, message, wParam, lParam);
}
pColorPacket CAtmoColorCalculator::AnalyzeHSV(tHSVColor *HSV_Img)
{
  int i; // counter

  int AtmoSetup_EdgeWeighting  = m_pAtmoConfig->getLiveView_EdgeWeighting();
  int AtmoSetup_WidescreenMode = m_pAtmoConfig->getLiveView_WidescreenMode();
  int AtmoSetup_DarknessLimit  = m_pAtmoConfig->getLiveView_DarknessLimit();
  int AtmoSetup_BrightCorrect  = m_pAtmoConfig->getLiveView_BrightCorrect();
  int AtmoSetup_SatWinSize     = m_pAtmoConfig->getLiveView_SatWinSize();
  int AtmoSetup_NumZones       = m_pAtmoConfig->getZoneCount();
  tHSVColor *temp_Img;

  if(AtmoSetup_NumZones != m_LastNumZones)
  {
     delete[] m_Weight;
     delete[] m_hue_hist;
     delete[] m_windowed_hue_hist;
     delete[] m_most_used_hue_last;
     delete[] m_most_used_hue;
     delete[] m_sat_hist;
     delete[] m_windowed_sat_hist;
     delete[] m_most_used_sat;
     delete[] m_Zone_Weights;
     delete[] m_average_v;
     delete[] m_average_counter;

     m_Weight              = new int[AtmoSetup_NumZones * IMAGE_SIZE];
     m_Zone_Weights        = new int*[AtmoSetup_NumZones];
     for(int i = 0; i < AtmoSetup_NumZones; i++)
         m_Zone_Weights[i] = &m_Weight[i * IMAGE_SIZE];

     m_hue_hist            = new long int[(h_MAX+1) * AtmoSetup_NumZones];
     m_windowed_hue_hist   = new long int[(h_MAX+1) * AtmoSetup_NumZones];

     m_most_used_hue_last  = new int[AtmoSetup_NumZones];
     m_most_used_hue       = new int[AtmoSetup_NumZones];
     memset( m_most_used_hue_last, 0, sizeof(int) * AtmoSetup_NumZones);

     m_sat_hist           = new long int[(s_MAX+1) * AtmoSetup_NumZones];
     m_windowed_sat_hist  = new long int[(s_MAX+1) * AtmoSetup_NumZones];
     m_most_used_sat      = new int[AtmoSetup_NumZones];

     m_average_v         = new long int[AtmoSetup_NumZones];
     m_average_counter   = new int[AtmoSetup_NumZones];

     m_LastNumZones = AtmoSetup_NumZones;
  }


  // calculate only if setup has changed
  if ((AtmoSetup_EdgeWeighting != m_LastEdgeWeighting) ||
      (AtmoSetup_WidescreenMode != m_LastWidescreenMode) ||
      (m_pAtmoConfig->getZonesTopCount() != m_LastLayout_TopCount) ||
      (m_pAtmoConfig->getZonesBottomCount() != m_LastLayout_BottomCount) ||
      (m_pAtmoConfig->getZonesLRCount() !=  m_LastLayout_LRCount) ||
      (m_pAtmoConfig->m_UpdateEdgeWeightningFlag != 0)
     )
  {

      for(i = 0 ;i < AtmoSetup_NumZones; i++) {
          CAtmoZoneDefinition *pZoneDef = m_pAtmoConfig->getZoneDefinition(i);
          if(pZoneDef)
          {
             pZoneDef->UpdateWeighting(m_Zone_Weights[i],
                                                              AtmoSetup_WidescreenMode,
                                                              AtmoSetup_EdgeWeighting);
#ifdef _debug_zone_weight_
             char filename[128];
             sprintf(filename, "zone_%d_gradient_debug.bmp",i);
             pZoneDef->SaveZoneBitmap( filename );
             sprintf(filename, "zone_%d_weight_%d_debug.bmp",i,AtmoSetup_EdgeWeighting);
             pZoneDef->SaveWeightBitmap(filename, m_Zone_Weights[i] );
#endif
          }

     }
     m_pAtmoConfig->m_UpdateEdgeWeightningFlag = 0;

     m_LastEdgeWeighting      = AtmoSetup_EdgeWeighting;
     m_LastWidescreenMode     = AtmoSetup_WidescreenMode;
     m_LastLayout_TopCount    = m_pAtmoConfig->getZonesTopCount();
     m_LastLayout_BottomCount = m_pAtmoConfig->getZonesBottomCount();
     m_LastLayout_LRCount     = m_pAtmoConfig->getZonesLRCount();
  }

  AtmoSetup_DarknessLimit = AtmoSetup_DarknessLimit * 10;


  /***************************************************************************/
  /* Hue                                                                     */
  /***************************************************************************/
  /*----------------------------*/
  /* hue histogram builtup      */
  /*----------------------------*/
  // HSV histogram
  // long int hue_hist[CAP_MAX_NUM_ZONES][h_MAX+1];

  // average brightness (value)
  // m_average_v m_average_counter

  // clean histogram --> calloc
  memset(m_hue_hist, 0, sizeof(long int) * (h_MAX+1) * AtmoSetup_NumZones);
  memset(m_average_v, 0, sizeof(long int) * AtmoSetup_NumZones);
  memset(m_average_counter, 0, sizeof(int) * AtmoSetup_NumZones);

  temp_Img = HSV_Img;
  i = 0;
  for (int row = 0; row < CAP_HEIGHT; row++)
  {
    for (int column = 0; column < CAP_WIDTH; column++)
    {
      // forget black bars: perform calculations only if pixel has some luminosity
	  if ((*temp_Img).v > AtmoSetup_DarknessLimit)
      {
        // builtup histogram for the x Zones of the Display
        for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
        {
          // Add weight to channel
         // Weight(zone, pixel_nummer) m_Weight[((zone) * (IMAGE_SIZE)) + (pixel_nummer)]
         // m_hue_hist[zone*(h_MAX+1) + HSV_Img[i].h] += m_Zone_Weights[zone][i] * HSV_Img[i].v;
            m_hue_hist[zone*(h_MAX+1) + (*temp_Img).h] += m_Zone_Weights[zone][i] * temp_Img->v;

            if(m_Zone_Weights[zone][i] > 0) {
               m_average_v[zone] += temp_Img->v;
               m_average_counter[zone]++;
            }

        }
        // calculate brightness average
      }
      temp_Img++;
      i++;
    }
  }

  /*----------------------------*/
  /* hue histogram windowing    */
  /*----------------------------*/
  // windowed HSV histogram
  // long int w_hue_hist[CAP_MAX_NUM_ZONES][h_MAX+1]; -> m_windowed_hue_hist
  // clean windowed histogram
  memset(m_windowed_hue_hist, 0, sizeof(long int) * (h_MAX+1) * AtmoSetup_NumZones);
  // steps in each direction; eg. 2 => -2 -1 0 1 2 windowing
  int hue_windowsize = m_pAtmoConfig->getLiveView_HueWinSize();

  for (i = 0; i < h_MAX+1; i++) // walk through histogram [0;h_MAX]
  {
    // windowing from -hue_windowsize -> +hue_windowsize
    for (int mywin = -hue_windowsize; mywin < hue_windowsize+1; mywin++)
    {
      // addressed histogram candlestick
      int myidx = i + mywin;

      // handle beginning of windowing -> roll back
      if (myidx < 0)     { myidx = myidx + h_MAX + 1; }

      // handle end of windowing -> roll forward
      if (myidx > h_MAX) { myidx = myidx - h_MAX - 1; }

      // Apply windowing to all x zones
      for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
      {
        // apply lite triangular window design with gradient of 10% per discrete step
        m_windowed_hue_hist[(zone * (h_MAX+1)) + i] += m_hue_hist[(zone * (h_MAX+1)) + myidx] * ((hue_windowsize+1)-abs(mywin)); // apply window
      }
    }
  }

  /*--------------------------------------*/
  /* analyze histogram for most used hue  */
  /*--------------------------------------*/
  // index of last maximum
  // static int most_used_hue_last[CAP_MAX_NUM_ZONES] = {0, 0, 0, 0, 0}; --> m_most_used_hue_last

  // resulting hue for each channel
  //int most_used_hue[CAP_MAX_NUM_ZONES]; --> m_most_used_hue

  FindMostUsed(AtmoSetup_NumZones, m_most_used_hue, m_windowed_hue_hist);
  for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
  {
    float percent = (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue_last[zone]] / (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue[zone]];
    if (percent > 0.93f) // less than 7% difference?
        m_most_used_hue[zone] = m_most_used_hue_last[zone]; // use last index
     else
        m_most_used_hue_last[zone] = m_most_used_hue[zone];
  }

  /*
  memset(m_most_used_hue, 0, sizeof(int) * AtmoSetup_NumZones);

  for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
  {
    long int value = 0;
    for (i = 0; i < h_MAX+1; i++) // walk through histogram
    {
      long int tmp = m_windowed_hue_hist[ (zone * (h_MAX+1)) + i ];
      if (tmp > value) // if new value bigger then old one
      {
        m_most_used_hue[zone] = i;     // remember index
        value = tmp; // w_hue_hist[zone][i]; // and value
      }
    }

    float percent = (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue_last[zone]] / (float)value;
    if (percent > 0.93f) // less than 7% difference?
    {
      m_most_used_hue[zone] = m_most_used_hue_last[zone]; // use last index
    }

    m_most_used_hue_last[zone] = m_most_used_hue[zone]; // save current index of most used hue
  }
  */

  /***************************************************************************/
  /* saturation                                                              */
  /***************************************************************************/
  // sat histogram
  // long int sat_hist[CAP_MAX_NUM_ZONES][s_MAX+1];  -> m_sat_hist
  // hue of the pixel we are working at
  int pixel_hue = 0;
  // clean histogram
  memset(m_sat_hist, 0, sizeof(long int) * (s_MAX+1) * AtmoSetup_NumZones);

  /*--------------------------------------*/
  /* saturation histogram builtup         */
  /*--------------------------------------*/
  i = 0;
  temp_Img = HSV_Img;
  for (int row = 0; row < CAP_HEIGHT; row++)
  {
    for (int column = 0; column < CAP_WIDTH; column++)
    {
      // forget black bars: perform calculations only if pixel has some luminosity
	  if ((*temp_Img).v > AtmoSetup_DarknessLimit)
      {
        // find histogram position for pixel
        pixel_hue = (*temp_Img).h;

        // TODO:   brightness calculation(if we require it some time)

        for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
        {
          // only use pixel for histogram if hue is near most_used_hue
          if ((pixel_hue > m_most_used_hue[zone] - hue_windowsize) &&
              (pixel_hue < m_most_used_hue[zone] + hue_windowsize))
          {
            // build histogram
            // sat_hist[channel][HSV_Img[i].s] += Weight[i].channel[channel] * HSV_Img[i].v;
            m_sat_hist[zone * (s_MAX+1) + (*temp_Img).s ] += m_Zone_Weights[zone][i] * (*temp_Img).v;

          }
        }
      }
      i++;
      temp_Img++;
    }
  }

  /*--------------------------------------*/
  /* saturation histogram windowing       */
  /*--------------------------------------*/
   // windowed HSV histogram
   // long int w_sat_hist[CAP_MAX_NUM_ZONES][s_MAX+1]; --> m_windowed_sat_hist
   // clean windowed histogram
   memset(m_windowed_sat_hist, 0, sizeof(long int) * (s_MAX+1) * AtmoSetup_NumZones);
   // steps in each direction; eg. 2 => -2 -1 0 1 2 windowing
   int sat_windowsize = AtmoSetup_SatWinSize;

   // walk through histogram [0;h_MAX]
   for (i = 0; i < s_MAX + 1; i++)
   {
     // windowing from -hue_windowsize -> +hue_windowsize
     for (int mywin = -sat_windowsize; mywin < sat_windowsize+1; mywin++)
     {
       // addressed histogram candlestick
       int myidx = i + mywin;

       // handle beginning of windowing -> roll back
       if (myidx < 0)     { myidx = myidx + s_MAX + 1; }

       // handle end of windowing -> roll forward
       if (myidx > h_MAX) { myidx = myidx - s_MAX - 1; }

       for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
       {
         /*
            apply lite triangular window design with
            gradient of 10% per discrete step
         */
         /*
         w_sat_hist[channel][i] += sat_hist[channel][myidx] *
                                  ((sat_windowsize+1)-abs(mywin)); // apply window
         */
         m_windowed_sat_hist[zone * (s_MAX+1) + i] += m_sat_hist[zone* (h_MAX+1) + myidx] *
                                  ((sat_windowsize+1)-abs(mywin)); // apply window
       }
     }
   }

  /*--------------------------------------*/
  /* analyze histogram for most used sat  */
  /*--------------------------------------*/
   // resulting sat (most_used_hue) for each channel
  // int most_used_sat[CAP_MAX_NUM_ZONES];->m_most_used_sat

  FindMostUsed(AtmoSetup_NumZones, m_most_used_sat, m_windowed_sat_hist);
  /*
  memset(m_most_used_sat, 0, sizeof(int) * AtmoSetup_NumZones);

  for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
  {
    int value = 0;
    // walk trough histogram
    for (i = 0; i < s_MAX+1; i++)
    {
      // if new value bigger then old one
      int tmp = m_windowed_sat_hist[zone * (s_MAX+1) + i];
      // if (w_sat_hist[channel][i] > value)
      if (tmp > value)
      {
        // remember index
        m_most_used_sat[zone] = i;
        // and value
        value = tmp;
      }
    }
  }
  */


  /*----------------------------------------------------------*/
  /* calculate average brightness within HSV image            */
  /* uniform Brightness for all channels is calculated        */
  /*----------------------------------------------------------*/
  /* code integrated into "hue histogram builtup" to save some looping time!
  int l_counter = 0;
  // average brightness (value)
  long int value_avg = 0;
  i = 0;
  for (int row = 0; row < CAP_HEIGHT; row++)
  {
    for (int column = 0; column < CAP_WIDTH; column++)
    {
      // find average value: only use bright pixels for luminance average
	  if (HSV_Img[i].v > AtmoSetup_DarknessLimit)
      {
        // build brightness average
        value_avg += HSV_Img[i].v;
        l_counter++;
      }
      i++;
    }
  }
  // calculate brightness average
  if (l_counter > 0) { value_avg = value_avg / l_counter; }
    else { value_avg = AtmoSetup_DarknessLimit; }

  */


  /*----------------------------*/
  /* adjust and copy results    */
  /*----------------------------*/
  tHSVColor hsv_pixel;
  // storage container for resulting RGB values
  pColorPacket output_colors;
  AllocColorPacket(output_colors, AtmoSetup_NumZones);

  // adjust brightness
//  int new_value = (int) ((float)value_avg * ((float)AtmoSetup_BrightCorrect / 100.0));
//  if (new_value > 255) new_value = 255;  // ensure brightness isn't set too high
//  hsv_pixel.v = (unsigned char)new_value;

  /*
  // calculate brightness average
  for(int zone = 0; zone < AtmoSetup_NumZones; zone++) {
      if(m_average_counter[zone] > 0)
          m_average_v[zone] = m_average_v[zone] / m_average_counter[zone]
      else
          m_average_v[zone] = AtmoSetup_DarknessLimit;
  }

  */

  for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
  {
    if(m_average_counter[zone] > 0)
       m_average_v[zone] = m_average_v[zone] / m_average_counter[zone];
    else
       m_average_v[zone] = AtmoSetup_DarknessLimit;

    m_average_v[zone] = (int)((float)m_average_v[zone] * ((float)AtmoSetup_BrightCorrect / 100.0));

    hsv_pixel.v = (unsigned char)ATMO_MAX(ATMO_MIN(m_average_v[zone],255),0);
    hsv_pixel.h = m_most_used_hue[zone];
    hsv_pixel.s = m_most_used_sat[zone];

    // convert back to rgb
    output_colors->zone[zone] = HSV2RGB(hsv_pixel);
  }


  return output_colors;
}
Beispiel #13
0
void TargetObject::updateRGB(){
  //std::cout << "before : hue = " << hue << ", red = " << red << ", green = " << green << ", blue = " << blue << std::endl;
  int hueVal = hue; //360. * hue / NOF_HUEBINS;
	HSV2RGB(hueVal, red, green, blue);
	//std::cout << " after : hue = " << hue << ", red = " << red << ", green = " << green << ", blue = " << blue << std::endl;
}
Beispiel #14
0
void TargetObject::resetJPDA(){

		predX = 0.99*predX + 0.01 * cvmGet(kalman->state_pre,0,0);
		predY = 0.99*predY + 0.01 * cvmGet(kalman->state_pre,1,0);
		
	
		double absVel = sqrt(vx*vx + vy*vy);
		speedVec.push_back(absVel);
		speedVecx.push_back(vx);
		speedVecy.push_back(vy);

		if (speedVec.size() > 20){
			meanSpeed = 0.0;
			for (int i = 0; i < speedVec.size(); i++){
				double curv = speedVec.at(i);
				meanSpeed = meanSpeed + curv;
				meanSpeedx = meanSpeedx + speedVecx.at(i);
				meanSpeedy = meanSpeedy + speedVecy.at(i);
			}
			meanSpeed = meanSpeed / speedVec.size();
			meanSpeedx = meanSpeedx / speedVecx.size();
			meanSpeedy = meanSpeedy / speedVecy.size();
			
			
			// remove the last one
			speedVec.erase(speedVec.begin());
			speedVecx.erase(speedVecx.begin());
			speedVecy.erase(speedVecy.begin());
		}


  //associated_to_any_data = false;

	/// reset the covariance matrix if the trace is too high
	///printMatrix(kalman->error_cov_post, "cov");
	CvSize size = cvGetSize( kalman->error_cov_post );
	int rows = size.height;
	int cols = size.width; 
	float totalSum = 0.0;
	if (rows == cols){ // check for square matrix 

		float *invdata = kalman->error_cov_post->data.fl;
		int invcovn = kalman->error_cov_post->cols;
		
		
		for (int i = 0; i < rows; i ++){
			for (int j =0; j < cols; j++){
			if (i==j){totalSum = totalSum + (invdata[i * invcovn + j] * invdata[i * invcovn + j]);}
			}
		} 
	}
	
	//std::cout << "trace norm: " << name << ": " << sqrt(totalSum) << std::endl; 
	if (sqrt(totalSum) > 1000.0){ // trace norm too big
		// rest the covariance to identity
		///cvSetIdentity( kalman->error_cov_post, cvRealScalar(1.0));

		/// meaurement noice covariance
		///cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1.0));
       
	}


	/// ---------- end resetting covariance

	//printMatrix(kalman->error_cov_post, "cov");
	//std:: cout << "in reset JPDA " << std:: endl;

	/// update the RGB value from the new hue value
	if (hueDataAvailable){
	  int hueVal = hue ; //360. * hue / NOF_HUEBINS;
		//std::cout << "setting rgb from hue: " << hue << ", " << hueVal << std::endl;
		NOF_HUEBINS;
		HSV2RGB(hueVal, red, green, blue);

		//std::cout << "set rgb to:  " << red << ", " << green << ", " << blue << std::endl;
		
	}
	hueDataAvailable = false;
	
	event_probs->clear();
	//std:: cout << "in reset JPDA 1 " << std:: endl;
	cvSet(combined_innov,cvScalarAll(0),0);
 	//std:: cout << "all_innovs size =  " << all_innovs->size() << std:: endl;
	// reset all innovs
	for (int i = 0; i < all_innovs->size(); i++){
		cvReleaseMat(&(all_innovs->at(i)));
	}
	//std:: cout << "in reset JPDA 3 " << std:: endl;
	all_innovs->clear();

	all_measurements->clear();
	//std:: cout << "exiting reset JPDA...all_innovs size = " << all_innovs->size() << std:: endl;
	//associated_to_cam_data = false;
}
Beispiel #15
0
int main(int argc, char* argv[])
{
	// Build color lookup table for depth display
	Mat colorLut = Mat(cv::Size(256, 1), CV_8UC3);
	for(int i = 0; i < 256; i++)
		colorLut.at<Vec3b>(i) = (i==0) ? Vec3b(0, 0, 0) : HSV2RGB(i/256.0f, 1, 1);

	// Open Dense3D
	Dense3DMTInstance dense3d;
	if(!Dense3DOpen(&dense3d))
	{
		printf("Could not open Dense3DMT library\n");
		return 1;
	}

	DUOInstance duo = GetDUOInstance(dense3d);

	char tmp[260];
	GetDUODeviceName(duo,tmp);
	printf("DUO Device Name:      '%s'\n", tmp);
	GetDUOSerialNumber(duo, tmp);
	printf("DUO Serial Number:    %s\n", tmp);
	GetDUOFirmwareVersion(duo, tmp);
	printf("DUO Firmware Version: v%s\n", tmp);
	GetDUOFirmwareBuild(duo, tmp);
	printf("DUO Firmware Build:   %s\n", tmp);
	printf("DUOLib Version:       v%s\n", GetDUOLibVersion());
	printf("Dense3DMT Version:    v%s\n", Dense3DGetLibVersion());

	// Set the Dense3D license (visit https://duo3d.com/account)
	if(!SetDense3DLicense(dense3d, "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX")) // <-- Put your Dense3D license
	{
		printf("Invalid or missing Dense3D license. To get your license visit https://duo3d.com/account\n");
		// Close Dense3D library
		Dense3DClose(dense3d);
		return 1;
	}
	// Set the image size
	if(!SetDense3DImageInfo(dense3d, WIDTH, HEIGHT, FPS))
	{
		printf("Invalid image size\n");
		// Close Dense3D library
		Dense3DClose(dense3d);
		return 1;
	}

	// Set Dense3D parameters
	Dense3DParams params;
	params.scale = 0;
    params.mode = 0;
    params.numDisparities = 2;
    params.sadWindowSize = 6;
    params.preFilterCap = 28;
    params.uniqenessRatio = 27;
    params.speckleWindowSize = 52;
    params.speckleRange = 14;
	if(!SetDense3Params(dense3d, params))
    {
        printf("GetDense3Params error\n");
		// Close Dense3D library
		Dense3DClose(dense3d);
        return 1;
    }

	// Queue used to receive Dense3D frames
	Dense3DFrameQueue d3dq;

	// Start DUO capture and Dense3D processing
	if(!Dense3DStart(dense3d, [](const PDense3DFrame pFrameData, void *pUserData)
					{
                        D3DFrame frame;
                        Size frameSize(pFrameData->duoFrame->width, pFrameData->duoFrame->height);
                        frame.leftImg = Mat(frameSize, CV_8U, pFrameData->duoFrame->leftData);
                        frame.rightImg = Mat(frameSize, CV_8U, pFrameData->duoFrame->rightData);
                        frame.disparity = Mat(frameSize, CV_32F, pFrameData->disparityData);
                        frame.depth = Mat(frameSize, CV_32FC3, pFrameData->depthData);
                        ((Dense3DFrameQueue*)pUserData)->push(frame);
					}, &d3dq))
	{
		printf("Dense3DStart error\n");
		// Close Dense3D library
		Dense3DClose(dense3d);
		return 1;
	}

	// Set exposure, LED brightness and camera orientation
	SetDUOExposure(duo, 85);
	SetDUOLedPWM(duo, 28);
	SetDUOVFlip(duo, true);

	// Run capture loop until <Esc> key is pressed
	while((cvWaitKey(1) & 0xff) != 27)
	{
		D3DFrame d3DFrame;
		if(!d3dq.pop(d3DFrame))
			continue;

		Mat disp8;
		d3DFrame.disparity.convertTo(disp8, CV_8UC1, 255.0/(params.numDisparities*16));
		Mat rgbBDisparity;
		cvtColor(disp8, rgbBDisparity, COLOR_GRAY2BGR);
		LUT(rgbBDisparity, colorLut, rgbBDisparity);

		// Display images
		imshow("Left Image", d3DFrame.leftImg);
		imshow("Right Image", d3DFrame.rightImg);
		imshow("Disparity Image", rgbBDisparity);
	}
	destroyAllWindows();

	Dense3DStop(dense3d);
	Dense3DClose(dense3d);
	return 0;
}
Beispiel #16
0
// draw title
void ShellTitle::Render(unsigned int aId, float aTime, const Transform2 &aTransform)
{
//#define USE_TITLE_VERTEX_ARRAY
#ifdef USE_TITLE_VERTEX_ARRAY
	static Vector2 vertexarray[32768];
	static unsigned int colorarray[32768];
	Vector2 *vertexptr = vertexarray;
	unsigned int *colorptr = colorarray;
#else
	glBegin(GL_QUADS);
#endif

	// draw title bar
	for (int row = 0; row < SDL_arraysize(titlemap); ++row)
	{
		float y0 = titley + row * titleh, y1 = y0 + titleh;

#ifdef USE_TITLE_VERTEX_ARRAY
		unsigned int color = (xs_RoundToInt(255*baralpha[row]) << 24) | 0x00505050;
		*colorptr++ = color;
		*colorptr++ = color;
		*colorptr++ = color;
		*colorptr++ = color;
		*vertexptr++ = Vector2(0, y0);
		*vertexptr++ = Vector2(640, y0);
		*vertexptr++ = Vector2(640, y1);
		*vertexptr++ = Vector2(0, y1);
#else
		glColor4f(0.3f, 0.3f, 0.3f, baralpha[row]);
		glVertex2f(0, y0);
		glVertex2f(640, y0);
		glVertex2f(640, y1);
		glVertex2f(0, y1);
#endif
	}

	// draw title body
	unsigned short *titlefillptr = titlefill;

#if 1
#ifdef USE_TITLE_MIRROR_WATER_EFFECT
	// starting mirror properties
	float mirror_y0 = MirrorWaveY(titley - titleh);
	float mirror_d0 = MirrorWaveX(mirror_y0);
	float mirror_a0 = mirroralphastart + mirroralphadelta * mirror_y0;
#endif

	for (int row = -1; row < (int)SDL_arraysize(titlemap) + 1; ++row)
	{
		float y = titley + row * titleh;

#ifdef USE_TITLE_MIRROR_WATER_EFFECT
		// row mirror properties
		float mirror_y1 = MirrorWaveY(y + titleh);
		float mirror_yd = (mirror_y1 - mirror_y0) / titleh;
		float mirror_d1 = MirrorWaveX(mirror_y1);
		float mirror_dd = (mirror_d1 - mirror_d0) / titleh;
		float mirror_a1 = mirroralphastart + mirroralphadelta * mirror_y1;
		float mirror_ad = (mirror_a1 - mirror_a0) / titleh;
#endif

		for (int col = -1; col < (int)SDL_arraysize(titlemap[0]); ++col)
		{
			float x = titlex + col * titlew;

			if (*titlefillptr != 0)
			{
				int phase = *titlefillptr >> 9;
				int fill = *titlefillptr & 0x1FF;

				// get block color
				float R, G, B;
				float h = BlockHue(col, row);
				bool border = (fill & ~(1<<4)) != 0;
				HSV2RGB(h + phase * 0.5f + border * 0.5f, 1.0f, 1.0f - 0.25f * border, R, G, B);

				// for each block...
				for (int i = 0; i < 9; ++i)
				{
					// if the block is filled
					if (fill & (1 << i))
					{
						// block borders
						float x0 = x + block[i][0][0];
						float x1 = x + block[i][0][1];
						float y0 = y + block[i][1][0];
						float y1 = y + block[i][1][1];

						// upright
#ifdef USE_TITLE_VERTEX_ARRAY
						unsigned int color = 0xFF000000 | (xs_RoundToInt(B * 255) << 16) | (xs_RoundToInt(G * 255) << 8) | (xs_RoundToInt(R * 255) );
						*colorptr++ = color;
						*colorptr++ = color;
						*colorptr++ = color;
						*colorptr++ = color;
						*vertexptr++ = Vector2(x0, y0);
						*vertexptr++ = Vector2(x1, y0);
						*vertexptr++ = Vector2(x1, y1);
						*vertexptr++ = Vector2(x0, y1);
#else
						glColor4f(R, G, B, 1.0f);
						glVertex2f(x0, y0);
						glVertex2f(x1, y0);
						glVertex2f(x1, y1);
						glVertex2f(x0, y1);
#endif

#ifdef USE_TITLE_MIRROR_WATER_EFFECT
						if (mirror_a0 > 0.0f || mirror_a1 > 0.0f)
						{
							// mirrored
							float m0 = y0 - y;
							float m1 = y1 - y;
							float a0 = std::max(mirror_a0 + mirror_ad * m0, 0.0f);
							float a1 = std::max(mirror_a0 + mirror_ad * m1, 0.0f);
							float dx0 = mirror_d0 + mirror_dd * m0;
							float dx1 = mirror_d0 + mirror_dd * m1;
							float yy0 = mirror_y0 + mirror_yd * m0;
							float yy1 = mirror_y0 + mirror_yd * m1;
#ifdef USE_TITLE_VERTEX_ARRAY
							color &= 0x00FFFFFF;
							color |= xs_RoundToInt(a1 * a1 * 255) << 24;
							*colorptr++ = color;
							*colorptr++ = color;
							*vertexptr++ = Vector2(x0 + dx1, yy1);
							*vertexptr++ = Vector2(x1 + dx1, yy1);
							color &= 0x00FFFFFF;
							color |= xs_RoundToInt(a0 * a0 * 255) << 24;
							*colorptr++ = color;
							*colorptr++ = color;
							*vertexptr++ = Vector2(x1 + dx0, yy0);
							*vertexptr++ = Vector2(x0 + dx0, yy0);
#else
							glColor4f(R, G, B, a1 * a1);
							glVertex2f(x0 + dx1, yy1);
							glVertex2f(x1 + dx1, yy1);
							glColor4f(R, G, B, a0 * a0);
							glVertex2f(x1 + dx0, yy0);
							glVertex2f(x0 + dx0, yy0);
#endif
						}
#endif
					}
				}
			}

			++titlefillptr;
		}

#ifdef USE_TITLE_MIRROR_WATER_EFFECT
		// mirror shift row
		mirror_y0 = mirror_y1;
		mirror_d0 = mirror_d1;
		mirror_a0 = mirror_a1;
#endif
	}