1
votes

I'm trying to manually change every pixel in a Mat.
For simplicity reasons, let's say I want to color each pixel black. I'm using the following method:

for (int i = 0; i < imageToWorkWith.rows; i++) {
    for (int j = 0; j < imageToWorkWith.cols; j++) {
        imageToWorkWith.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);
    }
}

Logically, it seems like this should go over every pixel in the mat, as it reads all the possible combinations of row/col.
Unfortunately, this doesn't work. For every image, I'm missing a "chunk" of columns. For example, when loading this image:
enter image description here
The result is this:
enter image description here
This "chunk" I'm missing is the same size, no matter what image I use. I can't seem to understand the reason for that. I know that the order of row/col for the "at" function is (row, col), but i tried switching them just for kicks, and the result of course is even worse.

What am I missing here? is looping over all rows/cols not enough?

1
everything seems to be correct, try to log the value of imageToWorkWith.cols, is it coming correct or not. number or rows is working fine by column count seems to be break - Manish Agrawal
I just tested your code, and it work fine! I'm not using ios, so Aurelius suggestion is correct - Engine
@Aurelius , yes! I have no idea how I missed that when researching the issue. Anyways, the problem was indeed the image having 4 channels and not 3. - Darkshore Grouper

1 Answers

0
votes

Just use Vec4b instead of Vec3b as the image by default is having 4 channels in ios. The result will be full white.

for (int i = 0; i < imageToWorkWith.rows; i++) {
    for (int j = 0; j < imageToWorkWith.cols; j++) {
        imageToWorkWith.at<cv::Vec4b>(i,j) = cv::Vec4b(0,0,0);
    }
}