线性渐变色中的OpenCV [英] Linear Color Gradient in openCV

查看:2287
本文介绍了线性渐变色中的OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建两种颜色,如Photoshop渐变。
R,G,B的两个颜色是输入和结果将梯度的垫。我试着像5小时至少,我找不到作为Photoshop中的确切作用。
我试图创建自己的公式(我找不到任何在网络上),通过改变RGB到HSV,然后加入色调的差异,相对于行的总数量,垫的每一行,也减少强度图像的中心,然后再提高它。在code是自我解释。

此外,如果任何人能告诉我确切的公式创建一个梯度这将是真正有用的。

在这里的Photoshop渐变的样子

这是我从我的code得到

  INT R1,G1,B1,R2,G2,B2; R1 = 255;
 G1 = 0;
 B1 = 0;
 R2 = 0;
 G2 = 255;
 B2 = 0; 垫输入= imread(img.jpg);
 垫的color1(input.size(),input.type());
 垫COLOR2(input.size(),input.type()); 矢量<&垫GT; BGR1;
 矢量<&垫GT; bgr2; 拆分(COLOR1,BGR1);
 BGR1 [0] = B1;
 BGR1 [1] = G1;
 BGR1 [2] = r1为
 合并(BGR1,颜色1); 拆分(颜色2,bgr2);
 bgr2 [0] = B2;
 bgr2 [1] = G 2;
 bgr2 [2] = R2;
 合并(bgr2,COLOR2); 矢量<&垫GT; hls1;
 矢量<&垫GT; hls2; cvtColor(颜色1,颜色1,CV_BGRA2BGR);
 cvtColor(颜色1,颜色1,CV_BGR2HSV);
 拆分(COLOR1,hls1); cvtColor(颜色2,颜色2,CV_BGRA2BGR);
 cvtColor(颜色2,颜色2,CV_BGR2HSV);
 拆分(颜色2,hls2); 双H1 = hls1 [0] .AT&下; UCHAR>(0,0);
 双H2 = hls2 [0] .AT&下; UCHAR>(0,0);
 双DIF =(H2 - H1)/ input.rows;
 双H =​​ H1; 双halfL = 255/2;
 双halfR = input.rows / 2;
 双LDIF = halfL / halfR;
 双L = 255;
 布尔isHalf = FALSE; 的for(int i = 0; I< input.rows;我++)
 {
  对于(INT J = 0; J< input.cols; J ++)
  {
   hls1 [0] .AT&下; UCHAR>(I,J)= H;
   hls1 [2] .AT&下; UCHAR>(I,J)= 1;
  }
  如果(isHalf == FALSE){
   L - = LDIF;
  }
  其他{
   L + = LDIF;
  }  如果(ⅰ&下; input.rows * 0.40)
  {
   H + = DIF * 0.40;
  }
  否则如果(ⅰ&下; input.rows * 0.60)
  {
   H + = DIF * 3;
  }
  其他
  {
   H + = DIF * 0.40;
  }  如果(ⅰ&GT = input.rows / 2)
  {
   isHalf = TRUE;
  }
 } 合并(hls1,颜色1);
 合并(hls2,COLOR2); cvtColor(颜色1,颜色1,CV_HSV2BGR);
 cvtColor(颜色1,颜色1,CV_BGR2BGRA); cvtColor(颜色2,颜色2,CV_HSV2BGR);
 cvtColor(颜色2,颜色2,CV_BGR2BGRA); namedWindow(颜色1,CV :: WINDOW_NORMAL);
 resizeWindow(颜色1,color1.size()宽/ 2,color1.size()高度/ 2。);
 imshow(颜色1,颜色1); waitKey(0);
 destroyAllWindows();
 系统(暂停);


解决方案

我纠正我的第一个code

这似乎是一个非常复杂的code的东西,应该更容易。
我会做这样的事情。

  INT taille = 500;
