【问题标题】:Get RGB pixels from input image and reconstruct an output image in opencv从输入图像中获取 RGB 像素并在 opencv 中重建输出图像
【发布时间】:2011-07-30 00:21:06
【问题描述】:

我想在 opencv 中加载图像并将图像拆分为通道(RGB),我想增加任何一种颜色并获得相应的输出图像。有没有最简单的方法来解决这个问题?

【问题讨论】:

    标签: image opencv rgb


    【解决方案1】:

    要向 RGB 图像添加任何标量,您可以使用 cvAddS(srcImage, scalarToAdd, dstImage)。 这是一个例子:

    int main(int argc, char** argv)
    
    {
    // Create a named window with the name of the file.
    cvNamedWindow( argv[1], 1 );
    // Load the image from the given file name.
    IplImage* img = cvLoadImage( argv[1] );
    //Make a scalar to add 30 to Blue Color and 20 to Red (BGR format)
    CvScalar colorAdd = cvScalar(30.0, 0, 20.0);
    cvAddS(img, colorAdd, img);
    // Show the image in the named window
    cvShowImage( argv[1], img );
    // Idle until the user hits the “Esc” key.
    while( 1 ) {
    if( cvWaitKey( 100 ) == 27 ) break;
    }
    cvDestroyWindow( argv[1] );
    cvReleaseImage( &img );
    exit(0);
    }
    

    尚未测试代码,希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      @karlphillip:对于 RGB 图像来说通常是一个更好的解决方案 - 处理行尾的任何填充,也可以很好地与 OMP 并行!

      for (int i=0; i < height;i++) 
      {
          unsigned char *pRow = pRGBImg->ptr(i);
          for (int j=0; j < width;j+=bpp) 
          // For educational puporses, here is how to print each R G B channel:
            std::cout << std::dec << "R:" << (int) pRow->imageData[j] <<  
                                " G:" << (int) pRow->imageData[j+1] <<  
                                " B:" << (int) pRow->imageData[j+2] << " "; 
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用 OpenCV C++ 接口,您可以使用重载的算术运算符简单地将标量添加到图像。

        int main(int argc, const char * argv[]) {
            cv::Mat image;
            // read an image
            if (argc < 2)
                return 2;
        
            image = cv::imread(argv[1]);
        
            if (!image.data) {
                std::cout << "Image file not found\n";
                return 1;
            }
        
            cv::Mat image2 = image.clone(); // Make a deep copy of the image
            image2 += cv::Scalar(30,0,20); // Add 30 to blue, 20 to red
        
            cv::namedWindow("original");
            cv::imshow("original", image);
        
            cv::namedWindow("addcolors");
            cv::imshow("addcolors", image2);
        
            cv::waitKey(0);
            return 0;
        }
        

        【讨论】:

          【解决方案4】:

          另一种选择是手动迭代图像的像素,然后在您感兴趣的频道上工作。这将使您能够灵活地单独或作为一个组操作每个通道。

          以下代码使用OpenCV的C接口:

          IplImage* pRGBImg = cvLoadImage("test.png", CV_LOAD_IMAGE_UNCHANGED); 
          int width = pRGBImg->width; 
          int height = pRGBImg->height;
          int bpp = pRGBImg->nChannels; 
          for (int i=0; i < width*height*bpp; i+=bpp) 
          {
            // For educational puporses, here is how to print each R G B channel:
            std::cout << std::dec << "R:" << (int) pRGBImg->imageData[i] <<  
                                    " G:" << (int) pRGBImg->imageData[i+1] <<  
                                    " B:" << (int) pRGBImg->imageData[i+2] << " "; 
          }
          

          但是,如果您想为某个频道添加固定值,您可能需要查看@Popovici 的答案。

          【讨论】:

            猜你喜欢
            • 2018-12-15
            • 1970-01-01
            • 1970-01-01
            • 2011-12-15
            • 1970-01-01
            • 2012-02-24
            • 2012-02-21
            • 2017-06-08
            • 2021-12-23
            相关资源
            最近更新 更多