简体   繁体   中英

OpenCV form application- convert Mat to Grayscale

I have question which is theoretically very easy but... only theoretically :) I want to convert Mat frame image to gray scale. I know that i shoud use something like this:

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

But the result is this:

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

I don't know why i have 3 images in one after convert to grayscale. This is part of my code:

  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;
    }
}

Any ideas why it work like that? I hope that you can help me with this theoretically easy issue:)

i bet, your cvtColor() call works fine (try to imwrite() the output, and see yourself), but then:

you're trying to blit an 8bit image with a 24bit pixel-flag:

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

if you want to keep the pixel-flag, you'll have to convert to 3channels again:

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);

(can't try now, but iirc, there's no way to blit an 8bit image with winforms)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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