我有一个问题,垫子的投资回报率被扭曲了。我认为下图最好地描述了这个问题。图像首先用 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 获得的掩码)。
这是我试图处理以获得车牌的图片。