template <typename PointInT> double HSVColorCoherence<PointInT>::computeCoherence (PointInT &source, PointInT &target) { // convert color space from RGB to HSV RGBValue source_rgb, target_rgb; source_rgb.float_value = source.rgba; target_rgb.float_value = target.rgba; float source_h, source_s, source_v, target_h, target_s, target_v; RGB2HSV (source_rgb.Red, source_rgb.Blue, source_rgb.Green, source_h, source_s, source_v); RGB2HSV (target_rgb.Red, target_rgb.Blue, target_rgb.Green, target_h, target_s, target_v); // hue value is in 0 ~ 2pi, but circulated. const float _h_diff = fabs (source_h - target_h); float h_diff; if (_h_diff > 0.5) h_diff = h_weight_ * (_h_diff - 0.5) * (_h_diff - 0.5); else h_diff = h_weight_ * _h_diff * _h_diff; const float s_diff = s_weight_ * (source_s - target_s) * (source_s - target_s); const float v_diff = v_weight_ * (source_v - target_v) * (source_v - target_v); const float diff2 = h_diff + s_diff + v_diff; return (1.0 / (1.0 + weight_ * diff2)); }
void RGB3ub2HSV(unsigned char R, unsigned char G, unsigned char B, float& H, float& S, float&V) { float r = float(R) / 255.0; float g = float(G) / 255.0; float b = float(B) / 255.0; RGB2HSV(r, g, b, H, S, V); }
void YellowColorFilter::filterImage(ImageRGB & image) { // Image size; int width = image.width(); int height = image.height(); // For every pixel in the image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the RGB values Rgb<unsigned char &> pixelRGB = image.at(x, y); // To save the Hue, Saturation and Value float hue, saturation, value; // Convert RGB to HSV. RGB2HSV(pixelRGB.red, pixelRGB.green, pixelRGB.blue, hue, saturation, value); // If the color is yellow. if (hue >= 25 && hue <= 60 && saturation >= 0.60) { // If the color is within our yellow range, make the output pixel white. pixelRGB.red = 255; pixelRGB.green = 255; pixelRGB.blue = 255; } else { // Else make the pixel black. pixelRGB.red = 0; pixelRGB.green = 0; pixelRGB.blue = 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); }
/*----------------------------------------------------------------*/ void RGB2HSV (int _r, int _g, int _b, float& _h, float& _s, float& _v) { float r = ((float)_r)/255.0; float g = ((float)_g)/255.0; float b = ((float)_b)/255.0; RGB2HSV (r, g, b, _h, _s, _v); }
HSV yCbCr2HSV(YCbCr422 ycbcr) { RGBf rgb; HSV hsv; rgb = yCbCr2RGB(ycbcr); hsv = RGB2HSV(rgb); return hsv; }
template <typename PointInT> double CombinedCoherence<PointInT>::computeCoherence (PointInT &source, PointInT &target) { // convert color space from RGB to HSV RGBValue source_rgb, target_rgb; source_rgb.int_value = source.rgba; target_rgb.int_value = target.rgba; float source_h, source_s, source_v, target_h, target_s, target_v; RGB2HSV (source_rgb.Red, source_rgb.Blue, source_rgb.Green, source_h, source_s, source_v); RGB2HSV (target_rgb.Red, target_rgb.Blue, target_rgb.Green, target_h, target_s, target_v); // hue value is in 0 ~ 2pi, but circulated. const float _h_diff = fabsf (source_h - target_h); // Also need to compute distance other way around circle - but need to check which is closer to 0 float _h_diff2; if (source_h < target_h) _h_diff2 = fabsf (1.0f + source_h - target_h); //Add 2pi to source, subtract target else _h_diff2 = fabsf (1.0f + target_h - source_h); //Add 2pi to target, subtract source float h_diff; //Now we need to choose the smaller distance if (_h_diff < _h_diff2) h_diff = static_cast<float> (h_weight_) * _h_diff * _h_diff; else h_diff = static_cast<float> (h_weight_) * _h_diff2 * _h_diff2; const float s_diff = static_cast<float> (s_weight_) * (source_s - target_s) * (source_s - target_s); const float v_diff = static_cast<float> (v_weight_) * (source_v - target_v) * (source_v - target_v); //const float color_diff = h_diff + s_diff + v_diff; //if (color_diff > 0.1) // return 0; Eigen::Vector4f p = source.getVector4fMap (); Eigen::Vector4f p_dash = target.getVector4fMap (); double d = (p - p_dash).norm () * dist_weight_; const float diff2 = h_diff + s_diff + v_diff + d; return (1.0 / (1.0 + weight_ * diff2)); }
template <typename PointInT> double RejectivePointCloudCoherence<PointInT>::computeScoreHSV (const PointInT &p1,const PointInT& p2, float dist) { // convert color space from RGB to HSV RGBValue source_rgb, target_rgb; source_rgb.int_value = p1.rgba; target_rgb.int_value = p2.rgba; float source_h, source_s, source_v, target_h, target_s, target_v; RGB2HSV (source_rgb.Red, source_rgb.Blue, source_rgb.Green, source_h, source_s, source_v); RGB2HSV (target_rgb.Red, target_rgb.Blue, target_rgb.Green, target_h, target_s, target_v); // hue value is in 0 ~ 2pi, but circulated. const float _h_diff = fabsf (source_h - target_h); // Also need to compute distance other way around circle - but need to check which is closer to 0 float _h_diff2; if (source_h < target_h) _h_diff2 = fabsf (1.0f + source_h - target_h); //Add 2pi to source, subtract target else _h_diff2 = fabsf (1.0f + target_h - source_h); //Add 2pi to target, subtract source float h_diff; //Now we need to choose the smaller distance if (_h_diff < _h_diff2) h_diff = static_cast<float> (h_weight_) * _h_diff * _h_diff; else h_diff = static_cast<float> (h_weight_) * _h_diff2 * _h_diff2; const float s_diff = static_cast<float> (s_weight_) * (source_s - target_s) * (source_s - target_s); const float v_diff = static_cast<float> (v_weight_) * (source_v - target_v) * (source_v - target_v); //const float color_diff = h_diff + s_diff + v_diff; //if (color_diff > 0.1) // return 0; const float diff2 = h_diff + s_diff + v_diff + dist / maximum_distance_; return (1.0 / (1.0 + diff2)); }
CImage *CImageRGBtoHSV(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 = RGB2HSV(i); ncimg->C[0]->val[p]=t0(i); ncimg->C[1]->val[p]=t1(i); ncimg->C[2]->val[p]=t2(i); } return(ncimg); }
bool is_in_range(guint8 * pixel, color l, color h, COLOR_SPACE space) { color c = make_color(pixel[0], pixel[1], pixel[2]); switch (space) { case COLOR_SPACE::HSL: c = RGB2HSL(c); break; case COLOR_SPACE::HSI: c = RGB2HSI(c); break; case COLOR_SPACE::HSV: c = RGB2HSV(c); break; case COLOR_SPACE::RGB: //we're good break; default: //invalid - throw exception? return false; }; return ( ( //hue ((l[0] < h[0]) && ( //low < high (c[0] >= l[0]) && (c[0] <= h[0]) )) || ((l[0] >= h[0]) && ( //low > high (c[0] >= l[0]) || (c[0] <= h[0]) )) ) && ( //saturation (c[1] >= l[1]) && (c[1] <= h[1]) ) && ( //value (c[2] >= l[2]) && (c[2] <= h[2]) ) ); }
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); }
void CAtmoGdiDisplayCaptureInput::CalcColors() { tRGBColor pixelColor; #ifdef UseGdiGetPixel COLORREF pixel; #ifdef UseGdiDesktopGetPixel HDC hdcScreen; #endif #endif int xx,yy; int capture_area_width = (m_ScreenSourceRect.right-m_ScreenSourceRect.left); int capture_area_height = (m_ScreenSourceRect.bottom-m_ScreenSourceRect.top); #ifndef UseGdiDesktopGetPixel HBITMAP hTempBitmap = CreateCompatibleBitmap(m_hdcScreen, capture_area_width, capture_area_height); HGDIOBJ hOldBmp = SelectObject(m_hTempBitmapDC, hTempBitmap); #endif #ifndef UseGdiGetPixel BITMAPINFO bmpInfo; ZeroMemory(&bmpInfo, sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GetDIBits(m_hTempBitmapDC,hTempBitmap,0,1,NULL,&bmpInfo,DIB_RGB_COLORS); // bmpInfo.bmiHeader.biWidth = capture_area_width; // bmpInfo.bmiHeader.biHeight = -capture_area_height; if(bmpInfo.bmiHeader.biSizeImage<=0) bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth * abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8; bmpInfo.bmiHeader.biCompression=BI_RGB; #endif // have a look into VncDesktop.cpp :-) **g vncDesktop::CaptureScreen // vncDesktop::EnableOptimisedBlits() vncDesktop::CopyToBuffer( // http://cboard.cprogramming.com/archive/index.php/t-89037.html // http://cboard.cprogramming.com/showthread.php?t=76907 das schaut gut aus!!!!!! // damit spart man die GetDIBits(...) aufrufe... und bekommt mittelsBitBlit gleich alles // ins eigene Ram Kopiert... Full Access to display ram... :-))) // #ifndef UseGdiDesktopGetPixel BitBlt(m_hTempBitmapDC, 0, 0, capture_area_width, capture_area_height, m_hdcScreen, m_ScreenSourceRect.left, m_ScreenSourceRect.top, SRCCOPY); #endif int index = (m_CurrentFrame * CAP_WIDTH); int indexSkip = ((m_rowsPerFrame-1) * CAP_WIDTH); unsigned int col = 0; #ifdef UseGdiGetPixel #ifdef UseGdiDesktopGetPixel hdcScreen = GetDC(NULL); #endif for(int y=m_CurrentFrame; y<CAP_HEIGHT; y+=m_rowsPerFrame ) { // yy = (y * capture_area_height) / CAP_HEIGHT; // yy = yy + m_ScreenSourceRect.top + m_tShift; yy = m_iSrcRows[y]; for(int x=0;x<CAP_WIDTH;x++) { // xx = (x * capture_area_width) / CAP_WIDTH; // xx = xx + m_ScreenSourceRect.left + m_lShift; #ifndef UseGdiDesktopGetPixel pixel = GetPixel(m_hTempBitmapDC, m_iSrcCols[x], yy); #else pixel = GetPixel(hdcScreen, m_iSrcCols[x], yy); #endif pixelColor.r = GetRValue(pixel); pixelColor.g = GetGValue(pixel); pixelColor.b = GetBValue(pixel); HSV_Img[index++] = RGB2HSV(pixelColor); } index += indexSkip; } #ifdef UseGdiDesktopGetPixel ReleaseDC(NULL, hdcScreen); #endif #else switch(bmpInfo.bmiHeader.biBitCount) { case 8: { // [TF] 8bit support added by Tobias Fleischer/Tobybear - still untested and experimental, might not work! int nColors = bmpInfo.bmiHeader.biClrUsed ? bmpInfo.bmiHeader.biClrUsed : 1 << bmpInfo.bmiHeader.biBitCount; for( int y=m_CurrentFrame; y<CAP_HEIGHT; y+=m_rowsPerFrame ) { // yy = (y * capture_area_height) / CAP_HEIGHT; // yy = yy + m_ScreenSourceRect.top + m_tShift; if(bmpInfo.bmiHeader.biHeight>0) yy = bmpInfo.bmiHeader.biHeight - m_iSrcRows[y] - 1; else yy = m_iSrcRows[y]; // eine Bildzeile in meinen Arbeitsbuffer kopieren! // geht vielfach schneller als GetPixel aufrufen... wenn man dein Speicher dann direkt // zugreift... :-) GetDIBits(m_hTempBitmapDC,hTempBitmap,yy,1,m_PixelBuffer,&bmpInfo,DIB_RGB_COLORS); for(int x=0; x<CAP_WIDTH; x++) { xx = m_iSrcCols[x]; col = m_PixelBuffer[xx] % nColors; pixelColor.b = bmpInfo.bmiColors[col].rgbBlue; pixelColor.g = bmpInfo.bmiColors[col].rgbGreen; pixelColor.r = bmpInfo.bmiColors[col].rgbRed; HSV_Img[index++] = RGB2HSV(pixelColor); } index += indexSkip; } break; } case 16: { // [TF] 16bit support added by Tobias Fleischer/Tobybear - tested for(int y=m_CurrentFrame; y<CAP_HEIGHT; y+=m_rowsPerFrame ) { // yy = (y * capture_area_height) / CAP_HEIGHT; // yy = yy + m_ScreenSourceRect.top + m_tShift; if(bmpInfo.bmiHeader.biHeight>0) yy = bmpInfo.bmiHeader.biHeight - m_iSrcRows[y] - 1; else yy = m_iSrcRows[y]; // eine Bildzeile in meinen Arbeitsbuffer kopieren! // geht vielfach schneller als GetPixel aufrufen... wenn man dein Speicher dann direkt // zugreift... :-) GetDIBits(m_hTempBitmapDC,hTempBitmap,yy,1,m_PixelBuffer,&bmpInfo,DIB_RGB_COLORS); for(int x=0;x<CAP_WIDTH;x++) { xx = m_iSrcCols[x] * 2; col = m_PixelBuffer[xx] + (m_PixelBuffer[xx+1]<<8); pixelColor.b = (col & 0x1F)<<3; pixelColor.g = (col & 0x3E0)>>2; pixelColor.r = (col & 0x7C00)>>7; HSV_Img[index++] = RGB2HSV(pixelColor); } index += indexSkip; } break; } case 24: { for(int y=m_CurrentFrame; y<CAP_HEIGHT; y+=m_rowsPerFrame ) { // yy = (y * capture_area_height) / CAP_HEIGHT; // yy = yy + m_ScreenSourceRect.top + m_tShift; if(bmpInfo.bmiHeader.biHeight>0) yy = bmpInfo.bmiHeader.biHeight - m_iSrcRows[y] - 1; else yy = m_iSrcRows[y]; // eine Bildzeile in meinen Arbeitsbuffer kopieren! // geht vielfach schneller als GetPixel aufrufen... wenn man dein Speicher dann direkt // zugreift... :-) GetDIBits(m_hTempBitmapDC,hTempBitmap,yy,1,m_PixelBuffer,&bmpInfo,DIB_RGB_COLORS); for(int x=0;x<CAP_WIDTH;x++) { xx = m_iSrcCols[x] * 3; pixelColor.b = m_PixelBuffer[xx++]; pixelColor.g = m_PixelBuffer[xx++]; pixelColor.r = m_PixelBuffer[xx++]; HSV_Img[index++] = RGB2HSV(pixelColor); } index += indexSkip; } break; } case 32: { for(int y=m_CurrentFrame; y<CAP_HEIGHT; y+=m_rowsPerFrame ) { if(bmpInfo.bmiHeader.biHeight>0) yy = bmpInfo.bmiHeader.biHeight - m_iSrcRows[y] - 1; else yy = m_iSrcRows[y]; // eine Bildzeile in meinen Arbeitsbuffer kopieren! // geht vielfach schneller als GetPixel aufrufen... wenn man dein Speicher dann direkt // zugreift... :-) GetDIBits(m_hTempBitmapDC,hTempBitmap,yy,1,m_PixelBuffer,&bmpInfo,DIB_RGB_COLORS); for(int x=0;x<CAP_WIDTH;x++) { // xx = (x * capture_area_width) / CAP_WIDTH; // xx = xx + m_ScreenSourceRect.left + m_lShift; xx = m_iSrcCols[x] * 4; pixelColor.b = m_PixelBuffer[xx++]; pixelColor.g = m_PixelBuffer[xx++]; pixelColor.r = m_PixelBuffer[xx++]; HSV_Img[index++] = RGB2HSV(pixelColor); } index += indexSkip; } break; } } #endif #ifndef UseGdiDesktopGetPixel SelectObject(m_hTempBitmapDC, hOldBmp); DeleteObject(hTempBitmap); #endif m_CurrentFrame++; if(m_CurrentFrame >= m_rowsPerFrame) m_CurrentFrame = 0; m_pAtmoDynData->getLivePacketQueue()->AddPacket( m_pAtmoColorCalculator->AnalyzeHSV( HSV_Img ) ); }