color replacement in image for iphone application

前端 未结 2 1106
一生所求
一生所求 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:14

    I know this answer will be useful to someone someday. try this out in your view viewdidLoad() override method for iOS. image in the code snippet below should be from your UIImageView seed also is fixed.you can make it dynamic based on user tap event.

     cv::Mat mask = cv::Mat::zeros(image.rows + 2, image.cols + 2, CV_8U);
     imageView.image = [self UIImageFromCVMat:image];
     cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
    
      try {
        if(seed.x > 0 && seed.y > 0){
            cv::floodFill(image, mask, seed, cv::Scalar(50, 155, 20) ,0,        cv::Scalar(2,2, 2), cv::Scalar(2,2, 2), 8);
            cv::floodFill(image, mask, seed2, cv::Scalar(50, 155, 20) ,0, cv::Scalar(2,2, 2), cv::Scalar(2,2, 2), 8);
            cv::floodFill(image, mask, seed3, cv::Scalar(50, 155, 0) ,0, cv::Scalar(2,2, 2), cv::Scalar(2,2, 2), 8);
    
        }
    } catch (Exception ex) {
    
    }
    cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
    
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.image = [self UIImageFromCVMat:image];
    
    0 讨论(0)
  • 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));
        }
            }
    
    0 讨论(0)
提交回复
热议问题