Suppose there is a black and white image of 10x10 pixels in size, then it needs to be converted to a 10x10 matrix in which the black color will correspond to 1, and 0 to white or you can use 1 and -1 it does not really matter (one of these). Preferably in C ++.

unsigned char* readBMP(const char* filename) { int i; FILE* f = fopen(filename, "rb"); unsigned char info[54]; fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header // extract image height and width from header int width = *(int*)&info[18]; int height = *(int*)&info[22]; int size = 3 * width * height; unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once fclose(f); for (i = 0; i < size; i += 3) { unsigned char tmp = data[i]; data[i] = data[i + 2]; data[i + 2] = tmp; } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { cout << "R: " << (int)data[3 * (i * width + j)] << "; G: " << (int)data[3 * (i * width + j) + 1] << "; B: " << (int)data[3 * (i * width + j) + 2] << endl; } } return data; } 
  • What image library are you using? And what image format? - cpp questions
  • Do you have access to image pixels? - MBo
  • Here is the code used to read bmp black and white - vados inferno
  • Only code to read a color image. - mkkik
  • one
    The code is designed to read 24-bit bitmap, and it is incorrect - it will fail if the width is not a multiple of 4. But what prevents you from adjusting this basis to the desired format? - MBo

0