Applying shades to an image while applying color

后端 未结 1 1862
无人共我
无人共我 2021-01-03 09:29

I am changing the color of a car based on a 3 slider values for HSV. I am able to change the color but it feels like paint.There is no originality in the image after color c

相关标签:
1条回答
  • 2021-01-03 09:31

    Hi you can refer below code in C++, here I am changing only hue value and if you want to change saturation and value just create a Mat with the slider position value and add or subtract with the appropriate channel(sat or val).

    int H=50;  
    Mat src, hsv, dst;
    char window_name[30] = "HSV Demo";
    void HSV_Demo( int, void* );
    
    
    int main( int argc, char** argv ){
    
      src = imread( "car.jpg", 1 );
      namedWindow( window_name, CV_WINDOW_AUTOSIZE );
      createTrackbar( "Hue", window_name,&H, 179, HSV_Demo );
      HSV_Demo( 0, 0 );
    
      while(true)
      {
        int c;
        c = waitKey( 20 );
        if( (char)c == 27 )
         break;
    
         if( (char)c == 's' ) imwrite("result.jpg",dst);
    
       }
    
    }
    
    
    void HSV_Demo( int, void* )
    {
      cvtColor(src, hsv,CV_BGR2HSV);
      Mat channel[3];
      split(hsv,channel);
      channel[0].setTo(H);
      Mat tmp[3] = { channel[0],channel[1],channel[2] };
      merge(tmp,3,dst);
      cvtColor(dst, dst,CV_HSV2BGR);
      imshow( window_name, dst );
    }
    

    Color1 Color2 Color3 Color4

    Edit:-

    Based on below comment here is the step to create black and white image using HSV by keeping shades.

    Note that the below method is not perfect as you can see the edges have irregularity, but you can try something like below or improve the below method itself.

    The idea is simple we will consider only the value-channel and add up with some constant(slider position) to create white and subtract to create black, then convert this to BGR image using cvtColor(). The shades may disappear if the addition or subtraction result out of bound.

    Before adding or subtracting we will create a mask image by segmenting the hue(here red) and create a new Mat with the constant(slider position) and mask, so that the background remains unchanged while addition or subtraction.

    void HSV_Demo( int, void* )
    {
      cvtColor(src, hsv,CV_BGR2HSV);
      Mat channel[3];
      split(hsv,channel);
    
       Mat thr1,thr2;
       inRange(hsv,Scalar(165,50,50),Scalar(179,255,255), thr1); //Create mask to change to avoid background
       inRange(hsv,Scalar(0,50,50),Scalar(10,255,255), thr2); //Create mask to change to avoid background
       thr1=thr1+thr2;
    
      if(H>255){
         if(H) H-=255;
         thr1.setTo(H,thr1); //Set the image to add to value which will create white color
        // channel[1].setTo(0);
         channel[2]=channel[2]+thr1;
       }
    
      else {
        H=255-H;
        thr1.setTo(H,thr1);
        // channel[1].setTo(0);
        channel[2]=channel[2]-thr1;//Set the image to subtract from value which will create black color
       }
    
      //Convert single channel to BGR
      Mat BGR;
      cvtColor(channel[2], BGR,CV_GRAY2BGR);
      imshow( window_name, BGR );
    
    
     /* Mat tmp[3] = { channel[0],channel[1],channel[2] };
      merge(tmp,3,dst);
      cvtColor(dst, dst,CV_HSV2BGR);
      imshow( window_name, dst );*/
    
    }
    

    In the above code you will get the same result by uncomment the commented code.

    Result:-
    enter image description here enter image description here

    enter image description here enter image description here

    0 讨论(0)
提交回复
热议问题