我正在使用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));
}
}
答案 0 :(得分:1)
我是这样做的。希望它会帮助某人:
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];