Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OpenCV c++ trouble while iterating over the image pixels

I tried to change colors from an image but for unknown reasons, the changes don’t apply over the entire region of the image. (I use c++ with opencv)

To test it, I tried to simply put the same color in the every pixel of the image ( (36, 81, 214) for every pixel ). For a 360 x 600 image, the color is applied to all 358 rows (because of padding – initially I tried to implement convolution transformation from scratch but this is not the point of this topic), BUT ONLY to the first 150 cols – and I don’t understand why – I added final_i and final_j to test if i,j variables values reached 358, 598 values and they reached them – then why the new color is applied only to a region of the image?

Here is the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Mat apply_convolution_kernel(Mat img_color, int kernel[][3], int kernel_size){
    int rows = img_color.rows;
    int cols = img_color.cols;

    Mat result = Mat(rows,cols, CV_32SC3, 0.0);
    cout<<img_color.rows<< " "<<img_color.cols<<endl;
    cout<<result.rows<< " "<<result.cols<<endl;

    int final_i = 0;
    int final_j = 0;
    // apply the kernel operator
    int aux_size = kernel_size / 2; // e.g for 3 x 3 kernel, we need [3/2] = 1 auxiliary padding
    for(int i=aux_size;i<rows - aux_size;i++){
        for(int j = aux_size;j<cols - aux_size;j++){

            Vec3b & pixel_from_res = result.at<Vec3b>(i,j);
            pixel_from_res[0] = 36;
            pixel_from_res[1] = 81;
            pixel_from_res[2] = 214;

            final_i = i;
            final_j = j;
        }
    }

    cout<<"final i: "<< final_i << " final j: "<<final_j<<endl;

    return result;
}


int main()
{
    cout << "Start" << endl;
    Mat img_color = imread("sakura.jpg", 1);
    int conv_size = 3;
    int conv_operator[3][3] = {
        {0, 1, 0},
        {1, 4, 1},
        {0, 1, 0}
        } ;
    Mat m1 =apply_convolution_kernel(img_color, conv_operator,3);
    imwrite("filter.jpg", m1);

    return 0;
}

Also, the resulted image is: (just for exemplification)

enter image description here

>Solution :

Type of every channel of matrix element is 32 bits integer – CV_32SC3.

If you want to read a pixel of image as structure with 3 channels, where precision of channel is int, you should use Vec3i:

Vec3i & pixel_from_res = result.at<Vec3i>(i,j);

or change a type of matrix elements to be CV_8UC3 -> for such type you can apply Vec3b.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading