繁体   English   中英

OpenCV表单应用程序-将Mat转换为灰度

[英]OpenCV form application- convert Mat to 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位像素标记对8位图像进行翻转:

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

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

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转换8bit图像)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM