The problem is that you are trying to access each pixel as an int (32-bit image per pixel), while your image is a three-channel unsigned char (24-bit image per pixel) or a single-channel unsigned char (8 bit per pixel image) for grayscale. You can try to access every pixel like this for a shade of gray.
for (int i =0; i<image2.width; i++){ image2.at<unsigned char>(i, i) = 255; }
or how is it for color
for (int i =0; i<image2.width; i++){ image2.at<Vec3b>(i, i)[0] = 255; image2.at<Vec3b>(i, i)[1] = 255; image2.at<Vec3b>(i, i)[2] = 255; }
source share