Get RGB pixels from input image and reconstruct an output image in opencv

前端 未结 4 1842
灰色年华
灰色年华 2020-12-22 07:04

I want to load the image in opencv and split the image into channels(RGB) and i want to increase any one of the colors and getting that corresponding output image.is there a

相关标签:
4条回答
  • 2020-12-22 07:20

    @karlphillip: Generally a better solution for RGB images - handles any padding at row ends, also parallelizes nicely with 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] << " "; 
        }
    }
    
    0 讨论(0)
  • 2020-12-22 07:23

    Another option is to manually iterate on the pixels of the image and work on the channel that interests you. This will give you the flexibility to manipulate each channel individually or as a group.

    The following code uses the C interface of OpenCV:

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

    However, if you want to add a fixed value to a certain channel you might want to check @Popovici's answer.

    0 讨论(0)
  • 2020-12-22 07:31

    Well to add any scalar to an RGB image you can use cvAddS(srcImage, scalarToAdd, dstImage). Here is an example:

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

    Haven't tested the code, hope it helps.

    0 讨论(0)
  • 2020-12-22 07:39

    With the OpenCV C++ interface you can simply add a Scalar to an image with the overloaded arithmetic operators.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题