c++ - OpenCV 表单应用-Mat转Grayscale

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

我有一个问题,理论上很简单,但是...只是理论上 :) 我想将 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;
    }
}

知道为什么它会这样工作吗?我希望你能帮助我解决这个理论上简单的问题:)

最佳答案

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

你正试图用 24 位像素标志 blit 一个 8 位图像:

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

如果你想保留像素标志,你必须再次转换为 3 channel :

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 8 位图像)

关于c++ - OpenCV 表单应用-Mat转Grayscale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779645/

相关文章:

jquery - 使用 JQuery 和 CSS 的选项卡式布局

forms - ZF2 : Trying to understand Zend\Form

python - 在Python中垂直翻转图像

c++ - 负 RGB 值

c++ - 使用 C/C++ 制作球体上的点、线和多边形

c++ - 在运行时加载的 dll/so 中 atexit() 的行为是什么?

c# - 如何在 C# 和 C++ 代码之间共享常量?

c++ - 如何使用具有递归模板函数的线程

python - 表单提交未收到多个 html 选择

c++ - 在来自相机的视频上显示一个矩形