G_Money
G_Money

Reputation: 318

Pixel modifications does not cover whole image

I've been doing some work with opencv and image processing. Every once in a while i've run into the situation where when I try to do some pixel modifications manually like here:

cv::Mat newImg = cv::Mat::zeros(img.size(), img.type());

    for( int y = 0; y < img.rows; y++ )
    {
        for( int x = 0; x < img.cols; x++ )
        {
            cv::Vec3b intensity = img.at<cv::Vec3b>(y, x);
            r = intensity.val[0];
            g = intensity.val[1];
            b = intensity.val[2];

            intensity.val[0] = r - (r * modify - r);
            intensity.val[1] = g - (g * modify - g);
            intensity.val[2] = b - (b * modify - b);

            newImg.at<cv::Vec3b>(y, x) = intensity;
        }
    }

This produces a new image that has a black box like so

enter image description here

Now i just dont understand why the for loops do not cover the whole photo, ive managed to fix this before but by pure luck and still do not understand why and where this problem comes from.

So my question in short is: How do i remove this black box from my images?

Thanks

Upvotes: 1

Views: 339

Answers (1)

Aurelius
Aurelius

Reputation: 11329

The most common cause of this issue is that you are accessing the image with the wrong element type. You use .at<cv::Vec3b>(y,x) for pixel access. This assumes that the image has only BGR channels. Your image data is probably in BGRA format, especially since this is iOS's native image format.

To remedy your problem, change all instances of cv::Vec3b with cv::Vec4b, and the code should work correctly.

Upvotes: 5

Related Questions