垫图像(taille,taille,CV_8UC3);
对于(INT Y = 0; Y< taille; Y ++){
   Vec3b VAL;
   VAL [0] = 0; VAL [1] =(Y * 255)/ taille; VAL [2] =(taille-Y)* 255 / taille;
   为(中间体X = 0; X&下; taille; X ++)
      image.at&所述; Vec3b>(Y,X)= VAL;
}

在Micka的建议,我想补充与taille = 400的结果的照片;

i am trying to create gradient of two colors like Photoshop. r,g,b of two colors is input and result will be the Mat of gradient. I tried it like for 5 hours at least and i could not find exact effect as of the Photoshop. I tried to create my own formula (as i could not find any on the web), by changing RGB to HSV and then adding the difference of hue, with respect to the total number of rows, to each row of Mat with and also decreasing intensity to the center of image and then increasing it again. The code is self explanatory.

Additionally if anyone can tell me the exact formula for creating a gradient it will be really helpful.

here is how Photoshop gradient looks like

and this is what i get from my code

 int r1, g1, b1, r2, g2, b2;

 r1 = 255;
 g1 = 0;
 b1 = 0;
 r2 = 0;
 g2 = 255;
 b2 = 0;

 Mat input = imread("img.jpg");
 Mat color1(input.size(), input.type());
 Mat color2(input.size(), input.type());

 vector<Mat> bgr1;
 vector<Mat> bgr2;

 split(color1, bgr1);
 bgr1[0] = b1;
 bgr1[1] = g1;
 bgr1[2] = r1;
 merge(bgr1, color1);

 split(color2, bgr2);
 bgr2[0] = b2;
 bgr2[1] = g2;
 bgr2[2] = r2;
 merge(bgr2, color2);

 vector<Mat> hls1;
 vector<Mat> hls2;

 cvtColor(color1, color1, CV_BGRA2BGR);
 cvtColor(color1, color1, CV_BGR2HSV);
 split(color1, hls1);

 cvtColor(color2, color2, CV_BGRA2BGR);
 cvtColor(color2, color2, CV_BGR2HSV);
 split(color2, hls2);

 double h1 = hls1[0].at<uchar>(0, 0);
 double h2 = hls2[0].at<uchar>(0, 0);
 double dif = (h2 - h1) / input.rows;
 double h = h1;

 double halfL = 255 / 2;
 double halfR = input.rows / 2;
 double ldif = halfL / halfR;
 double l = 255;
 bool isHalf = false;

 for (int i = 0; i < input.rows; i++)
 {
  for (int j = 0; j < input.cols; j++)
  {
   hls1[0].at<uchar>(i, j) = h;
   hls1[2].at<uchar>(i, j) = l;
  }


  if (isHalf == false){
   l -= ldif;
  }
  else{
   l += ldif;
  }

  if (i < input.rows * 0.40)
  {
   h += dif * 0.40;
  }
  else if (i < input.rows * 0.60)
  {
   h += dif * 3;
  }
  else
  {
   h += dif * 0.40;
  }

  if (i >= input.rows / 2)
  {
   isHalf = true;
  }
 }

 merge(hls1, color1);
 merge(hls2, color2);

 cvtColor(color1, color1, CV_HSV2BGR);
 cvtColor(color1, color1, CV_BGR2BGRA);

 cvtColor(color2, color2, CV_HSV2BGR);
 cvtColor(color2, color2, CV_BGR2BGRA);

 namedWindow("Color1", cv::WINDOW_NORMAL);
 resizeWindow("Color1", color1.size().width / 2, color1.size().height / 2);
 imshow("Color1", color1);

 waitKey(0);
 destroyAllWindows();
 system("pause");

解决方案

I corrected my first code

It seems to be a really complex code for something that should be easier. I would do something like that.

int taille = 500;    
Mat image(taille,taille,CV_8UC3);
for(int y = 0; y < taille; y++){
   Vec3b val;
   val[0] = 0; val[1] = (y*255)/taille; val[2] = (taille-y)*255/taille;
   for(int x = 0; x < taille; x++)
      image.at<Vec3b>(y,x) = val;
}

On Micka's advice, I add a picture of the result with taille = 400;

这篇关于线性渐变色中的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