我遇到了垫子的ROI变形的问题。我认为下面的图片最能说明问题。首先使用cv:cvtColor将图像变为灰色,然后在灰色垫子上使用cv :: threshold,然后在阈值垫上使用Canny,然后我找到轮廓。我浏览了所有的轮廓,并根据轮廓的面积找到最大的轮廓。然后我使用drawContour将其用作阈值垫上的遮罩。那就是我使用以下代码的地方:
cv::Mat inMat = [self cvMatFromUIImage: inputImage];
cv::Mat grayMat, threshMat, canny_output;
cv::cvtColor(inMat, grayMat, CV_BGR2GRAY);
cv::threshold(grayMat, threshMat, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Canny(threshMat, canny_output, 100, 100, 3);
cv::Mat drawing = cv::Mat::zeros( canny_output.size(), CV_8UC3 );
std::vector<cv::Vec4i> hierarchy;
std::vector<std::vector<cv::Point> > contours;
cv::findContours(canny_output, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
NSMutableArray *contourAreasArray = [NSMutableArray new];
// Sorts the contour areas from greatest or smallest
for (int i = 0; i < contours.size(); ++i) {
NSNumber *contourArea = [NSNumber numberWithDouble:cv::contourArea(contours[i])];
[contourAreasArray addObject:contourArea];
}
NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
[contourAreasArray sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
cv::Rect maskRect;
Mat mask;
for (int i = 0; i < contours.size(); ++i)
{
NSNumber *contourArea = [NSNumber numberWithDouble:cv::contourArea(contours[i])];
cv::Rect brect = cv::boundingRect(contours[i]);
if ([contourArea doubleValue] == [[contourAreasArray objectAtIndex:0] doubleValue]) {
//NSLog(@"Drawing Contour");
maskRect = cv::boundingRect(contours[i]);
cv::drawContours(drawing, contours, i, cvScalar(255,255,255), CV_FILLED);
cv::rectangle(drawing, brect, cvScalar(255));
}
}
cv::Rect region_of_interest = maskRect;
cv::Mat roiImage = cv::Mat(threshMat, region_of_interest);
// final result is converted to UIImage for display
UIImage *threshUIImage = [self UIImageFromCVMat:drawing];
UIImage *resultUIImage = [self UIImageFromCVMat:roiImage];
self.plateImageView.image = resultUIImage;
self.threshImageView.image = threshUIImage;
我在哪里错了?如果你们需要更多代码让我知道,生病了就更乐意发布更多代码......(左图是提取的(ROI Mat),右边是我从drawContour获得的面具)。
这是我试图获得车牌的图片。
答案 0 :(得分:0)
好吧我终于明白了...解决方案是做以下事情,基本上只是将ROI图像复制到另一个垫子中:
cv::Rect region_of_interest = maskRect;
cv::Mat roiImage = cv::Mat(threshMat, region_of_interest);
cv::Mat finalROIImage;
roiImage.copyTo(finalROIImage);