我正在iOS上使用OpenCV進行一些實驗。我試圖把一個PNG圖像上的檢測face.Thats的頂部我嘗試在一個deteted面放置圖像:OpenCV。圖像作爲灰度而不是RGB返回
但我得到這個:
我用這個功能來轉換一個UIImage到:與CvMat
- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;
cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
return cvMat;
}
在這裏,我添加圖像:
- (void)processImage:(cv::Mat&)image;
{
cv::vector<cv::Rect> faces;
cv::Mat frame_gray;
cvtColor(image, frame_gray, CV_BGRA2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(100, 100));
for(unsigned int i = 0; i < faces.size(); ++i)
{
rectangle(image, cv::Point(faces[i].x, faces[i].y),
cv::Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
cv::Scalar(0,255,255));
CGPoint pos;
pos.x = faces[i].x;
pos.y = faces[i].y;
cv::Rect roi(cv::Point(pos.x , pos.y), cv::Size(faces[i].width , faces[i].height ));
cv::Mat destinationROI = image(roi);
cv::Mat smallImage = [self faceIntoImageView:pos size:CGSizeMake(faces[i].width, faces[i].height)];
smallImage.copyTo(image(roi));
}
}
-(cv::Mat)faceIntoImageView:(CGPoint)position size:(CGSize)size
{
UIImage* face = [UIImage imageNamed:@"Face1.png"];
face = [self imageWithImage:face scaledToSize:size];
return [self cvMatFromUIImage:face];
}
那麼你的問題是什麼? – Shmidt
爲什麼輸出圖像與原始圖像完全不同。輸出圖像似乎是灰色的.. –
你可以粘貼方法'imageWithImage:face scaledToSize:'也? – Shmidt