簡體   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