kadu
kadu

Reputation: 513

Accessing elements from image in open cv using c++

I am using the following code to access a [5x5] block from an image. But i am getting a [15x5] block with every element repeated three times. Could anyone point out the error in this code.? I had asked a question in the link below about how to access the elements. How to access first 5*5 block from an image in open cv?

for(int m=0;m<10;m++)
{
    for(int n=0;n<90;n++)
    {
         int block_width = Ns;
         int block_height = Ns;
         int roi_origin_x = m;
         int roi_origin_y = n;
         cv::Rect roi(roi_origin_x, roi_origin_y, block_width, block_height);
         cv::Mat region = obtained_mask(roi);
         std::cout<< " region " <<region<< std::endl;
    }
}

Upvotes: 0

Views: 73

Answers (2)

Mohammad
Mohammad

Reputation: 494

It seems that your image is gray-scaled and you are trying to load it from a file as an RGB image. So you have repeated intensity values in every channel. Equal values for every channel in RGB represent a gray color (from white to black which moves in the diagonal of RGB cube). You can convert region to gray-scaled image using cvtColor or you can retrieve first channel of region's pixel which costs you less processing cycle time.

Upvotes: 1

BConic
BConic

Reputation: 8980

It probably has to do with the RGB channels of your image.

Try the following:

int block_width = Ns;
int block_height = Ns;
for(int roi_origin_y=0; roi_origin_y<90; ++roi_origin_y)
{
    for(int roi_origin_x=0; roi_origin_x<10; ++roi_origin_x)
    {
        if(roi_origin_y+block_width>=obtained_mask.rows || roi_origin_x+block_height>=obtained_mask.cols)
            break;
        cv::Mat region(block_height,block_width,obtained_mask.type());
        for(int dy=0; dy<block_height; ++dy)
        {
            for(int dy=0; dy<block_height; ++dy)
            {
                for(int k=0; k<obtained_mask.channels(); ++k)
                    region(dy,obtained_mask.channels()*dx+k) = obtained_mask(roi_origin_y+dy,obtained_mask.channels()*(roi_origin_x+dx)+k);
            }
        }
        // TODO: process NsxNs block stored in region
    }
}

Upvotes: 1

Related Questions