【问题标题】:OpenCV form application- convert Mat to GrayscaleOpenCV 表单应用程序 - 将 Mat 转换为灰度
【发布时间】:2015-03-02 23:56:37
【问题描述】:

我有一个理论上很容易的问题,但......只是理论上:) 我想将 Mat 帧图像转换为灰度。我知道我应该使用这样的东西:

cv::cvtColor(frameA1, grayImageA1, CV_BGR2GRAY);

但是结果是这样的:

http://i57.tinypic.com/jfvodt.jpg

我不知道为什么我在转换为灰度后会有 3 张图像。这是我的代码的一部分:

  private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
    BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
    capture1.open(1);
    while (camStatus1){
        if (camBusy1) continue;
        capture1.read(frameA1);
        if (frameA1.empty()) continue;
        cv::cvtColor(frameA1, grayImageA1, CV_BGR2GRAY);
        worker->ReportProgress(1);
    }

}
private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
    if (!camBusy1){
        camBusy1 = 1;
        System::Drawing::Graphics^ graphics = imageBox1->CreateGraphics();
        System::IntPtr ptr(frameA1.ptr());
        System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(frameA1.cols, frameA1.rows, frameA1.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
        System::Drawing::RectangleF rect(0, 0, imageBox1->Width, imageBox1->Height);
        graphics->DrawImage(b, rect);
        if (debugA){
            System::Drawing::Graphics^ graphics = imageBox3->CreateGraphics();
            System::IntPtr ptr(grayImageA1.ptr());
            System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(grayImageA1.cols, grayImageA1.rows, grayImageA1.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
            System::Drawing::RectangleF rect(0, 0, imageBox3->Width, imageBox3->Height);
            graphics->DrawImage(b, rect);
        }
        camBusy1 = 0;
    }
}

任何想法为什么它会这样工作?我希望你能帮助我解决这个理论上很简单的问题:)

【问题讨论】:

    标签: c++ forms opencv visual-studio-2013 grayscale


    【解决方案1】:

    我敢打赌,你的 cvtColor() 调用工作正常(尝试 imwrite() 输出,然后看看你自己),但是:

    您正在尝试使用 24 位像素标志对 8 位图像进行 blit:

    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(grayImageA1.cols, grayImageA1.rows, grayImageA1.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
    

    如果要保留像素标志,则必须再次转换为 3channels:

    Mat grayImage24;
    cv::cvtColor(grayImageA1, grayImage24, CV_GRAY2BGR);
    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(grayImage24.cols, grayImage24.rows, grayImage24.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
    

    (现在不能尝试,但是iirc,没有办法用winforms blit 8bit图像)

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2012-01-17
      • 2012-05-07
      • 2021-04-05
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2016-09-09
      相关资源
      最近更新 更多