c++ - OpenCV:为什么 ROI Mat 会失真?

标签 c++ ios opencv image-processing

我遇到了垫子的 ROI 失真的问题。我认为下图最能说明问题。图像首先用 cv:cvtColor 变成灰色,然后我在灰色垫子上使用 cv::threshold,然后我在阈值垫子上使用 Canny,然后我找到轮廓。我遍历所有轮廓并根据轮廓面积找到最大的轮廓。然后我使用 drawContour 将其用作阈值垫上的 mask 。这就是我使用以下代码的地方:

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 获得的掩码)。

enter image description here

The image I'm trying to process. (Basically getting the license plate)

这是我为获得车牌而尝试处理的图片。

最佳答案

好吧,我终于想通了......解决方案是执行以下操作,基本上只是将 ROI 图像复制到另一个 Mat 中:

cv::Rect region_of_interest = maskRect;
cv::Mat roiImage = cv::Mat(threshMat, region_of_interest);
cv::Mat finalROIImage;
roiImage.copyTo(finalROIImage);

关于c++ - OpenCV:为什么 ROI Mat 会失真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700387/

相关文章:

opencv - 在 OpenCV 中检测不完整的矩形(缺角/短边)

c++ - findcontours opencv中的段错误

c++ - 使用增量时间时如何保持跳跃高度相同?

c++ - 基本派生类成员覆盖。挑战事件11.3

C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)

ios - 与 iOS 5 的文档交互

c++ - 传入一个 vector 作为具有设定值的参数

ios - 由于未捕获的异常而终止应用程序,原因 : 'attempt to delete row 3 from section 1 which only contains 0 rows before the update'

ios - 在swift 4中将对象数组中的选定项目保存到UserDefaults

python - 对彩色图像 OpenCV 使用 matchTemplate