Fairly new to C++ and I've been working with the OpenCV library. I'm attempting to create a dimming function for an image viewer. This is the main body of void dim(int val)
.
I'm looping over every pixel, getting a reference to the RGB values of the pixel within a Vec3b then decreasing them by some change factor, calculated from val / 255
(where 0 <= val <= 255
. But for some reason the pix[0]
etc are all being set to 0, meaning that for any value of val
the image turns black.
May be doing something slightly stupid with floating point here, or misunderstanding some aspect of OpenCV; but I can't see what it is.
for(int x = 0; x < rows; x++) {
for(int y = 0; y < cols; y++) {
Vec3b &pix = dst.at<Vec3b>(x,y);
float change = val / 255.0f;
pix[0] = (uchar)(pix[0] * change);
pix[1] = (uchar)(pix[1] * change);
pix[2] = (uchar)(pix[2] * change);
}
}
Thanks in advance!
val
too small, or the resultdst
will turns all black. – WangYudongdst *= val/255.0f
. Also, it looks like you have your indices wrong. You should havex < cols
andy < rows
. – Nicu Stiurca