color replacement in image for iphone application

前端 未结 2 1360
一生所求
一生所求 2020-12-07 19:44

Basically i want to implement color replacement feature for my paint application. Below are original and expected output

Original:

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 20:17

    Finally i am able to achieve some desired output using below javacv code and same ported to opencv too.

    this solution has 2 problems

    1. don't have edge detection, i think using contours i can achieve it
    2. replaced color has flat hue and sat which should set based on source pixel hue sat difference but not sure how to achieve that. may be instead of cvSet using cvAddS

      IplImage image = cvLoadImage("sample.png");
      CvSize cvSize = cvGetSize(image);
      
      
      IplImage hsvImage = cvCreateImage(cvSize, image.depth(),image.nChannels());
      
      IplImage hChannel = cvCreateImage(cvSize, image.depth(), 1); 
              IplImage  sChannel = cvCreateImage(cvSize, image.depth(), 1); 
              IplImage  vChannel = cvCreateImage(cvSize, image.depth(), 1);
      cvSplit(hsvImage, hChannel, sChannel, vChannel, null);
      
      
      IplImage cvInRange = cvCreateImage(cvSize, image.depth(), 1);
      CvScalar source=new CvScalar(72/2,0.07*255,66,0); //source color to replace
      CvScalar from=getScaler(source,false);
      CvScalar to=getScaler(source, true);
      
      cvInRangeS(hsvImage, from , to, cvInRange);
      
      IplImage dest = cvCreateImage(cvSize, image.depth(), image.nChannels());
      
      IplImage temp = cvCreateImage(cvSize, IPL_DEPTH_8U, 2);
      cvMerge(hChannel, sChannel, null, null, temp);
      
      cvSet(temp, new CvScalar(45,255,0,0), cvInRange);// destination hue and sat
      cvSplit(temp, hChannel, sChannel, null, null);
      cvMerge(hChannel, sChannel, vChannel, null, dest);
      cvCvtColor(dest, dest, CV_HSV2BGR);
      cvSaveImage("output.png", dest);
      

    method to for calculating threshold

        CvScalar getScaler(CvScalar seed,boolean plus){
        if(plus){
            return CV_RGB(seed.red()+(seed.red()*thresold),seed.green()+(seed.green()*thresold),seed.blue()+(seed.blue()*thresold));
        }else{
            return CV_RGB(seed.red()-(seed.red()*thresold),seed.green()-(seed.green()*thresold),seed.blue()-(seed.blue()*thresold));
        }
            }
    

提交回复
热议问题