gpt4 book ai didi

ios - 检测对象的颜色并更改其颜色 ios

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:28 24 4
gpt4 key购买 nike

我正在使用 opencv 边缘和轮廓检测来裁剪一些对象。现在那个对象包含两种或多种颜色,所以我必须一个一个地改变颜色,例如我想改变 apple 的颜色并将其背景添加为图像,所以我该怎么做。

我正在尝试 referenve app ,但我对此一无所知。我用了link相同但卡在第三或第四步,因为不知道如何遮盖颜色。彼此link我使用并编写了如下所示的代码,但没有找到我的解决方案。请分享任何链接或教程。

  IplImage *image=[self CreateIplImageFromUIImage:[UIImage imageNamed:@"mask.png"]];
IplImage *hsvImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);

cvCvtColor(image,hsvImage,CV_RGB2HSV);

NSLog(@"size is %dx%d",cvGetSize(image).width,cvGetSize(image).height);

IplImage *hChannel = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
IplImage *sChannel = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
IplImage *vChannel = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

cvSplit(hsvImage, hChannel, sChannel, vChannel, NULL);

IplImage *cvinRangeOut=cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

cvinRangeOut=[self inRangeImage:hsvImage];

IplImage *dest = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);

IplImage *temp = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
cvMerge(hChannel, sChannel, NULL, NULL, temp);

cvSet(hsvImage, cv::Scalar(255,0,0),cvinRangeOut);// destination hue and sat

cvSplit(hsvImage, hChannel, sChannel, NULL, NULL);
cvMerge(hChannel, sChannel, vChannel, NULL, dest);
cvCvtColor(dest, dest, CV_HSV2BGR);

imageView2.image=[self UIImageFromIplImage:dest];
}

-(IplImage *)inRangeImage:(IplImage *)image
{
CvScalar source=CvScalar() ;//source color to replace
IplImage *cvInRange = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
CvScalar from=getScaler(source,false);
CvScalar to=getScaler(source, true);
cvInRangeS(image, from, to, cvInRange);
return cvInRange;
}


CvScalar getScaler(CvScalar seed,Boolean plus){
if(plus){
NSLog(@"rgb are %f, %f %f",seed.val[0],seed.val[1],seed.val[2]);
NSLog(@"seeds are %f %f %f",seed.val[0]+(seed.val[0]* (int)cv::threshold),seed.val[1]+(seed.val[1]*(int)cv::threshold),seed.val[2]+(seed.val[2]* (int)cv::threshold));
return CV_RGB(seed.val[0]+(seed.val[0]*(int)cv::threshold),seed.val[1]+(seed.val[1]*(int)cv::threshold),seed.val[2]+(seed.val[2]* (int)cv::threshold));
}else{
NSLog(@"seeds are %f %f %f",seed.val[0]-(seed.val[0]*(int)cv::threshold),seed.val[1]-(seed.val[1]*(int)cv::threshold),seed.val[2]-(seed.val[2]* (int)cv::threshold));
return CV_RGB(seed.val[0]-(seed.val[0]*(int)cv::threshold),seed.val[1]-(seed.val[1]*(int)cv::threshold),seed.val[2]-(seed.val[2]* (int)cv::threshold));
}
}

最佳答案

我是这样做的。希望它能帮助某人:

imageView.image=[UIImage imageNamed:@"test.png"];
cv::Mat img=[self cvMatFromUIImage:imageView.image];
cv::Mat hsvImage=cvCreateImage(img.size(),8, 3);
cv::cvtColor(img, hsvImage, CV_BGR2HSV);

std::vector<cv::Mat>channels;

cv::split(hsvImage, channels);
cv::Mat hue = channels[0];
cv::Mat dest;
cv::Mat temp=cvCreateImage(img.size(), 8, 3);

cv::inRange(hsvImage, cv::Scalar(90,50,50), cv::Scalar(130,255,255), dest);
cv::merge(channels, temp);
temp.setTo(cv::Scalar(60,255,255),dest);
cv::split(temp, channels);
cv::merge(channels, dest);
cv::cvtColor(dest, hsvImage, CV_HSV2BGR);
imageView.image=[self UIImageFromCVMat:hsvImage];

关于ios - 检测对象的颜色并更改其颜色 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com