【问题标题】:QImage OpenCV - setPixel with only green and black colorsQImage OpenCV - setPixel 只有绿色和黑色
【发布时间】:2013-01-27 19:17:38
【问题描述】:

我使用 Qt 制作了一个简单的图形用户界面,并使用 OpenCV 对网络摄像头流进行处理,即精明边缘检测。

我尝试在网络摄像头的两个显示器之间实现切换:

1*) “正常模式”:一种灰度显示,其中网络摄像头提供具有灰度颜色的边界检测视频

2*) "greenMode" :绿色和黑色显示,其中网络摄像头提供相同的“检测到边界”,但具有绿色和黑色。

第一个作品(带有灰度)作品。结果如下:

现在我遇到了第二个问题。这是我找不到解决方案的代码部分:

  // Init capture
  capture = cvCaptureFromCAM(0);
  first_image = cvQueryFrame( capture );
  // Init current qimage
  current_qimage = QImage(QSize(first_image->width,first_image->height),QImage::Format_RGB32);

  IplImage* frame = cvQueryFrame(capture);
  int w = frame->width;
  int h = frame->height;

  if (greenMode) // greenMode : black and green result
  {
    current_image = cvCreateImage(cvGetSize(frame),8,3); 
    cvCvtColor(frame,current_image,CV_BGR2RGB);

    for(int j = 0; j < h; j++)
    {  
      for(int i = 0; i < w; i++)
      {
        current_qimage.setPixel(i,j,qRgb(current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1]));
      }
    }
  }
  else // normal Mode : grayscale result WHICH WORKS
  {
    current_image = cvCreateImage(cvGetSize(frame),8,1); 
    cvCvtColor(frame,current_image,CV_BGR2GRAY);

    for(int j = 0; j < h; j++)
    {  
      for(int i = 0; i < w; i++)
      {
        current_qimage.setPixel(i,j,qRgb(current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1]));

      }
    }
  }
  gaussianfilter(webcam_off);
  border_detect(webcam_off);
  cvReleaseImage(&current_image);
  repaint();

“greenMode”似乎没有用这个“setPixel”放置好的像素(我取中间的 rgb 值:current_image-&gt;imageData[i+j*w+1]):

current_image = cvCreateImage(cvGetSize(frame),8,3); 
    cvCvtColor(frame,current_image,CV_BGR2RGB);

    for(int j = 0; j < h; j++)
    {  
      for(int i = 0; i < w; i++)
      {
        current_qimage.setPixel(i,j,qRgb(current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1]));
      }
    }

这是我得到的:

首先,输出不是绿色和黑色,其次,与灰度图像相比,它是缩放的。

你有什么线索可以得到greenMode吗?

【问题讨论】:

    标签: qt opencv qimage


    【解决方案1】:
    qRgb(current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1],current_image->imageData[i+j*w+1])
    

    您对所有三个 RGB 颜色分量使用相同的值。 R == G == B 总是会导致灰色。

    要将 RGB 值转换为绿色/黑色,例如,您可以转换为灰度(使用亮度方法),然后将其着色为绿色:

    const int v = qRound(  0.21 * pixel.red() + 0.71 * pixel.green() + 0.07 * pixel.blue() );
    setPixel( i, j, qRgb( v, 0, 0 ) );
    

    (可能有更复杂的着色方法)。

    对于缩放,我假设在计算 current_image 的索引时会发生错误。您对两个图像使用相同的 (i+j*w+1),但灰色有 1 个通道和第二个 3(第三个 cvCreateImage 参数)。所以后者每个像素会多出两个值。

    【讨论】:

    • 好的。现在当我想以 RGB 显示输出时,我仍然有上面相同的图。我做了: current_image = cvCreateImage(cvGetSize(frame),8,3); cvCvtColor(frame,current_image,CV_BGR2RGB); for(int j = 0; j imageData[i+jw +2],current_image->imageData[i+jw+1],current_image->imageData[i+j*w])); } } 但它不工作...
    • 只有+2,+1,+0是不行的,如果每个像素有3个字节,那前面肯定有一个因子3,比如3*(i+j*w)跨度>
    • 我会先尝试让彩色图像正确,然后转换为绿色/黑色。
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 2015-10-13
    • 2018-09-15
    • 2015-12-14
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多